| 1 | import { Effect, Schema, SchemaAST } from "effect" |
| 2 | |
| 3 | export const constructionEvents: Array<string> = [] |
| 4 | const value = Schema.Number.pipe(Schema.withConstructorDefault(Effect.sync(() => { |
| 5 | constructionEvents.push("default") |
| 6 | return 1 |
| 7 | }))) |
| 8 | export class Constructed extends Schema.TaggedClass<Constructed>()("Constructed", { |
| 9 | value |
| 10 | }) { |
| 11 | readonly initialized = constructionEvents.push("class") > 0 |
| 12 | } |
| 13 | |
| 14 | export const constructionCases = { |
| 15 | struct: { |
| 16 | schema: Schema.Struct({ value, optional: Schema.optionalKey(Schema.String) }) |
| 17 | .check(Schema.makeFilter((input) => { |
| 18 | constructionEvents.push("check") |
| 19 | return input.value > 0 |
| 20 | })), |
| 21 | inputs: [{}, { value: undefined }, { value: -1 }, { value: "bad", optional: 1 }, { value: 2, extra: true }] |
| 22 | }, |
| 23 | array: { schema: Schema.Array(value), inputs: [[undefined, 2], ["bad", 3], null] }, |
| 24 | tuple: { |
| 25 | schema: Schema.TupleWithRest(Schema.Tuple([value]), [Schema.String, Schema.Boolean]), |
| 26 | inputs: [[], [undefined, "a", true], ["bad", 1, false], [1, true]] |
| 27 | }, |
| 28 | optionalTuple: { |
| 29 | schema: Schema.Tuple([Schema.optionalKey(Schema.Undefined)]), |
| 30 | inputs: [[], [undefined], [1], [undefined, 2]] |
| 31 | }, |
| 32 | record: { schema: Schema.Record(Schema.String, value), inputs: [{ a: undefined }, { a: "bad", b: "bad" }, {}, null] }, |
| 33 | mixedRecord: { |
| 34 | schema: Schema.StructWithRest(Schema.Struct({ value }), [ |
| 35 | Schema.Record(Schema.TemplateLiteral(["x-", Schema.String]), Schema.Number) |
| 36 | ]), |
| 37 | inputs: [{}, { value: 1, "x-a": 2 }, { value: "bad", "x-a": "bad", extra: true }] |
| 38 | }, |
| 39 | union: { |
| 40 | schema: Schema.Union([ |
| 41 | Schema.Struct({ _tag: Schema.tag("A"), value }), |
| 42 | Schema.Struct({ _tag: Schema.tag("B"), text: Schema.String }) |
| 43 | ]), |
| 44 | inputs: [{}, { text: "a" }, { _tag: "A", value: "bad" }, { _tag: "C" }] |
| 45 | }, |
| 46 | oneOf: { schema: Schema.Union([Schema.String, Schema.Literal("a")], { mode: "oneOf" }), inputs: ["a", "b", 1] }, |
| 47 | class: { |
| 48 | schema: Constructed, |
| 49 | inputs: [{}, { value: undefined }, { value: -1 }, { value: "bad" }, { _tag: "wrong" }] |
| 50 | }, |
| 51 | transformed: { |
| 52 | schema: Schema.Struct({ value: Schema.FiniteFromString.pipe(Schema.withConstructorDefault(Effect.succeed(1))) }), |
| 53 | inputs: [{}, { value: "1" }, { value: 2 }] |
| 54 | }, |
| 55 | declaration: { schema: Schema.ReadonlySet(Schema.Number), inputs: [new Set([1]), new Set(["bad"]), null] }, |
| 56 | empty: { schema: Schema.Struct({}), inputs: [{}, 1, [], null] } |
| 57 | } satisfies Record<string, { readonly schema: Schema.Constraint; readonly inputs: ReadonlyArray<unknown> }> |
| 58 | |
| 59 | export const constructionSchemas = Object.fromEntries( |
| 60 | Object.entries(constructionCases).map(( |
| 61 | [name, test] |
| 62 | ) => [`construct-${name}`, Schema.make(SchemaAST.toType(test.schema.ast))]) |
| 63 | ) |
| 64 | |
| 65 | export const constructionOptions: ReadonlyArray<SchemaAST.ParseOptions | undefined> = [ |
| 66 | undefined, |
| 67 | { errors: "all", reportInput: true }, |
| 68 | { onExcessProperty: "error" }, |
| 69 | { disableChecks: true } |
| 70 | ] |