mirror of
https://github.com/Effect-TS/effect.git
synced 2026-12-02 03:15:52 +08:00
Refactor numeric filters (#119)
* fix Class constructor defaults * refactor order checks
This commit is contained in:
+84
-13
@@ -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<T>`
|
||||
export const makeGreaterThan = <T>(O: Order.Order<T>) => {
|
||||
const greaterThan = Order.greaterThan(O)
|
||||
return (exclusiveMinimum: T) =>
|
||||
SchemaCheck.make<T>((input) => greaterThan(input, exclusiveMinimum), {
|
||||
title: `greaterThan(${exclusiveMinimum})`,
|
||||
description: `a value greater than ${exclusiveMinimum}`
|
||||
})
|
||||
export const deriveGreaterThan = <T>(options: {
|
||||
readonly order: Order.Order<T>
|
||||
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<T>(
|
||||
(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
|
||||
|
||||
@@ -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<Context.Tag<"Tag", "-">>()
|
||||
|
||||
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")
|
||||
|
||||
@@ -638,3 +638,11 @@ export const fromNumber = (n: number): Option.Option<bigint> => {
|
||||
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)
|
||||
|
||||
@@ -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<Tag extends SchemaAST.LiteralValue> extends setConstructorD
|
||||
*/
|
||||
export function tag<Tag extends SchemaAST.LiteralValue>(literal: Tag): tag<Tag> {
|
||||
return Literal(literal).pipe(
|
||||
setConstructorDefault(() => Result.succeedSome(literal))
|
||||
withConstructorDefault(() => Result.succeedSome(literal))
|
||||
)
|
||||
}
|
||||
|
||||
@@ -1931,7 +1928,7 @@ export interface setConstructorDefault<S extends Top> extends make<S> {
|
||||
/**
|
||||
* @since 4.0.0
|
||||
*/
|
||||
export const setConstructorDefault = <S extends Top & { readonly "~type.default": "no-constructor-default" }>(
|
||||
export const withConstructorDefault = <S extends Top & { readonly "~type.default": "no-constructor-default" }>(
|
||||
parser: (
|
||||
input: O.Option<unknown>,
|
||||
ast: SchemaAST.AST,
|
||||
@@ -2266,10 +2263,7 @@ function makeClass<
|
||||
|
||||
return class extends Inherited {
|
||||
constructor(...[input, options]: ReadonlyArray<any>) {
|
||||
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"
|
||||
|
||||
@@ -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 = <T>(O: Order.Order<T>) => {
|
||||
const greaterThan = Order.greaterThan(O)
|
||||
export const deriveGreaterThan = <T>(options: {
|
||||
readonly order: Order.Order<T>
|
||||
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<T>((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 = <T>(O: Order.Order<T>) => {
|
||||
const greaterThan = Order.greaterThan(O)
|
||||
const lessThan = Order.lessThan(O)
|
||||
return (minimum: T, maximum: T, annotations?: Annotations) => {
|
||||
return make<T>((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 = <T>(options: {
|
||||
readonly order: Order.Order<T>
|
||||
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<T>((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 = <T>(options: {
|
||||
readonly order: Order.Order<T>
|
||||
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<T>((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 = <T>(options: {
|
||||
readonly order: Order.Order<T>
|
||||
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<T>((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 = <T>(options: {
|
||||
readonly order: Order.Order<T>
|
||||
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<T>((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 = <T>(options: {
|
||||
readonly remainder: (input: T, divisor: T) => T
|
||||
readonly zero: NoInfer<T>
|
||||
readonly annotate?: ((divisor: T) => Annotations) | undefined
|
||||
readonly format?: (value: T) => string | undefined
|
||||
}) =>
|
||||
(divisor: T) => {
|
||||
const format = options.format ?? globalThis.String
|
||||
return make<T>((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([
|
||||
|
||||
@@ -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>("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, { value: Effect.Effect<number> }>()("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)
|
||||
|
||||
@@ -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)
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user