-
-
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.
- Loading branch information
Showing
7 changed files
with
305 additions
and
13 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,40 @@ | ||
--- | ||
"@effect/schema": patch | ||
--- | ||
|
||
Add `encodedBoundSchema` API. | ||
|
||
The `encodedBoundSchema` function is similar to `encodedSchema` but preserves the refinements up to the first transformation point in the | ||
original schema. | ||
|
||
**Function Signature:** | ||
|
||
```ts | ||
export const encodedBoundSchema = <A, I, R>(schema: Schema<A, I, R>): Schema<I> | ||
``` | ||
The term "bound" in this context refers to the boundary up to which refinements are preserved when extracting the encoded form of a schema. It essentially marks the limit to which initial validations and structure are maintained before any transformations are applied. | ||
**Example Usage:** | ||
```ts | ||
import { Schema } from "@effect/schema" | ||
|
||
const schema = Schema.Struct({ | ||
foo: Schema.String.pipe(Schema.minLength(3), Schema.compose(Schema.Trim)) | ||
}) | ||
|
||
// The resultingEncodedBoundSchema preserves the minLength(3) refinement, | ||
// ensuring the string length condition is enforced but omits the Trim transformation. | ||
const resultingEncodedBoundSchema = Schema.encodedBoundSchema(schema) | ||
|
||
// resultingEncodedBoundSchema is the same as: | ||
Schema.Struct({ | ||
foo: Schema.String.pipe(Schema.minLength(3)) | ||
}) | ||
``` | ||
|
||
In the provided example: | ||
|
||
- **Initial Schema**: The schema for `foo` includes a refinement to ensure strings have a minimum length of three characters and a transformation to trim the string. | ||
- **Resulting Schema**: `resultingEncodedBoundSchema` maintains the `minLength(3)` condition, ensuring that this validation persists. However, it excludes the trimming transformation, focusing solely on the length requirement without altering the string's formatting. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import * as AST from "@effect/schema/AST" | ||
import * as S from "@effect/schema/Schema" | ||
import { describe, expect, it } from "vitest" | ||
|
||
describe("encodedBoundAST", () => { | ||
it("refinements", () => { | ||
const ast = S.String.pipe(S.minLength(2)).ast | ||
const encodedAST = AST.encodedBoundAST(ast) | ||
expect(encodedAST === ast).toBe(true) | ||
}) | ||
|
||
describe(`should return the same reference if the AST doesn't represent a transformation`, () => { | ||
it("declaration (true)", () => { | ||
const schema = S.OptionFromSelf(S.String) | ||
expect(AST.encodedBoundAST(schema.ast) === schema.ast).toBe(true) | ||
}) | ||
|
||
it("declaration (false)", () => { | ||
const schema = S.OptionFromSelf(S.NumberFromString) | ||
expect(AST.encodedBoundAST(schema.ast) === schema.ast).toBe(false) | ||
}) | ||
|
||
it("tuple (true)", () => { | ||
const schema = S.Tuple(S.String, S.Number) | ||
expect(AST.encodedBoundAST(schema.ast) === schema.ast).toBe(true) | ||
}) | ||
|
||
it("tuple (false)", () => { | ||
const schema = S.Tuple(S.String, S.NumberFromString) | ||
expect(AST.encodedBoundAST(schema.ast) === schema.ast).toBe(false) | ||
}) | ||
|
||
it("array (true)", () => { | ||
const schema = S.Array(S.Number) | ||
expect(AST.encodedBoundAST(schema.ast) === schema.ast).toBe(true) | ||
}) | ||
|
||
it("array (false)", () => { | ||
const schema = S.Array(S.NumberFromString) | ||
expect(AST.encodedBoundAST(schema.ast) === schema.ast).toBe(false) | ||
}) | ||
|
||
it("union (true)", () => { | ||
const schema = S.Union(S.String, S.Number) | ||
expect(AST.encodedBoundAST(schema.ast) === schema.ast).toBe(true) | ||
}) | ||
|
||
it("union (false)", () => { | ||
const schema = S.Union(S.String, S.NumberFromString) | ||
expect(AST.encodedBoundAST(schema.ast) === schema.ast).toBe(false) | ||
}) | ||
|
||
it("struct (true)", () => { | ||
const schema = S.Struct({ a: S.String, b: S.Number }) | ||
expect(AST.encodedBoundAST(schema.ast) === schema.ast).toBe(true) | ||
}) | ||
|
||
it("struct (false)", () => { | ||
const schema = S.Struct({ a: S.String, b: S.NumberFromString }) | ||
expect(AST.encodedBoundAST(schema.ast) === schema.ast).toBe(false) | ||
}) | ||
|
||
it("record (true)", () => { | ||
const schema = S.Record(S.String, S.Number) | ||
expect(AST.encodedBoundAST(schema.ast) === schema.ast).toBe(true) | ||
}) | ||
|
||
it("record (false)", () => { | ||
const schema = S.Record(S.String, S.NumberFromString) | ||
expect(AST.encodedBoundAST(schema.ast) === schema.ast).toBe(false) | ||
}) | ||
}) | ||
}) |
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,48 @@ | ||
import * as S from "@effect/schema/Schema" | ||
import * as Util from "@effect/schema/test/TestUtils" | ||
import { describe, it } from "vitest" | ||
|
||
describe("encodedBoundSchema", () => { | ||
it("refinements", async () => { | ||
const StringFromStruct = S.transform( | ||
S.Struct({ value: S.String }).annotations({ identifier: "ValueStruct" }), | ||
S.String, | ||
{ | ||
encode: (name) => ({ value: name }), | ||
decode: (nameInForm) => nameInForm.value | ||
} | ||
).annotations({ identifier: "StringFromStruct" }) | ||
|
||
const Handle = S.String.pipe( | ||
S.minLength(3), | ||
S.pattern(/^[a-zA-Z0-9]+(?:-[a-zA-Z0-9]+)*$/) | ||
).annotations({ identifier: "Handle" }) | ||
|
||
const FullSchema = S.Struct({ | ||
names: S.NonEmptyArray(StringFromStruct), | ||
handle: Handle | ||
}).annotations({ identifier: "FullSchema" }) | ||
|
||
const schema = S.encodedBoundSchema(FullSchema) | ||
|
||
await Util.expectDecodeUnknownSuccess(schema, { | ||
names: [{ value: "Name #1" }], | ||
handle: "user123" | ||
}) | ||
|
||
await Util.expectDecodeUnknownFailure( | ||
schema, | ||
{ | ||
names: [{ value: "Name #1" }], | ||
handle: "aa" | ||
}, | ||
`{ readonly names: readonly [ValueStruct, ...ValueStruct[]]; readonly handle: Handle } | ||
└─ ["handle"] | ||
└─ Handle | ||
└─ From side refinement failure | ||
└─ a string at least 3 character(s) long | ||
└─ Predicate refinement failure | ||
└─ Expected a string at least 3 character(s) long, actual "aa"` | ||
) | ||
}) | ||
}) |