-
-
Notifications
You must be signed in to change notification settings - Fork 231
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
7 changed files
with
306 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
--- | ||
"@effect/schema": patch | ||
--- | ||
|
||
Enhanced Parsing with `TemplateLiteralParser`, closes #3307 | ||
|
||
In this update we've introduced a sophisticated API for more refined string parsing: `TemplateLiteralParser`. This enhancement stems from recognizing limitations in the `Schema.TemplateLiteral` and `Schema.pattern` functionalities, which effectively validate string formats without extracting structured data. | ||
|
||
**Overview of Existing Limitations** | ||
|
||
The `Schema.TemplateLiteral` function, while useful as a simple validator, only verifies that an input conforms to a specific string pattern by converting template literal definitions into regular expressions. Similarly, `Schema.pattern` employs regular expressions directly for the same purpose. Post-validation, both methods require additional manual parsing to convert the validated string into a usable data format. | ||
|
||
**Introducing TemplateLiteralParser** | ||
|
||
To address these limitations and eliminate the need for manual post-validation parsing, the new `TemplateLiteralParser` API has been developed. It not only validates the input format but also automatically parses it into a more structured and type-safe output, specifically into a **tuple** format. | ||
|
||
This new approach enhances developer productivity by reducing boilerplate code and simplifying the process of working with complex string inputs. | ||
|
||
**Example** (string based schemas) | ||
|
||
```ts | ||
import { Schema } from "@effect/schema" | ||
|
||
// const schema: Schema.Schema<readonly [number, "a", string], `${string}a${string}`, never> | ||
const schema = Schema.TemplateLiteralParser( | ||
Schema.NumberFromString, | ||
"a", | ||
Schema.NonEmptyString | ||
) | ||
|
||
console.log(Schema.decodeEither(schema)("100ab")) | ||
// { _id: 'Either', _tag: 'Right', right: [ 100, 'a', 'b' ] } | ||
|
||
console.log(Schema.encode(schema)([100, "a", "b"])) | ||
// { _id: 'Either', _tag: 'Right', right: '100ab' } | ||
``` | ||
|
||
**Example** (number based schemas) | ||
|
||
```ts | ||
import { Schema } from "@effect/schema" | ||
|
||
// const schema: Schema.Schema<readonly [number, "a"], `${number}a`, never> | ||
const schema = Schema.TemplateLiteralParser(Schema.Int, "a") | ||
|
||
console.log(Schema.decodeEither(schema)("1a")) | ||
// { _id: 'Either', _tag: 'Right', right: [ 1, 'a' ] } | ||
|
||
console.log(Schema.encode(schema)([1, "a"])) | ||
// { _id: 'Either', _tag: 'Right', right: '1a' } | ||
``` |
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
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
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
101 changes: 101 additions & 0 deletions
101
packages/schema/test/Schema/TemplateLiteralParser.test.ts
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,101 @@ | ||
import * as Schema from "@effect/schema/Schema" | ||
import * as Util from "@effect/schema/test/TestUtils" | ||
import { describe, expect, it } from "vitest" | ||
|
||
describe("TemplateLiteralParser", () => { | ||
it("should throw on unsupported template literal spans", () => { | ||
expect(() => Schema.TemplateLiteralParser(Schema.Boolean)).toThrow( | ||
new Error(`Unsupported template literal span | ||
schema (BooleanKeyword): boolean`) | ||
) | ||
expect(() => Schema.TemplateLiteralParser(Schema.SymbolFromSelf)).toThrow( | ||
new Error(`Unsupported template literal span | ||
schema (SymbolKeyword): symbol`) | ||
) | ||
}) | ||
|
||
it("should expose the params", () => { | ||
const params = ["/", Schema.Int, "/", Schema.String] as const | ||
const schema = Schema.TemplateLiteralParser(...params) | ||
expect(schema.params).toStrictEqual(params) | ||
}) | ||
|
||
describe("number based schemas", () => { | ||
it("decoding", async () => { | ||
const schema = Schema.TemplateLiteralParser(Schema.Int, "a") | ||
await Util.expectDecodeUnknownSuccess(schema, "1a", [1, "a"]) | ||
await Util.expectDecodeUnknownFailure( | ||
schema, | ||
"1.1a", | ||
`(\`\${number}a\` <-> readonly [Int, "a"]) | ||
└─ Type side transformation failure | ||
└─ readonly [Int, "a"] | ||
└─ [0] | ||
└─ Int | ||
└─ Predicate refinement failure | ||
└─ Expected Int, actual 1.1` | ||
) | ||
}) | ||
|
||
it("encoding", async () => { | ||
const schema = Schema.TemplateLiteralParser(Schema.Int, "a", Schema.Char) | ||
await Util.expectEncodeSuccess(schema, [1, "a", "b"], "1ab") | ||
await Util.expectEncodeFailure( | ||
schema, | ||
[1.1, "a", ""], | ||
`(\`\${number}a\${string}\` <-> readonly [Int, "a", Char]) | ||
└─ Type side transformation failure | ||
└─ readonly [Int, "a", Char] | ||
└─ [0] | ||
└─ Int | ||
└─ Predicate refinement failure | ||
└─ Expected Int, actual 1.1` | ||
) | ||
await Util.expectEncodeFailure( | ||
schema, | ||
[1, "a", ""], | ||
`(\`\${number}a\${string}\` <-> readonly [Int, "a", Char]) | ||
└─ Type side transformation failure | ||
└─ readonly [Int, "a", Char] | ||
└─ [2] | ||
└─ Char | ||
└─ Predicate refinement failure | ||
└─ Expected Char, actual ""` | ||
) | ||
}) | ||
}) | ||
|
||
describe("string based schemas", () => { | ||
it("decoding", async () => { | ||
const schema = Schema.TemplateLiteralParser(Schema.NumberFromString, "a", Schema.NonEmptyString) | ||
await Util.expectDecodeUnknownSuccess(schema, "100ab", [100, "a", "b"]) | ||
await Util.expectDecodeUnknownFailure( | ||
schema, | ||
"-ab", | ||
`(\`\${string}a\${string}\` <-> readonly [NumberFromString, "a", NonEmptyString]) | ||
└─ Type side transformation failure | ||
└─ readonly [NumberFromString, "a", NonEmptyString] | ||
└─ [0] | ||
└─ NumberFromString | ||
└─ Transformation process failure | ||
└─ Expected NumberFromString, actual "-"` | ||
) | ||
}) | ||
|
||
it("encoding", async () => { | ||
const schema = Schema.TemplateLiteralParser(Schema.NumberFromString, "a", Schema.Char) | ||
await Util.expectEncodeSuccess(schema, [100, "a", "b"], "100ab") | ||
await Util.expectEncodeFailure( | ||
schema, | ||
[100, "a", ""], | ||
`(\`\${string}a\${string}\` <-> readonly [NumberFromString, "a", Char]) | ||
└─ Type side transformation failure | ||
└─ readonly [NumberFromString, "a", Char] | ||
└─ [2] | ||
└─ Char | ||
└─ Predicate refinement failure | ||
└─ Expected Char, actual ""` | ||
) | ||
}) | ||
}) | ||
}) |