diff --git a/.changeset/dry-ducks-cheat.md b/.changeset/dry-ducks-cheat.md new file mode 100644 index 00000000000..c3f452c7bbc --- /dev/null +++ b/.changeset/dry-ducks-cheat.md @@ -0,0 +1,5 @@ +--- +"@effect/schema": minor +--- + +Schema: Arbitrary remove `from`/`to` API in favour of `make` diff --git a/packages/schema/src/Arbitrary.ts b/packages/schema/src/Arbitrary.ts index 1624ffa960c..1c705cdb866 100644 --- a/packages/schema/src/Arbitrary.ts +++ b/packages/schema/src/Arbitrary.ts @@ -39,20 +39,14 @@ export const unsafe = ( ): Arbitrary => go(schema.ast, {}) /** + * Returns a fast-check Arbitrary for the `A` type of the provided schema. + * * @category arbitrary * @since 1.0.0 */ -export const to = ( +export const make = ( schema: Schema.Schema -): Arbitrary => go(AST.to(schema.ast), {}) - -/** - * @category arbitrary - * @since 1.0.0 - */ -export const from = ( - schema: Schema.Schema -): Arbitrary => go(AST.from(schema.ast), {}) +): Arbitrary => go(schema.ast, {}) const depthSize = 1 @@ -99,12 +93,9 @@ const go = (ast: AST.AST, options: Options): Arbitrary => { return hook.value(...ast.typeParameters.map((p) => go(p, options))) case "Refinement": return hook.value(getRefinementFromArbitrary(ast, options)) - case "Suspend": + default: return hook.value() } - throw new Error( - "Arbitrary annotations are only managed in the following three scenarios: Declarations, Refinements, Suspensions" - ) } switch (ast._tag) { case "Declaration": { @@ -293,7 +284,7 @@ const go = (ast: AST.AST, options: Options): Arbitrary => { return (fc) => fc.constant(null).chain(() => get()(fc)) } case "Transform": - throw new Error("cannot build an Arbitrary for transformations") + return go(ast.to, options) } } diff --git a/packages/schema/src/Schema.ts b/packages/schema/src/Schema.ts index 46089ad1669..a7ae7767ac1 100644 --- a/packages/schema/src/Schema.ts +++ b/packages/schema/src/Schema.ts @@ -3287,8 +3287,7 @@ export { const makeEncodingTransformation = ( id: string, decode: (s: string) => Either.Either, - encode: (u: Uint8Array) => string, - arbitrary: Arbitrary + encode: (u: Uint8Array) => string ): Schema => transformOrFail( string, @@ -3300,11 +3299,7 @@ const makeEncodingTransformation = ( ), (u) => ParseResult.succeed(encode(u)), { strict: false } - ).pipe(annotations({ - [AST.IdentifierAnnotationId]: id, - [hooks.PrettyHookId]: (): Pretty.Pretty => (u) => `${id}(${encode(u)})`, - [hooks.ArbitraryHookId]: () => arbitrary - })) + ).pipe(identifier(id)) /** * @category Encoding transformations @@ -3313,8 +3308,7 @@ const makeEncodingTransformation = ( export const Base64: Schema = makeEncodingTransformation( "Base64", Encoding.decodeBase64, - Encoding.encodeBase64, - (fc) => fc.base64String().map((s) => Either.getOrThrow(Encoding.decodeBase64(s))) + Encoding.encodeBase64 ) /** @@ -3324,8 +3318,7 @@ export const Base64: Schema = makeEncodingTransformation( export const Base64Url: Schema = makeEncodingTransformation( "Base64Url", Encoding.decodeBase64Url, - Encoding.encodeBase64Url, - (fc) => fc.base64String().map((s) => Either.getOrThrow(Encoding.decodeBase64Url(s))) + Encoding.encodeBase64Url ) /** @@ -3335,8 +3328,7 @@ export const Base64Url: Schema = makeEncodingTransformation( export const Hex: Schema = makeEncodingTransformation( "Hex", Encoding.decodeHex, - Encoding.encodeHex, - (fc) => fc.hexaString().map((s) => Either.getOrThrow(Encoding.decodeHex(s))) + Encoding.encodeHex ) /** diff --git a/packages/schema/test/Arbitrary/Arbitrary.test.ts b/packages/schema/test/Arbitrary/Arbitrary.test.ts index 1731b704734..7724e0841fe 100644 --- a/packages/schema/test/Arbitrary/Arbitrary.test.ts +++ b/packages/schema/test/Arbitrary/Arbitrary.test.ts @@ -1,7 +1,7 @@ import * as Arbitrary from "@effect/schema/Arbitrary" import * as ParseResult from "@effect/schema/ParseResult" import * as S from "@effect/schema/Schema" -import { propertyFrom, propertyTo } from "@effect/schema/test/util" +import { expectValidArbitrary } from "@effect/schema/test/util" import * as fc from "fast-check" import { describe, expect, it } from "vitest" @@ -17,142 +17,121 @@ describe("Arbitrary > Arbitrary", () => { ) }) - it("should throw on transformations", () => { + it("make(S.to(schema))", () => { const schema = S.NumberFromString - expect(() => Arbitrary.unsafe(schema)).toThrow( - new Error("cannot build an Arbitrary for transformations") - ) - }) - - it("should throw on wrong annotations", () => { - expect(() => { - const schema = S.string.pipe( - S.annotations({ - [Arbitrary.ArbitraryHookId]: (): Arbitrary.Arbitrary => (fc) => fc.string() - }) - ) - Arbitrary.to(schema) - }).toThrow( - new Error( - "Arbitrary annotations are only managed in the following three scenarios: Declarations, Refinements, Suspensions" - ) - ) - }) - - it("to", () => { - const schema = S.NumberFromString - propertyTo(schema) + expectValidArbitrary(S.to(schema)) }) - it("from", () => { - const NumberFromString = S.NumberFromString + it("make(S.from(schema))", () => { const schema = S.struct({ - a: NumberFromString, - b: S.tuple(NumberFromString), - c: S.union(NumberFromString, S.boolean), - d: NumberFromString.pipe(S.positive()), - e: S.optionFromSelf(NumberFromString) + a: S.NumberFromString, + b: S.tuple(S.NumberFromString), + c: S.union(S.NumberFromString, S.boolean), + d: S.NumberFromString.pipe(S.positive()), + e: S.optionFromSelf(S.NumberFromString) }) - propertyFrom(schema) + expectValidArbitrary(S.from(schema)) }) - it("templateLiteral. a", () => { - const schema = S.templateLiteral(S.literal("a")) - propertyTo(schema) - }) + describe("templateLiteral", () => { + it("a", () => { + const schema = S.templateLiteral(S.literal("a")) + expectValidArbitrary(schema) + }) - it("templateLiteral. a b", () => { - const schema = S.templateLiteral(S.literal("a"), S.literal(" "), S.literal("b")) - propertyTo(schema) - }) + it("a b", () => { + const schema = S.templateLiteral(S.literal("a"), S.literal(" "), S.literal("b")) + expectValidArbitrary(schema) + }) - it("templateLiteral. a${string}", () => { - const schema = S.templateLiteral(S.literal("a"), S.string) - propertyTo(schema) - }) + it("a${string}", () => { + const schema = S.templateLiteral(S.literal("a"), S.string) + expectValidArbitrary(schema) + }) - it("templateLiteral. a${number}", () => { - const schema = S.templateLiteral(S.literal("a"), S.number) - propertyTo(schema) - }) + it("a${number}", () => { + const schema = S.templateLiteral(S.literal("a"), S.number) + expectValidArbitrary(schema) + }) - it("templateLiteral. a", () => { - const schema = S.templateLiteral(S.literal("a")) - propertyTo(schema) - }) + it("a", () => { + const schema = S.templateLiteral(S.literal("a")) + expectValidArbitrary(schema) + }) - it("templateLiteral. ${string}", () => { - const schema = S.templateLiteral(S.string) - propertyTo(schema) - }) + it("${string}", () => { + const schema = S.templateLiteral(S.string) + expectValidArbitrary(schema) + }) - it("templateLiteral. a${string}b", () => { - const schema = S.templateLiteral(S.literal("a"), S.string, S.literal("b")) - propertyTo(schema) + it("a${string}b", () => { + const schema = S.templateLiteral(S.literal("a"), S.string, S.literal("b")) + expectValidArbitrary(schema) + }) }) it("never", () => { - expect(() => Arbitrary.to(S.never)(fc)).toThrow( + expect(() => Arbitrary.make(S.never)(fc)).toThrow( new Error("cannot build an Arbitrary for `never`") ) }) it("string", () => { - propertyTo(S.string) + expectValidArbitrary(S.string) }) it("void", () => { - propertyTo(S.void) + expectValidArbitrary(S.void) }) it("number", () => { - propertyTo(S.number) + expectValidArbitrary(S.number) }) it("boolean", () => { - propertyTo(S.boolean) + expectValidArbitrary(S.boolean) }) it("bigint", () => { - propertyTo(S.bigintFromSelf) + expectValidArbitrary(S.bigintFromSelf) }) it("symbol", () => { - propertyTo(S.symbolFromSelf) + expectValidArbitrary(S.symbolFromSelf) }) it("object", () => { - propertyTo(S.object) + expectValidArbitrary(S.object) }) it("any", () => { - propertyTo(S.any) + expectValidArbitrary(S.any) }) it("unknown", () => { - propertyTo(S.unknown) + expectValidArbitrary(S.unknown) }) it("literal 1 member", () => { const schema = S.literal(1) - propertyTo(schema) + expectValidArbitrary(schema) }) it("literal 2 members", () => { const schema = S.literal(1, "a") - propertyTo(schema) + expectValidArbitrary(schema) }) it("uniqueSymbol", () => { const a = Symbol.for("@effect/schema/test/a") const schema = S.uniqueSymbol(a) - propertyTo(schema) + expectValidArbitrary(schema) }) it("empty enums should throw", () => { enum Fruits {} const schema = S.enums(Fruits) - expect(() => Arbitrary.to(schema)(fc)).toThrow( + expect(() => Arbitrary.make(schema)(fc)).toThrow( new Error("cannot build an Arbitrary for an empty enum") ) }) @@ -163,7 +142,7 @@ describe("Arbitrary > Arbitrary", () => { Banana } const schema = S.enums(Fruits) - propertyTo(schema) + expectValidArbitrary(schema) }) it("String enums", () => { @@ -173,7 +152,7 @@ describe("Arbitrary > Arbitrary", () => { Cantaloupe = 0 } const schema = S.enums(Fruits) - propertyTo(schema) + expectValidArbitrary(schema) }) it("Const enums", () => { @@ -183,62 +162,62 @@ describe("Arbitrary > Arbitrary", () => { Cantaloupe: 3 } as const const schema = S.enums(Fruits) - propertyTo(schema) + expectValidArbitrary(schema) }) it("tuple. empty", () => { const schema = S.tuple() - propertyTo(schema) + expectValidArbitrary(schema) }) it("tuple. required element", () => { const schema = S.tuple(S.number) - propertyTo(schema) + expectValidArbitrary(schema) }) it("tuple. required element with undefined", () => { const schema = S.tuple(S.union(S.number, S.undefined)) - propertyTo(schema) + expectValidArbitrary(schema) }) it("tuple. optional element", () => { const schema = S.tuple().pipe(S.optionalElement(S.number)) - propertyTo(schema) + expectValidArbitrary(schema) }) it("tuple. optional element with undefined", () => { const schema = S.tuple().pipe(S.optionalElement(S.union(S.number, S.undefined))) - propertyTo(schema) + expectValidArbitrary(schema) }) it("tuple. e + e?", () => { const schema = S.tuple(S.string).pipe(S.optionalElement(S.number)) - propertyTo(schema) + expectValidArbitrary(schema) }) it("tuple. e + r", () => { const schema = S.tuple(S.string).pipe(S.rest(S.number)) - propertyTo(schema) + expectValidArbitrary(schema) }) it("tuple. e? + r", () => { const schema = S.tuple().pipe(S.optionalElement(S.string), S.rest(S.number)) - propertyTo(schema) + expectValidArbitrary(schema) }) it("tuple. r", () => { const schema = S.array(S.number) - propertyTo(schema) + expectValidArbitrary(schema) }) it("tuple. r + e", () => { const schema = S.array(S.string).pipe(S.element(S.number)) - propertyTo(schema) + expectValidArbitrary(schema) }) it("tuple. e + r + e", () => { const schema = S.tuple(S.string).pipe(S.rest(S.number), S.element(S.boolean)) - propertyTo(schema) + expectValidArbitrary(schema) }) describe("suspend", () => { @@ -263,7 +242,7 @@ describe("Arbitrary > Arbitrary", () => { [Arbitrary.ArbitraryHookId]: () => () => arb })) }) - propertyTo(schema) + expectValidArbitrary(schema) }) it("struct", () => { @@ -275,10 +254,10 @@ describe("Arbitrary > Arbitrary", () => { a: S.string, as: S.array(S.suspend(() => schema)) }) - propertyTo(schema) + expectValidArbitrary(schema) }) - it("from", () => { + it("make(S.from(schema))", () => { const NumberFromString = S.NumberFromString interface I { readonly a: string | I @@ -290,7 +269,7 @@ describe("Arbitrary > Arbitrary", () => { a: S.union(NumberFromString, S.suspend(() => schema)) }) - propertyFrom(schema) + expectValidArbitrary(S.from(schema)) }) it("tuple", () => { @@ -299,7 +278,7 @@ describe("Arbitrary > Arbitrary", () => { S.number, S.union(S.literal(null), S.suspend(() => schema)) ) - propertyTo(schema) + expectValidArbitrary(schema) }) it("record", () => { @@ -307,7 +286,7 @@ describe("Arbitrary > Arbitrary", () => { [_: string]: A } const schema: S.Schema = S.record(S.string, S.suspend(() => schema)) - propertyTo(schema) + expectValidArbitrary(schema) }) it("should support mutually suspended schemas", () => { @@ -334,186 +313,190 @@ describe("Arbitrary > Arbitrary", () => { left: Expression, right: Expression }) - propertyTo(Operation, { numRuns: 5 }) + expectValidArbitrary(Operation, { numRuns: 5 }) }) }) describe("struct", () => { it("required property signature", () => { const schema = S.struct({ a: S.number }) - propertyTo(schema) + expectValidArbitrary(schema) }) it("required property signature with undefined", () => { const schema = S.struct({ a: S.union(S.number, S.undefined) }) - propertyTo(schema) + expectValidArbitrary(schema) }) it("optional property signature", () => { const schema = S.struct({ a: S.optional(S.number, { exact: true }) }) - propertyTo(schema) + expectValidArbitrary(schema) }) it("optional property signature with undefined", () => { const schema = S.struct({ a: S.optional(S.union(S.number, S.undefined), { exact: true }) }) - propertyTo(schema) + expectValidArbitrary(schema) }) it("baseline", () => { const schema = S.struct({ a: S.string, b: S.number }) - propertyTo(schema) + expectValidArbitrary(schema) }) }) it("union", () => { const schema = S.union(S.string, S.number) - propertyTo(schema) + expectValidArbitrary(schema) }) it("record(string, string)", () => { const schema = S.record(S.string, S.string) - propertyTo(schema) + expectValidArbitrary(schema) }) it("record(symbol, string)", () => { const schema = S.record(S.symbolFromSelf, S.string) - propertyTo(schema) + expectValidArbitrary(schema) }) - it("minItems", () => { - const schema = S.array(S.string).pipe(S.minItems(2)) - propertyTo(schema) - }) + describe("array filters", () => { + it("minItems", () => { + const schema = S.array(S.string).pipe(S.minItems(2)) + expectValidArbitrary(schema) + }) - it("maxItems", () => { - const schema = S.array(S.string).pipe(S.maxItems(5)) - propertyTo(schema) - }) + it("maxItems", () => { + const schema = S.array(S.string).pipe(S.maxItems(5)) + expectValidArbitrary(schema) + }) - it("itemsCount", () => { - const schema = S.array(S.string).pipe(S.itemsCount(3)) - propertyTo(schema) + it("itemsCount", () => { + const schema = S.array(S.string).pipe(S.itemsCount(3)) + expectValidArbitrary(schema) + }) }) - it("minLength", () => { - const schema = S.string.pipe(S.minLength(1)) - propertyTo(schema) + it("extend/ struct + record", () => { + const schema = S.struct({ a: S.string }).pipe( + S.extend(S.record(S.string, S.union(S.string, S.number))) + ) + expectValidArbitrary(schema) }) - it("maxLength", () => { - const schema = S.string.pipe(S.maxLength(2)) - propertyTo(schema) - }) + describe("string filters", () => { + it("minLength", () => { + const schema = S.string.pipe(S.minLength(1)) + expectValidArbitrary(schema) + }) - it("length", () => { - const schema = S.string.pipe(S.length(10)) - propertyTo(schema) - }) + it("maxLength", () => { + const schema = S.string.pipe(S.maxLength(2)) + expectValidArbitrary(schema) + }) - it("startsWith", () => { - const schema = S.string.pipe(S.startsWith("a")) - propertyTo(schema) - }) + it("length", () => { + const schema = S.string.pipe(S.length(10)) + expectValidArbitrary(schema) + }) - it("endsWith", () => { - const schema = S.string.pipe(S.endsWith("a")) - propertyTo(schema) - }) + it("startsWith", () => { + const schema = S.string.pipe(S.startsWith("a")) + expectValidArbitrary(schema) + }) - it("int", () => { - const schema = S.number.pipe(S.int()) - propertyTo(schema) - }) + it("endsWith", () => { + const schema = S.string.pipe(S.endsWith("a")) + expectValidArbitrary(schema) + }) - it("extend/ struct + record", () => { - const schema = S.struct({ a: S.string }).pipe( - S.extend(S.record(S.string, S.union(S.string, S.number))) - ) - propertyTo(schema) + it("pattern", () => { + const regexp = /^[A-Z]{3}[0-9]{3}$/ + const schema = S.string.pipe(S.pattern(regexp)) + expectValidArbitrary(schema) + }) }) - it("between + int", () => { - const schema = S.number.pipe(S.between(1, 10), S.int()) - propertyTo(schema) - }) + describe("number filters", () => { + it("int", () => { + const schema = S.number.pipe(S.int()) + expectValidArbitrary(schema) + }) - it("int + between", () => { - const schema = S.number.pipe(S.int(), S.between(1, 10)) - propertyTo(schema) - }) + it("between + int", () => { + const schema = S.number.pipe(S.between(1, 10), S.int()) + expectValidArbitrary(schema) + }) - it("pattern: should be set by default", () => { - const regexp = /^[A-Z]{3}[0-9]{3}$/ - const schema = S.string.pipe(S.pattern(regexp)) - propertyTo(schema) - }) + it("int + between", () => { + const schema = S.number.pipe(S.int(), S.between(1, 10)) + expectValidArbitrary(schema) + }) - describe("number", () => { it("lessThanOrEqualTo", () => { const schema = S.number.pipe(S.lessThanOrEqualTo(1)) - propertyTo(schema) + expectValidArbitrary(schema) }) it("greaterThanOrEqualTo", () => { const schema = S.number.pipe(S.greaterThanOrEqualTo(1)) - propertyTo(schema) + expectValidArbitrary(schema) }) it("lessThan", () => { const schema = S.number.pipe(S.lessThan(1)) - propertyTo(schema) + expectValidArbitrary(schema) }) it("greaterThan", () => { const schema = S.number.pipe(S.greaterThan(1)) - propertyTo(schema) + expectValidArbitrary(schema) }) it("between", () => { const schema = S.number.pipe(S.between(1, 10)) - propertyTo(schema) + expectValidArbitrary(schema) }) it("nonNaN", () => { const schema = S.number.pipe(S.nonNaN()) - propertyTo(schema) + expectValidArbitrary(schema) }) it("finite", () => { const schema = S.number.pipe(S.finite()) - propertyTo(schema) + expectValidArbitrary(schema) }) it("NumberConstraints should support doubles as constraints", () => { const schema = S.number.pipe(S.clamp(1.3, 3.1)) - propertyTo(schema) + expectValidArbitrary(schema) }) }) - describe("bigint", () => { + describe("bigint filters", () => { it("lessThanOrEqualTo", () => { const schema = S.bigint.pipe(S.lessThanOrEqualToBigint(BigInt(1))) - propertyTo(schema) + expectValidArbitrary(schema) }) it("greaterThanOrEqualTo", () => { const schema = S.bigint.pipe(S.greaterThanOrEqualToBigint(BigInt(1))) - propertyTo(schema) + expectValidArbitrary(schema) }) it("lessThan", () => { const schema = S.bigint.pipe(S.lessThanBigint(BigInt(1))) - propertyTo(schema) + expectValidArbitrary(schema) }) it("greaterThan", () => { const schema = S.bigint.pipe(S.greaterThanBigint(BigInt(1))) - propertyTo(schema) + expectValidArbitrary(schema) }) it("between", () => { const schema = S.bigint.pipe(S.betweenBigint(BigInt(1), BigInt(10))) - propertyTo(schema) + expectValidArbitrary(schema) }) }) }) diff --git a/packages/schema/test/Arbitrary/Class.test.ts b/packages/schema/test/Arbitrary/Class.test.ts index c035d494f69..9520e383817 100644 --- a/packages/schema/test/Arbitrary/Class.test.ts +++ b/packages/schema/test/Arbitrary/Class.test.ts @@ -1,5 +1,5 @@ import * as S from "@effect/schema/Schema" -import { propertyTo } from "@effect/schema/test/util" +import { expectValidArbitrary } from "@effect/schema/test/util" import { describe, it } from "vitest" describe("class", () => { @@ -7,28 +7,28 @@ describe("class", () => { class Class extends S.Class()({ a: S.number }) {} - propertyTo(Class) + expectValidArbitrary(Class) }) it("required property signature with undefined", () => { class Class extends S.Class()({ a: S.union(S.number, S.undefined) }) {} - propertyTo(Class) + expectValidArbitrary(Class) }) it("optional property signature", () => { class Class extends S.Class()({ a: S.optional(S.number, { exact: true }) }) {} - propertyTo(Class) + expectValidArbitrary(Class) }) it("optional property signature with undefined", () => { class Class extends S.Class()({ a: S.optional(S.union(S.number, S.undefined), { exact: true }) }) {} - propertyTo(Class) + expectValidArbitrary(Class) }) it("baseline", () => { @@ -36,6 +36,6 @@ describe("class", () => { a: S.string, b: S.NumberFromString }) {} - propertyTo(Class) + expectValidArbitrary(Class) }) }) diff --git a/packages/schema/test/Encoding/Base64.test.ts b/packages/schema/test/Encoding/Base64.test.ts index dbd3713dfdb..3c6838236c8 100644 --- a/packages/schema/test/Encoding/Base64.test.ts +++ b/packages/schema/test/Encoding/Base64.test.ts @@ -2,7 +2,7 @@ import * as S from "@effect/schema/Schema" import * as Util from "@effect/schema/test/util" import { describe, it } from "vitest" -describe("Encoding/Base64", () => { +describe("Encoding > Base64", () => { const schema = S.Base64 const encoder = new TextEncoder() diff --git a/packages/schema/test/Encoding/Base64Url.test.ts b/packages/schema/test/Encoding/Base64Url.test.ts index 0770e8c8ad0..a0069f439f3 100644 --- a/packages/schema/test/Encoding/Base64Url.test.ts +++ b/packages/schema/test/Encoding/Base64Url.test.ts @@ -2,7 +2,7 @@ import * as S from "@effect/schema/Schema" import * as Util from "@effect/schema/test/util" import { describe, it } from "vitest" -describe("Encoding/Base64Url", () => { +describe("Encoding > Base64Url", () => { const schema = S.Base64Url const encoder = new TextEncoder() diff --git a/packages/schema/test/Encoding/Hex.test.ts b/packages/schema/test/Encoding/Hex.test.ts index 6b147e914d5..ade20af0d51 100644 --- a/packages/schema/test/Encoding/Hex.test.ts +++ b/packages/schema/test/Encoding/Hex.test.ts @@ -2,7 +2,7 @@ import * as S from "@effect/schema/Schema" import * as Util from "@effect/schema/test/util" import { describe, it } from "vitest" -describe("Encoding/Hex", () => { +describe("Encoding > Hex", () => { const schema = S.Hex const encoder = new TextEncoder() diff --git a/packages/schema/test/Equivalence.test.ts b/packages/schema/test/Equivalence.test.ts index 0af97af5e8c..0fbf0fb952e 100644 --- a/packages/schema/test/Equivalence.test.ts +++ b/packages/schema/test/Equivalence.test.ts @@ -17,7 +17,7 @@ export const propertyTo = ( schema: S.Schema, params?: fc.Parameters<[A, ...Array]> ) => { - const arb = A.to(schema)(fc) + const arb = A.make(schema)(fc) // console.log(fc.sample(arb, 10)) const equivalence = E.to(schema) diff --git a/packages/schema/test/JSONSchema.test.ts b/packages/schema/test/JSONSchema.test.ts index 0127c53a8ec..c519e38538d 100644 --- a/packages/schema/test/JSONSchema.test.ts +++ b/packages/schema/test/JSONSchema.test.ts @@ -25,7 +25,7 @@ type Json = const propertyTo = (schema: Schema.Schema, options?: { params?: fc.Parameters<[A]> }) => { - const arbitrary = A.to(schema) + const arbitrary = A.make(schema) const is = Schema.is(schema) const jsonSchema = JSONSchema.to(schema) // console.log(JSON.stringify(jsonSchema, null, 2)) @@ -51,7 +51,7 @@ const propertyTo = (schema: Schema.Schema, options?: { } const propertyFrom = (schema: Schema.Schema) => { - const arbitrary = A.from(schema) + const arbitrary = A.make(Schema.from(schema)) const is = Schema.is(Schema.from(schema)) const validate = new Ajv({ strictTuples: false, allowUnionTypes: true }).compile( JSONSchema.from(schema) diff --git a/packages/schema/test/util.ts b/packages/schema/test/util.ts index 364ec4a665b..5f8826fc174 100644 --- a/packages/schema/test/util.ts +++ b/packages/schema/test/util.ts @@ -81,7 +81,7 @@ export const roundtrip = (schema: S.Schema) => { if (!doRoundtrip) { return } - const arb = A.to(schema) + const arb = A.make(schema) const is = S.is(schema) const encode = S.encode(schema) const decode = S.decode(schema) @@ -174,28 +174,15 @@ export const X3 = S.transform( const doProperty = true -export const propertyTo = (schema: S.Schema, params?: fc.Parameters<[A]>) => { +export const expectValidArbitrary = (schema: S.Schema, params?: fc.Parameters<[A]>) => { if (!doProperty) { return } - const arbitrary = A.to(schema) - const arb = arbitrary(fc) - // console.log(JSON.stringify(fc.sample(arb, 10), null, 2)) + const arb = A.make(schema)(fc) const is = S.is(schema) fc.assert(fc.property(arb, (a) => is(a)), params) } -export const propertyFrom = (schema: S.Schema) => { - if (!doProperty) { - return - } - const arbitrary = A.from(schema) - const arb = arbitrary(fc) - // console.log(JSON.stringify(fc.sample(arb, 10), null, 2)) - const is = S.is(S.from(schema)) - fc.assert(fc.property(arb, (a) => is(a))) -} - export const isBun = "Bun" in globalThis export const expectPromiseSuccess = async (promise: Promise, a: A) => { @@ -221,7 +208,7 @@ export const expectPromiseFailure = async (promise: Promise, message: stri } export const sample = (schema: S.Schema, n: number) => { - const arbitrary = A.to(schema) + const arbitrary = A.make(schema) const arb = arbitrary(fc) console.log(JSON.stringify(fc.sample(arb, n), null, 2)) }