-
-
Notifications
You must be signed in to change notification settings - Fork 261
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for
TemplateLiteral
parameters in TemplateLiteral
, cl… (
- Loading branch information
Showing
7 changed files
with
568 additions
and
180 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
--- | ||
"effect": patch | ||
--- | ||
|
||
Schema: Add support for `TemplateLiteral` parameters in `TemplateLiteral`, closes #4166. | ||
|
||
This update also adds support for `TemplateLiteral` and `TemplateLiteralParser` parameters in `TemplateLiteralParser`. | ||
|
||
Before | ||
|
||
```ts | ||
import { Schema } from "effect" | ||
|
||
const schema = Schema.TemplateLiteralParser( | ||
"<", | ||
Schema.TemplateLiteralParser("h", Schema.Literal(1, 2)), | ||
">" | ||
) | ||
/* | ||
throws: | ||
Error: Unsupported template literal span | ||
schema (TemplateLiteral): `h${"1" | "2"}` | ||
*/ | ||
``` | ||
|
||
After | ||
|
||
```ts | ||
import { Schema } from "effect" | ||
|
||
// Schema<readonly ["<", readonly ["h", 2 | 1], ">"], "<h2>" | "<h1>", never> | ||
const schema = Schema.TemplateLiteralParser( | ||
"<", | ||
Schema.TemplateLiteralParser("h", Schema.Literal(1, 2)), | ||
">" | ||
) | ||
|
||
console.log(Schema.decodeUnknownSync(schema)("<h1>")) | ||
// Output: [ '<', [ 'h', 1 ], '>' ] | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
--- | ||
"effect": patch | ||
--- | ||
|
||
Schema: Fix bug in `TemplateLiteralParser` where unions of numeric literals were not coerced correctly. | ||
|
||
Before | ||
|
||
```ts | ||
import { Schema } from "effect" | ||
|
||
const schema = Schema.TemplateLiteralParser("a", Schema.Literal(1, 2)) | ||
|
||
console.log(Schema.decodeUnknownSync(schema)("a1")) | ||
/* | ||
throws: | ||
ParseError: (`a${"1" | "2"}` <-> readonly ["a", 1 | 2]) | ||
└─ Type side transformation failure | ||
└─ readonly ["a", 1 | 2] | ||
└─ [1] | ||
└─ 1 | 2 | ||
├─ Expected 1, actual "1" | ||
└─ Expected 2, actual "1" | ||
*/ | ||
``` | ||
|
||
After | ||
|
||
```ts | ||
import { Schema } from "effect" | ||
|
||
const schema = Schema.TemplateLiteralParser("a", Schema.Literal(1, 2)) | ||
|
||
console.log(Schema.decodeUnknownSync(schema)("a1")) | ||
// Output: [ 'a', 1 ] | ||
|
||
console.log(Schema.decodeUnknownSync(schema)("a2")) | ||
// Output: [ 'a', 2 ] | ||
|
||
console.log(Schema.decodeUnknownSync(schema)("a3")) | ||
/* | ||
throws: | ||
ParseError: (`a${"1" | "2"}` <-> readonly ["a", 1 | 2]) | ||
└─ Encoded side transformation failure | ||
└─ Expected `a${"1" | "2"}`, actual "a3" | ||
*/ | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.