From aa22b33c522adce8e23b4e5e75bc824332bdfa29 Mon Sep 17 00:00:00 2001 From: Giulio Canti Date: Mon, 12 May 2025 17:03:33 +0200 Subject: [PATCH] Refactor numeric filters (#119) * fix Class constructor defaults * refactor order checks --- packages/effect/SCHEMA.md | 97 +++++++-- packages/effect/dtslint/Schema.tst.ts | 4 +- packages/effect/src/BigInt.ts | 8 + packages/effect/src/Schema.ts | 14 +- packages/effect/src/SchemaCheck.ts | 290 ++++++++++++++++++++++---- packages/effect/test/Schema.test.ts | 247 +++++++++++++++++++--- packages/effect/test/SchemaTest.ts | 8 +- 7 files changed, 568 insertions(+), 100 deletions(-) diff --git a/packages/effect/SCHEMA.md b/packages/effect/SCHEMA.md index 08907e5dce..25e7304364 100644 --- a/packages/effect/SCHEMA.md +++ b/packages/effect/SCHEMA.md @@ -270,7 +270,7 @@ import { Result, Schema } from "effect" const schema = Schema.Struct({ a: Schema.Number.pipe( - Schema.setConstructorDefault(() => Result.succeedSome(-1)) + Schema.withConstructorDefault(() => Result.succeedSome(-1)) ) }) @@ -289,7 +289,7 @@ import { Effect, Option, Schema, SchemaResult } from "effect" const schema = Schema.Struct({ a: Schema.Number.pipe( - Schema.setConstructorDefault(() => + Schema.withConstructorDefault(() => Effect.gen(function* () { yield* Effect.sleep(100) return Option.some(-1) @@ -314,7 +314,7 @@ class ConstructorService extends Context.Tag< const schema = Schema.Struct({ a: Schema.Number.pipe( - Schema.setConstructorDefault(() => + Schema.withConstructorDefault(() => Effect.gen(function* () { yield* Effect.sleep(100) const oservice = yield* Effect.serviceOption(ConstructorService) @@ -351,9 +351,9 @@ import { Result, Schema } from "effect" const schema = Schema.Struct({ a: Schema.Struct({ b: Schema.Number.pipe( - Schema.setConstructorDefault(() => Result.succeedSome(-1)) + Schema.withConstructorDefault(() => Result.succeedSome(-1)) ) - }).pipe(Schema.setConstructorDefault(() => Result.succeedSome({}))) + }).pipe(Schema.withConstructorDefault(() => Result.succeedSome({}))) }) console.log(schema.makeSync({})) @@ -567,20 +567,31 @@ A **filter factory** is a function that returns a reusable filter. This pattern You can now create filters like `greaterThan` for any type with an ordering. -**Example** (Reusable `greaterThan` filter) +**Example** (Deriving a `greaterThan` filter) ```ts import { Order, SchemaCheck } from "effect" // Creates a filter factory using an Order instance // Returns a `SchemaAST.Filter` -export const makeGreaterThan = (O: Order.Order) => { - const greaterThan = Order.greaterThan(O) - return (exclusiveMinimum: T) => - SchemaCheck.make((input) => greaterThan(input, exclusiveMinimum), { - title: `greaterThan(${exclusiveMinimum})`, - description: `a value greater than ${exclusiveMinimum}` - }) +export const deriveGreaterThan = (options: { + readonly order: Order.Order + readonly annotate?: ((exclusiveMinimum: T) => Annotations) | undefined + readonly format?: (value: T) => string | undefined +}) => { + const greaterThan = Order.greaterThan(options.order) + const format = options.format ?? globalThis.String + return (exclusiveMinimum: T, annotations?: Annotations) => { + return SchemaCheck.make( + (input) => greaterThan(input, exclusiveMinimum), + { + title: `greaterThan(${format(exclusiveMinimum)})`, + description: `a value greater than ${format(exclusiveMinimum)}`, + ...options.annotate?.(exclusiveMinimum), + ...annotations + } + ) + } } ``` @@ -1764,6 +1775,66 @@ Schema.String.pipe( ) ``` +## Numbers + +```ts +import { Schema, SchemaCheck } from "effect" + +Schema.Number.pipe(Schema.check(SchemaCheck.between(5, 10))) +Schema.Number.pipe(Schema.check(SchemaCheck.greaterThan(5))) +Schema.Number.pipe(Schema.check(SchemaCheck.greaterThanOrEqualTo(5))) +Schema.Number.pipe(Schema.check(SchemaCheck.lessThan(5))) +Schema.Number.pipe(Schema.check(SchemaCheck.lessThanOrEqualTo(5))) +Schema.Number.pipe(Schema.check(SchemaCheck.positive)) +Schema.Number.pipe(Schema.check(SchemaCheck.nonNegative)) +Schema.Number.pipe(Schema.check(SchemaCheck.negative)) +Schema.Number.pipe(Schema.check(SchemaCheck.nonPositive)) +Schema.Number.pipe(Schema.check(SchemaCheck.multipleOf(5))) +``` + +## Integers + +```ts +import { Schema, SchemaCheck } from "effect" + +Schema.Number.pipe(Schema.check(SchemaCheck.int)) +Schema.Number.pipe(Schema.check(SchemaCheck.int32)) +``` + +## BigInts + +```ts +import { BigInt, Order, Schema, SchemaCheck } from "effect" + +const options = { order: Order.bigint } + +const between = SchemaCheck.deriveBetween(options) +const greaterThan = SchemaCheck.deriveGreaterThan(options) +const greaterThanOrEqualTo = SchemaCheck.deriveGreaterThanOrEqualTo(options) +const lessThan = SchemaCheck.deriveLessThan(options) +const lessThanOrEqualTo = SchemaCheck.deriveLessThanOrEqualTo(options) +const multipleOf = SchemaCheck.deriveMultipleOf({ + remainder: BigInt.remainder, + zero: 0n +}) + +const positive = greaterThan(0n) +const nonNegative = greaterThanOrEqualTo(0n) +const negative = lessThan(0n) +const nonPositive = lessThanOrEqualTo(0n) + +Schema.BigInt.pipe(Schema.check(between(5n, 10n))) +Schema.BigInt.pipe(Schema.check(greaterThan(5n))) +Schema.BigInt.pipe(Schema.check(greaterThanOrEqualTo(5n))) +Schema.BigInt.pipe(Schema.check(lessThan(5n))) +Schema.BigInt.pipe(Schema.check(lessThanOrEqualTo(5n))) +Schema.BigInt.pipe(Schema.check(multipleOf(5n))) +Schema.BigInt.pipe(Schema.check(positive)) +Schema.BigInt.pipe(Schema.check(nonNegative)) +Schema.BigInt.pipe(Schema.check(negative)) +Schema.BigInt.pipe(Schema.check(nonPositive)) +``` + ## RWC References - https://github.com/Anastasia-Labs/lucid-evolution/blob/5068114c9f8f95c6b997d0d2233a9e9543632f35/packages/experimental/src/TSchema.ts#L353 diff --git a/packages/effect/dtslint/Schema.tst.ts b/packages/effect/dtslint/Schema.tst.ts index 498b5e5bbc..afc0c2b31b 100644 --- a/packages/effect/dtslint/Schema.tst.ts +++ b/packages/effect/dtslint/Schema.tst.ts @@ -359,7 +359,7 @@ describe("Schema", () => { describe("flip", () => { it("Struct & setConstructorDefault", () => { const schema = Schema.Struct({ - a: Schema.String.pipe(Schema.setConstructorDefault(() => Result.succeedSome("c"))) + a: Schema.String.pipe(Schema.withConstructorDefault(() => Result.succeedSome("c"))) }) expect(schema.makeSync).type.toBe< (input: { readonly a?: string }, options?: Schema.MakeOptions | undefined) => { readonly a: string } @@ -398,7 +398,7 @@ describe("Schema", () => { it("withConstructorDefault", () => { const service = hole>() - const schema = Schema.String.pipe(Schema.setConstructorDefault(() => + const schema = Schema.String.pipe(Schema.withConstructorDefault(() => Effect.gen(function*() { yield* Effect.serviceOption(service) return Option.some("some-result") diff --git a/packages/effect/src/BigInt.ts b/packages/effect/src/BigInt.ts index 0502597daa..1e62262e5b 100644 --- a/packages/effect/src/BigInt.ts +++ b/packages/effect/src/BigInt.ts @@ -638,3 +638,11 @@ export const fromNumber = (n: number): Option.Option => { return Option.none() } } + +/** + * @since 4.0.0 + */ +export const remainder: { + (divisor: bigint): (self: bigint) => bigint + (self: bigint, divisor: bigint): bigint +} = dual(2, (self: bigint, divisor: bigint): bigint => self % divisor) diff --git a/packages/effect/src/Schema.ts b/packages/effect/src/Schema.ts index 6c595199a6..3ce70bd299 100644 --- a/packages/effect/src/Schema.ts +++ b/packages/effect/src/Schema.ts @@ -45,7 +45,6 @@ type DefaultConstructorToken = "no-constructor-default" | "has-constructor-defau */ export interface MakeOptions { readonly parseOptions?: SchemaAST.ParseOptions | undefined - readonly skipValidation?: boolean | undefined } /** @@ -155,9 +154,7 @@ export abstract class Bottom$< declare readonly "~encoded.isOptional": EncodedIsOptional declare readonly "~encoded.make.in": E - constructor(readonly ast: Ast) { - this.makeSync = this.makeSync.bind(this) - } + constructor(readonly ast: Ast) {} abstract rebuild(ast: this["ast"]): this["~rebuild.out"] pipe() { return pipeArguments(this, arguments) @@ -791,7 +788,7 @@ export interface tag extends setConstructorD */ export function tag(literal: Tag): tag { return Literal(literal).pipe( - setConstructorDefault(() => Result.succeedSome(literal)) + withConstructorDefault(() => Result.succeedSome(literal)) ) } @@ -1931,7 +1928,7 @@ export interface setConstructorDefault extends make { /** * @since 4.0.0 */ -export const setConstructorDefault = ( +export const withConstructorDefault = ( parser: ( input: O.Option, ast: SchemaAST.AST, @@ -2266,10 +2263,7 @@ function makeClass< return class extends Inherited { constructor(...[input, options]: ReadonlyArray) { - if (options?.skipValidation !== true) { - schema.makeSync(input, options) - } - super(input, { ...options, skipValidation: true }) + super({ ...input, ...schema.makeSync(input, options) }) } static readonly "~effect/Schema" = "~effect/Schema" diff --git a/packages/effect/src/SchemaCheck.ts b/packages/effect/src/SchemaCheck.ts index 345865fac7..1e92e0c8db 100644 --- a/packages/effect/src/SchemaCheck.ts +++ b/packages/effect/src/SchemaCheck.ts @@ -2,6 +2,7 @@ * @since 4.0.0 */ +import * as Num from "./Number.js" import * as Option from "./Option.js" import * as Order from "./Order.js" import * as Predicate from "./Predicate.js" @@ -258,71 +259,276 @@ export const finite = make((n: number) => globalThis.Number.isFinite(n), { * @category Order checks * @since 4.0.0 */ -const makeGreaterThan = (O: Order.Order) => { - const greaterThan = Order.greaterThan(O) +export const deriveGreaterThan = (options: { + readonly order: Order.Order + readonly annotate?: ((exclusiveMinimum: T) => Annotations) | undefined + readonly format?: (value: T) => string | undefined +}) => { + const greaterThan = Order.greaterThan(options.order) + const format = options.format ?? globalThis.String return (exclusiveMinimum: T, annotations?: Annotations) => { return make((input) => greaterThan(input, exclusiveMinimum), { - title: `greaterThan(${exclusiveMinimum})`, - description: `a value greater than ${exclusiveMinimum}`, - jsonSchema: { - type: "fragment", - fragment: { - exclusiveMinimum - } - }, - meta: { - id: "greaterThan", - exclusiveMinimum - }, + title: `greaterThan(${format(exclusiveMinimum)})`, + description: `a value greater than ${format(exclusiveMinimum)}`, + ...options.annotate?.(exclusiveMinimum), ...annotations }) } } -/** - * @category Number checks - * @since 4.0.0 - */ -export const greaterThan = makeGreaterThan(Order.number) - /** * @category Order checks * @since 4.0.0 */ -const makeBetween = (O: Order.Order) => { - const greaterThan = Order.greaterThan(O) - const lessThan = Order.lessThan(O) - return (minimum: T, maximum: T, annotations?: Annotations) => { - return make((input) => greaterThan(input, minimum) && lessThan(input, maximum), { - title: `between(${minimum}, ${maximum})`, - description: `a value between ${minimum} and ${maximum}`, - jsonSchema: { - type: "fragment", - fragment: { - minimum, - maximum - } - }, - meta: { - id: "between", - minimum, - maximum - }, +export const deriveGreaterThanOrEqualTo = (options: { + readonly order: Order.Order + readonly annotate?: ((exclusiveMinimum: T) => Annotations) | undefined + readonly format?: (value: T) => string | undefined +}) => { + const greaterThanOrEqualTo = Order.greaterThanOrEqualTo(options.order) + const format = options.format ?? globalThis.String + return (minimum: T, annotations?: Annotations) => { + return make((input) => greaterThanOrEqualTo(input, minimum), { + title: `greaterThanOrEqualTo(${format(minimum)})`, + description: `a value greater than or equal to ${format(minimum)}`, + ...options.annotate?.(minimum), ...annotations }) } } +/** + * @category Order checks + * @since 4.0.0 + */ +export const deriveLessThan = (options: { + readonly order: Order.Order + readonly annotate?: ((exclusiveMaximum: T) => Annotations) | undefined + readonly format?: (value: T) => string | undefined +}) => { + const lessThan = Order.lessThan(options.order) + const format = options.format ?? globalThis.String + return (exclusiveMaximum: T, annotations?: Annotations) => { + return make((input) => lessThan(input, exclusiveMaximum), { + title: `lessThan(${format(exclusiveMaximum)})`, + description: `a value less than ${format(exclusiveMaximum)}`, + ...options.annotate?.(exclusiveMaximum), + ...annotations + }) + } +} + +/** + * @category Order checks + * @since 4.0.0 + */ +export const deriveLessThanOrEqualTo = (options: { + readonly order: Order.Order + readonly annotate?: ((exclusiveMaximum: T) => Annotations) | undefined + readonly format?: (value: T) => string | undefined +}) => { + const lessThanOrEqualTo = Order.lessThanOrEqualTo(options.order) + const format = options.format ?? globalThis.String + return (maximum: T, annotations?: Annotations) => { + return make((input) => lessThanOrEqualTo(input, maximum), { + title: `lessThanOrEqualTo(${format(maximum)})`, + description: `a value less than or equal to ${format(maximum)}`, + ...options.annotate?.(maximum), + ...annotations + }) + } +} + +/** + * @category Order checks + * @since 4.0.0 + */ +export const deriveBetween = (options: { + readonly order: Order.Order + readonly annotate?: ((minimum: T, maximum: T) => Annotations) | undefined + readonly format?: (value: T) => string | undefined +}) => { + const greaterThanOrEqualTo = Order.greaterThanOrEqualTo(options.order) + const lessThanOrEqualTo = Order.lessThanOrEqualTo(options.order) + const format = options.format ?? globalThis.String + return (minimum: T, maximum: T, annotations?: Annotations) => { + return make((input) => greaterThanOrEqualTo(input, minimum) && lessThanOrEqualTo(input, maximum), { + title: `between(${format(minimum)}, ${format(maximum)})`, + description: `a value between ${format(minimum)} and ${format(maximum)}`, + ...options.annotate?.(minimum, maximum), + ...annotations + }) + } +} + +/** + * @category Numeric checks + * @since 4.0.0 + */ +export const deriveMultipleOf = (options: { + readonly remainder: (input: T, divisor: T) => T + readonly zero: NoInfer + readonly annotate?: ((divisor: T) => Annotations) | undefined + readonly format?: (value: T) => string | undefined +}) => +(divisor: T) => { + const format = options.format ?? globalThis.String + return make((input) => options.remainder(input, divisor) === options.zero, { + title: `multipleOf(${format(divisor)})`, + description: `a value that is a multiple of ${format(divisor)}`, + ...options.annotate?.(divisor) + }) +} + /** * @category Number checks * @since 4.0.0 */ -export const between = makeBetween(Order.number) +export const greaterThan = deriveGreaterThan({ + order: Order.number, + annotate: (exclusiveMinimum) => ({ + jsonSchema: { + type: "fragment", + fragment: { + exclusiveMinimum + } + }, + meta: { + id: "greaterThan", + exclusiveMinimum + } + }) +}) + +/** + * @category Number checks + * @since 4.0.0 + */ +export const greaterThanOrEqualTo = deriveGreaterThanOrEqualTo({ + order: Order.number, + annotate: (minimum) => ({ + jsonSchema: { + type: "fragment", + fragment: { + minimum + } + }, + meta: { + id: "greaterThanOrEqualTo", + minimum + } + }) +}) + +/** + * @category Number checks + * @since 4.0.0 + */ +export const lessThan = deriveLessThan({ + order: Order.number, + annotate: (exclusiveMaximum) => ({ + jsonSchema: { + type: "fragment", + fragment: { + exclusiveMaximum + } + }, + meta: { + id: "lessThan", + exclusiveMaximum + } + }) +}) + +/** + * @category Number checks + * @since 4.0.0 + */ +export const lessThanOrEqualTo = deriveLessThanOrEqualTo({ + order: Order.number, + annotate: (maximum) => ({ + jsonSchema: { + type: "fragment", + fragment: { + maximum + } + }, + meta: { + id: "lessThanOrEqualTo", + maximum + } + }) +}) + +/** + * @category Number checks + * @since 4.0.0 + */ +export const between = deriveBetween({ + order: Order.number, + annotate: (minimum, maximum) => ({ + jsonSchema: { + type: "fragment", + fragment: { + minimum, + maximum + } + }, + meta: { + id: "between", + minimum, + maximum + } + }) +}) + +/** + * @category Number checks + * @since 4.0.0 + */ +export const positive = greaterThan(0) + +/** + * @category Number checks + * @since 4.0.0 + */ +export const negative = lessThan(0) + +/** + * @category Number checks + * @since 4.0.0 + */ +export const nonNegative = greaterThanOrEqualTo(0) + +/** + * @category Number checks + * @since 4.0.0 + */ +export const nonPositive = lessThanOrEqualTo(0) + +/** + * @category Number checks + * @since 4.0.0 + */ +export const multipleOf = deriveMultipleOf({ + remainder: Num.remainder, + zero: 0, + annotate: (divisor) => ({ + title: `multipleOf(${divisor})`, + description: `a value that is a multiple of ${divisor}`, + jsonSchema: { + type: "fragment", + fragment: { + multipleOf: Math.abs(divisor) // JSON Schema only supports positive divisors + } + } + }) +}) /** * Restricts to safe integer range * - * @category Number checks + * @category Integer checks * @since 4.0.0 */ export const int = make((n: number) => Number.isSafeInteger(n), { @@ -340,7 +546,7 @@ export const int = make((n: number) => Number.isSafeInteger(n), { }) /** - * @category Number checks + * @category Integer checks * @since 4.0.0 */ export const int32 = new FilterGroup([ diff --git a/packages/effect/test/Schema.test.ts b/packages/effect/test/Schema.test.ts index 9bb52481e3..e44e2c7ce5 100644 --- a/packages/effect/test/Schema.test.ts +++ b/packages/effect/test/Schema.test.ts @@ -1,8 +1,10 @@ import { + BigInt, Context, Effect, Equal, Option, + Order, Result, Schema, SchemaAST, @@ -435,7 +437,7 @@ describe("Schema", () => { ) }) - it("Struct & filter", async () => { + it("Struct & check", async () => { const from = Schema.Struct({ a: Schema.String }) @@ -587,9 +589,9 @@ describe("Schema", () => { }) }) - describe("Filters", () => { + describe("Checks", () => { describe("check", () => { - it("single filter", async () => { + it("single check", async () => { const schema = Schema.String.pipe(Schema.check( SchemaCheck.minLength(3) )) @@ -604,7 +606,7 @@ describe("Schema", () => { ) }) - it("multiple filters", async () => { + it("multiple checks", async () => { const schema = Schema.String.pipe(Schema.check( SchemaCheck.minLength(3), SchemaCheck.includes("c") @@ -630,7 +632,7 @@ describe("Schema", () => { ) }) - it("aborting filters", async () => { + it("aborting checks", async () => { const schema = Schema.String.pipe(Schema.check( SchemaCheck.abort(SchemaCheck.minLength(2)), SchemaCheck.includes("b") @@ -647,7 +649,7 @@ describe("Schema", () => { }) describe("checkEncoded", () => { - it("single filter", async () => { + it("single check", async () => { const schema = FiniteFromString.pipe( Schema.checkEncoded(SchemaCheck.minLength(3)) ) @@ -664,7 +666,7 @@ describe("Schema", () => { ) }) - it("multiple filters", async () => { + it("multiple checks", async () => { const schema = FiniteFromString.pipe( Schema.checkEncoded(SchemaCheck.minLength(3), SchemaCheck.includes("1")) ) @@ -721,7 +723,7 @@ describe("Schema", () => { ) }) - describe("String filters", () => { + describe("String checks", () => { it("regex", async () => { const schema = Schema.String.pipe(Schema.check(SchemaCheck.regex(/^a/))) @@ -882,7 +884,7 @@ describe("Schema", () => { }) }) - describe("Number filters", () => { + describe("Number checks", () => { it("greaterThan", async () => { const schema = Schema.Number.pipe(Schema.check(SchemaCheck.greaterThan(1))) @@ -907,6 +909,66 @@ describe("Schema", () => { ) }) + it("greaterThanOrEqualTo", async () => { + const schema = Schema.Number.pipe(Schema.check(SchemaCheck.greaterThanOrEqualTo(1))) + + strictEqual(SchemaAST.format(schema.ast), `number & greaterThanOrEqualTo(1)`) + + await assertions.decoding.succeed(schema, 1) + await assertions.decoding.fail( + schema, + 0, + `number & greaterThanOrEqualTo(1) +└─ greaterThanOrEqualTo(1) + └─ Invalid data 0` + ) + }) + + it("lessThan", async () => { + const schema = Schema.Number.pipe(Schema.check(SchemaCheck.lessThan(1))) + + strictEqual(SchemaAST.format(schema.ast), `number & lessThan(1)`) + + await assertions.decoding.succeed(schema, 0) + await assertions.decoding.fail( + schema, + 1, + `number & lessThan(1) +└─ lessThan(1) + └─ Invalid data 1` + ) + }) + + it("lessThanOrEqualTo", async () => { + const schema = Schema.Number.pipe(Schema.check(SchemaCheck.lessThanOrEqualTo(1))) + + strictEqual(SchemaAST.format(schema.ast), `number & lessThanOrEqualTo(1)`) + + await assertions.decoding.succeed(schema, 1) + await assertions.decoding.fail( + schema, + 2, + `number & lessThanOrEqualTo(1) +└─ lessThanOrEqualTo(1) + └─ Invalid data 2` + ) + }) + + it("multipleOf", async () => { + const schema = Schema.Number.pipe(Schema.check(SchemaCheck.multipleOf(2))) + + strictEqual(SchemaAST.format(schema.ast), `number & multipleOf(2)`) + + await assertions.decoding.succeed(schema, 4) + await assertions.decoding.fail( + schema, + 3, + `number & multipleOf(2) +└─ multipleOf(2) + └─ Invalid data 3` + ) + }) + it("between", async () => { const schema = Schema.Number.pipe(Schema.check(SchemaCheck.between(1, 3))) @@ -1003,6 +1065,122 @@ describe("Schema", () => { └─ Invalid data 9007199254740992` ) }) + + describe("BigInt Checks", () => { + const options = { order: Order.bigint } + + const between = SchemaCheck.deriveBetween(options) + const greaterThan = SchemaCheck.deriveGreaterThan(options) + const greaterThanOrEqualTo = SchemaCheck.deriveGreaterThanOrEqualTo(options) + const lessThan = SchemaCheck.deriveLessThan(options) + const lessThanOrEqualTo = SchemaCheck.deriveLessThanOrEqualTo(options) + const multipleOf = SchemaCheck.deriveMultipleOf({ + remainder: BigInt.remainder, + zero: 0n + }) + + const positive = greaterThan(0n) + const nonNegative = greaterThanOrEqualTo(0n) + const negative = lessThan(0n) + const nonPositive = lessThanOrEqualTo(0n) + + it("between", async () => { + const schema = Schema.BigInt.pipe(Schema.check(between(5n, 10n))) + + strictEqual(SchemaAST.format(schema.ast), `bigint & between(5, 10)`) + + await assertions.decoding.succeed(schema, 5n) + await assertions.decoding.succeed(schema, 7n) + await assertions.decoding.succeed(schema, 10n) + await assertions.decoding.fail( + schema, + 4n, + `bigint & between(5, 10) +└─ between(5, 10) + └─ Invalid data 4n` + ) + }) + + it("greaterThan", async () => { + const schema = Schema.BigInt.pipe(Schema.check(greaterThan(5n))) + + strictEqual(SchemaAST.format(schema.ast), `bigint & greaterThan(5)`) + + await assertions.decoding.succeed(schema, 6n) + await assertions.decoding.fail( + schema, + 5n, + `bigint & greaterThan(5) +└─ greaterThan(5) + └─ Invalid data 5n` + ) + }) + + it("greaterThanOrEqualTo", async () => { + const schema = Schema.BigInt.pipe(Schema.check(greaterThanOrEqualTo(5n))) + + strictEqual(SchemaAST.format(schema.ast), `bigint & greaterThanOrEqualTo(5)`) + + await assertions.decoding.succeed(schema, 5n) + await assertions.decoding.succeed(schema, 6n) + await assertions.decoding.fail( + schema, + 4n, + `bigint & greaterThanOrEqualTo(5) +└─ greaterThanOrEqualTo(5) + └─ Invalid data 4n` + ) + }) + + it("lessThan", async () => { + const schema = Schema.BigInt.pipe(Schema.check(lessThan(5n))) + + strictEqual(SchemaAST.format(schema.ast), `bigint & lessThan(5)`) + + await assertions.decoding.succeed(schema, 4n) + await assertions.decoding.fail( + schema, + 5n, + `bigint & lessThan(5) +└─ lessThan(5) + └─ Invalid data 5n` + ) + }) + + it("lessThanOrEqualTo", async () => { + const schema = Schema.BigInt.pipe(Schema.check(lessThanOrEqualTo(5n))) + + strictEqual(SchemaAST.format(schema.ast), `bigint & lessThanOrEqualTo(5)`) + + await assertions.decoding.succeed(schema, 5n) + await assertions.decoding.succeed(schema, 4n) + await assertions.decoding.fail( + schema, + 6n, + `bigint & lessThanOrEqualTo(5) +└─ lessThanOrEqualTo(5) + └─ Invalid data 6n` + ) + }) + + it("multipleOf", async () => { + const schema = Schema.BigInt.pipe(Schema.check(multipleOf(5n))) + + strictEqual(SchemaAST.format(schema.ast), `bigint & multipleOf(5)`) + }) + + it("positive", async () => { + const schema = Schema.BigInt.pipe(Schema.check(positive)) + + strictEqual(SchemaAST.format(schema.ast), `bigint & greaterThan(0)`) + }) + + Schema.BigInt.pipe(Schema.check(positive)) + Schema.BigInt.pipe(Schema.check(nonNegative)) + Schema.BigInt.pipe(Schema.check(negative)) + Schema.BigInt.pipe(Schema.check(nonPositive)) + Schema.BigInt.pipe(Schema.check(multipleOf(5n))) + }) }) describe("Transformations", () => { @@ -1109,7 +1287,7 @@ describe("Schema", () => { strictEqual(schema.to, Schema.Finite) }) - it("transformation with filters", async () => { + it("transformation with checks", async () => { const schema = Schema.String.pipe( Schema.decodeTo( FiniteFromString, @@ -1222,7 +1400,7 @@ describe("Schema", () => { await assertions.encoding.succeed(schema, 2, { expected: "2" }) }) - it("double transformation with filters", async () => { + it("double transformation with checks", async () => { const schema = Schema.Struct({ a: Schema.String.pipe(Schema.check(SchemaCheck.minLength(2))).pipe( Schema.decodeTo( @@ -1380,7 +1558,7 @@ describe("Schema", () => { await assertions.encoding.succeed(schema, 2, { expected: "2" }) }) - it("double transformation with filters", async () => { + it("double transformation with checks", async () => { const schema = Schema.Struct({ a: Schema.String.pipe( Schema.encodeTo( @@ -1446,9 +1624,9 @@ describe("Schema", () => { ) }) - it("setConstructorDefault", () => { + it("withConstructorDefault", () => { const schema = Schema.Struct({ - a: FiniteFromString.pipe(Schema.setConstructorDefault(() => Result.succeedSome(-1))) + a: FiniteFromString.pipe(Schema.withConstructorDefault(() => Result.succeedSome(-1))) }) assertions.makeSync.succeed(schema, { a: 1 }) @@ -1561,10 +1739,10 @@ describe("Schema", () => { }) }) - describe("setConstructorDefault", () => { + describe("withConstructorDefault", () => { it("by default should not apply defaults when decoding / encoding", async () => { const schema = Schema.Struct({ - a: Schema.String.pipe(Schema.optionalKey, Schema.setConstructorDefault(() => Result.succeedSome("a"))) + a: Schema.String.pipe(Schema.optionalKey, Schema.withConstructorDefault(() => Result.succeedSome("a"))) }) await assertions.decoding.succeed(schema, {}) @@ -1573,27 +1751,42 @@ describe("Schema", () => { it("Struct & Some", () => { const schema = Schema.Struct({ - a: FiniteFromString.pipe(Schema.setConstructorDefault(() => Result.succeedSome(-1))) + a: FiniteFromString.pipe(Schema.withConstructorDefault(() => Result.succeedSome(-1))) }) assertions.makeSync.succeed(schema, { a: 1 }) assertions.makeSync.succeed(schema, {}, { a: -1 }) }) - it("nested defaults", () => { - const schema = Schema.Struct({ - a: Schema.Struct({ - b: FiniteFromString.pipe(Schema.setConstructorDefault(() => Result.succeedSome(-1))) - }).pipe(Schema.setConstructorDefault(() => Result.succeedSome({}))) + describe("nested defaults", () => { + it("Struct", () => { + const schema = Schema.Struct({ + a: Schema.Struct({ + b: FiniteFromString.pipe(Schema.withConstructorDefault(() => Result.succeedSome(-1))) + }).pipe(Schema.withConstructorDefault(() => Result.succeedSome({}))) + }) + + assertions.makeSync.succeed(schema, { a: { b: 1 } }) + assertions.makeSync.succeed(schema, { a: {} }, { a: { b: -1 } }) + assertions.makeSync.succeed(schema, {}, { a: { b: -1 } }) }) - assertions.makeSync.succeed(schema, { a: { b: 1 } }) - assertions.makeSync.succeed(schema, {}, { a: { b: -1 } }) + it("Class", () => { + class A extends Schema.Class("A")(Schema.Struct({ + a: Schema.Struct({ + b: FiniteFromString.pipe(Schema.withConstructorDefault(() => Result.succeedSome(-1))) + }).pipe(Schema.withConstructorDefault(() => Result.succeedSome({}))) + })) {} + + assertions.makeSync.succeed(A, { a: { b: 1 } }, new A({ a: { b: 1 } })) + assertions.makeSync.succeed(A, { a: {} }, new A({ a: { b: -1 } })) + assertions.makeSync.succeed(A, {}, new A({ a: { b: -1 } })) + }) }) it("Struct & Effect sync", () => { const schema = Schema.Struct({ - a: FiniteFromString.pipe(Schema.setConstructorDefault(() => Effect.succeed(Option.some(-1)))) + a: FiniteFromString.pipe(Schema.withConstructorDefault(() => Effect.succeed(Option.some(-1)))) }) assertions.makeSync.succeed(schema, { a: 1 }) @@ -1602,7 +1795,7 @@ describe("Schema", () => { it("Struct & Effect async", async () => { const schema = Schema.Struct({ - a: FiniteFromString.pipe(Schema.setConstructorDefault(() => + a: FiniteFromString.pipe(Schema.withConstructorDefault(() => Effect.gen(function*() { yield* Effect.sleep(100) return Option.some(-1) @@ -1618,7 +1811,7 @@ describe("Schema", () => { class Service extends Context.Tag }>()("Service") {} const schema = Schema.Struct({ - a: FiniteFromString.pipe(Schema.setConstructorDefault(() => + a: FiniteFromString.pipe(Schema.withConstructorDefault(() => Effect.gen(function*() { yield* Effect.sleep(100) const oservice = yield* Effect.serviceOption(Service) diff --git a/packages/effect/test/SchemaTest.ts b/packages/effect/test/SchemaTest.ts index 0a57e1e5b5..96cb6f2bb4 100644 --- a/packages/effect/test/SchemaTest.ts +++ b/packages/effect/test/SchemaTest.ts @@ -58,9 +58,7 @@ export const assertions = (asserts: { input: S["~type.make.in"], expected?: S["Type"] ) { - // Destructure to verify that "this" type is bound - const { makeSync } = schema - deepStrictEqual(makeSync(input), expected === undefined ? input : expected) + deepStrictEqual(schema.makeSync(input), expected === undefined ? input : expected) }, /** @@ -71,9 +69,7 @@ export const assertions = (asserts: { input: S["~type.make.in"], message: string ) { - // Destructure to verify that "this" type is bound - const { makeSync } = schema - throws(() => makeSync(input), (err) => { + throws(() => schema.makeSync(input), (err) => { assertInstanceOf(err, Error) strictEqual(err.message, message) })