diff --git a/packages/effect/SCHEMA.md b/packages/effect/SCHEMA.md index b7b871579b..bf77ffe4aa 100644 --- a/packages/effect/SCHEMA.md +++ b/packages/effect/SCHEMA.md @@ -79,27 +79,28 @@ The base interface is `Bottom`, which sits at the bottom of the schema type hier ```ts export interface Bottom< - T, - E, - RD, - RE, - Ast extends AST.AST, - RebuildOut extends Top, - AnnotateIn extends Annotations.Annotations = Annotations.Bottom, - TypeMakeIn = T, - Iso = T, - TypeMake = TypeMakeIn, - TypeMutability extends Mutability = "readonly", - TypeOptionality extends Optionality = "required", - TypeConstructorDefault extends ConstructorDefault = "no-default", - EncodedMutability extends Mutability = "readonly", - EncodedOptionality extends Optionality = "required" + out T, + out E, + out RD, + out RE, + out Ast extends AST.AST, + out RebuildOut extends Top, + out TypeMakeIn = T, + out Iso = T, + in out TypeParameters extends ReadonlyArray = readonly [], + out TypeMake = TypeMakeIn, + out TypeMutability extends Mutability = "readonly", + out TypeOptionality extends Optionality = "required", + out TypeConstructorDefault extends ConstructorDefault = "no-default", + out EncodedMutability extends Mutability = "readonly", + out EncodedOptionality extends Optionality = "required" > extends Pipeable.Pipeable { readonly [TypeId]: typeof TypeId readonly ast: Ast readonly "~rebuild.out": RebuildOut - readonly "~annotate.in": AnnotateIn + readonly "~type.parameters": TypeParameters + readonly "~annotate.in": Annotations.Bottom readonly Type: T readonly Encoded: E @@ -135,9 +136,9 @@ export interface Bottom< - `RE`: the type of the services required for encoding - `Ast`: the AST node type - `RebuildOut`: the type returned when modifying the schema (namely when you add annotations or checks) -- `AnnotateIn`: the type of accepted annotations - `TypeMakeIn`: the type of the input to the `makeUnsafe` constructor - `Iso`: the type of the focus of the default `Optic.Iso` +- `TypeParameters`: the type of the type parameters Contextual information about the schema (when the schema is used in a composite schema such as a struct or a tuple): diff --git a/packages/effect/dtslint/schema/Annotations.tst.ts b/packages/effect/dtslint/schema/Annotations.tst.ts index 62a010a6cd..ba2c5f11ac 100644 --- a/packages/effect/dtslint/schema/Annotations.tst.ts +++ b/packages/effect/dtslint/schema/Annotations.tst.ts @@ -1,14 +1,26 @@ +import type { Option } from "effect/data" import { Annotations, Schema } from "effect/schema" import { describe, expect, it } from "tstyche" describe("Annotations", () => { - it("getUnsafe", () => { - const schema = Schema.String - const annotations = Annotations.getUnsafe(schema) - expect(annotations).type.toBe | undefined>() - const description = annotations?.description - expect(description).type.toBe() - const examples = annotations?.examples - expect(examples).type.toBe | undefined>() + describe("getUnsafe", () => { + it("String", () => { + const schema = Schema.String + const annotations = Annotations.getUnsafe(schema) + expect(annotations).type.toBe | undefined>() + expect(annotations?.examples).type.toBe | undefined>() + }) + + it("URL", () => { + const schema = Schema.URL + const annotations = Annotations.getUnsafe(schema) + expect(annotations).type.toBe | undefined>() + }) + + it("Option(string)", () => { + const schema = Schema.Option(Schema.String) + const annotations = Annotations.getUnsafe(schema) + expect(annotations).type.toBe, readonly [Schema.String]> | undefined>() + }) }) }) diff --git a/packages/effect/src/schema/Annotations.ts b/packages/effect/src/schema/Annotations.ts index 50457474c6..c0b27402ea 100644 --- a/packages/effect/src/schema/Annotations.ts +++ b/packages/effect/src/schema/Annotations.ts @@ -80,12 +80,16 @@ export interface Key extends Documentation { * @category Model * @since 4.0.0 */ -export interface Bottom = readonly []> extends Documentation { +export interface Bottom> extends Documentation { readonly contentEncoding?: string | undefined /** * The message to use when the value is invalid. */ readonly message?: string | undefined + /** + * The message to use when a key is unexpected. + */ + readonly messageUnexpectedKey?: string | undefined readonly identifier?: string | undefined readonly parseOptions?: AST.ParseOptions | undefined readonly jsonSchema?: @@ -93,47 +97,28 @@ export interface Bottom = re | ToJsonSchema.Annotation.Constraint | undefined readonly arbitrary?: - | ToArbitrary.Annotation.Declaration - | ToArbitrary.Annotation.Override + | ToArbitrary.Annotation.Override | ToArbitrary.Annotation.Constraint | ToArbitrary.Annotation.Constraints | undefined } -/** - * @category Model - * @since 4.0.0 - */ -export interface Struct extends Bottom { - /** - * The message to use when a key is unexpected. - */ - readonly messageUnexpectedKey?: string | undefined -} - /** * @category Model * @since 4.0.0 */ export interface Declaration = readonly []> - extends Documentation + extends Bottom { - readonly contentEncoding?: string | undefined - /** - * The message to use when the value is invalid. - */ - readonly message?: string | undefined - readonly identifier?: string | undefined - readonly parseOptions?: AST.ParseOptions | undefined readonly defaultJsonSerializer?: | (( typeParameters: { readonly [K in keyof TypeParameters]: Schema.Schema } ) => AST.Link) | undefined readonly jsonSchema?: ToJsonSchema.Annotation.Override | undefined - readonly arbitrary?: ToArbitrary.Annotation.Declaration | undefined - readonly equivalence?: ToEquivalence.Annotation.Declaration | undefined - readonly format?: ToFormat.Annotation.Declaration | undefined + readonly arbitrary?: ToArbitrary.Annotation.Override | undefined + readonly equivalence?: ToEquivalence.Annotation.Override | undefined + readonly format?: ToFormat.Annotation.Override | undefined /** @internal */ readonly "~sentinels"?: ReadonlyArray | undefined } @@ -265,8 +250,8 @@ export function getBrand(check: Check.Check): string | symbol | undefined /** * Return all the typed annotations from the schema. * - * This function is unsafe because it returns the annotations as they are stored - * in the AST, without any validation. + * This function is potentially unsafe because it returns the annotations as + * they are stored in the AST, without any validation. * * @since 4.0.0 */ diff --git a/packages/effect/src/schema/Schema.ts b/packages/effect/src/schema/Schema.ts index 53a2da6f1f..905242cd47 100644 --- a/packages/effect/src/schema/Schema.ts +++ b/packages/effect/src/schema/Schema.ts @@ -91,27 +91,28 @@ export interface MakeOptions { * @since 4.0.0 */ export interface Bottom< - T, - E, - RD, - RE, - Ast extends AST.AST, - RebuildOut extends Top, - AnnotateIn extends Annotations.Annotations = Annotations.Bottom, - TypeMakeIn = T, - Iso = T, - TypeMake = TypeMakeIn, - TypeMutability extends Mutability = "readonly", - TypeOptionality extends Optionality = "required", - TypeConstructorDefault extends ConstructorDefault = "no-default", - EncodedMutability extends Mutability = "readonly", - EncodedOptionality extends Optionality = "required" + out T, + out E, + out RD, + out RE, + out Ast extends AST.AST, + out RebuildOut extends Top, + out TypeMakeIn = T, + out Iso = T, + in out TypeParameters extends ReadonlyArray = readonly [], + out TypeMake = TypeMakeIn, + out TypeMutability extends Mutability = "readonly", + out TypeOptionality extends Optionality = "required", + out TypeConstructorDefault extends ConstructorDefault = "no-default", + out EncodedMutability extends Mutability = "readonly", + out EncodedOptionality extends Optionality = "required" > extends Pipeable.Pipeable { readonly [TypeId]: typeof TypeId readonly ast: Ast readonly "~rebuild.out": RebuildOut - readonly "~annotate.in": AnnotateIn + readonly "~type.parameters": TypeParameters + readonly "~annotate.in": Annotations.Bottom readonly "Type": T readonly "Encoded": E @@ -181,9 +182,9 @@ export function revealBottom( S["EncodingServices"], S["ast"], S["~rebuild.out"], - S["~annotate.in"], S["~type.make.in"], S["Iso"], + S["~type.parameters"], S["~type.make"], S["~type.mutability"], S["~type.optionality"], @@ -233,9 +234,9 @@ export interface Top extends unknown, AST.AST, Top, - Annotations.Annotations, unknown, unknown, + any, // this is because TypeParameters is invariant unknown, Mutability, Optionality, @@ -703,9 +704,9 @@ export interface optionalKey extends S["EncodingServices"], S["ast"], optionalKey, - S["~annotate.in"], S["~type.make.in"], S["Iso"], + S["~type.parameters"], S["~type.make"], S["~type.mutability"], "optional", @@ -799,9 +800,9 @@ export interface mutableKey extends S["EncodingServices"], S["ast"], mutableKey, - S["~annotate.in"], S["~type.make.in"], S["Iso"], + S["~type.parameters"], S["~type.make"], "mutable", S["~type.optionality"], @@ -837,9 +838,9 @@ export interface typeCodec extends never, S["ast"], typeCodec, - S["~annotate.in"], S["~type.make.in"], S["Iso"], + S["~type.parameters"], S["~type.make"], S["~type.mutability"], S["~type.optionality"], @@ -874,9 +875,9 @@ export interface encodedCodec extends never, AST.AST, encodedCodec, - Annotations.Bottom>, S["Encoded"], S["Encoded"], + ReadonlyArray, S["Encoded"], S["~type.mutability"], S["~type.optionality"], @@ -915,9 +916,9 @@ export interface flip extends S["DecodingServices"], AST.AST, flip, - Annotations.Bottom>, S["Encoded"], S["Encoded"], + ReadonlyArray, S["Encoded"], S["~encoded.mutability"], S["~encoded.optionality"], @@ -1404,7 +1405,6 @@ export interface Struct extends Struct.EncodingServices, AST.TypeLiteral, Struct, - Annotations.Struct>>, Simplify>, Simplify> > @@ -1618,7 +1618,6 @@ export interface Record$ extends Record.EncodingServices, AST.TypeLiteral, Record$, - Annotations.Bottom>, Simplify>, Record.Iso > @@ -1722,7 +1721,6 @@ export interface StructWithRest< StructWithRest.EncodingServices, AST.TypeLiteral, StructWithRest, - Annotations.Bottom>>, Simplify>, Simplify> > @@ -1838,7 +1836,6 @@ export interface Tuple extends Tuple.EncodingServices, AST.TupleType, Tuple, - Annotations.Bottom>, Tuple.MakeIn, Tuple.Iso > @@ -1964,7 +1961,6 @@ export interface TupleWithRest< S["EncodingServices"] | Rest[number]["EncodingServices"], AST.TupleType, TupleWithRest, - Annotations.Bottom>, TupleWithRest.MakeIn, TupleWithRest.Iso > @@ -1996,7 +1992,6 @@ export interface Array$ extends S["EncodingServices"], AST.TupleType, Array$, - Annotations.Bottom>, ReadonlyArray, ReadonlyArray > @@ -2029,7 +2024,6 @@ export interface NonEmptyArray extends S["EncodingServices"], AST.TupleType, NonEmptyArray, - Annotations.Bottom]>, readonly [S["~type.make"], ...Array], readonly [S["Iso"], ...Array] > @@ -2082,10 +2076,10 @@ export interface mutable extends S["EncodingServices"], S["ast"], mutable, - // we keep "~annotate.in", "~type.make" and "~type.make.in" as they are because they are contravariant - S["~annotate.in"], + // "~type.make" and "~type.make.in" as they are because they are contravariant S["~type.make.in"], S["Iso"], + S["~type.parameters"], S["~type.make"], S["~type.mutability"], S["~type.optionality"], @@ -2121,9 +2115,9 @@ export interface readonly$ extends S["EncodingServices"], S["ast"], readonly$, - S["~annotate.in"], S["~type.make.in"], S["Iso"], + S["~type.parameters"], S["~type.make"], S["~type.mutability"], S["~type.optionality"], @@ -2159,7 +2153,6 @@ export interface Union> extends Members[number]["EncodingServices"], AST.UnionType, Union, - Annotations.Bottom, Members[number]["~type.make"], Members[number]["Iso"] > @@ -2332,9 +2325,9 @@ export interface suspend extends S["EncodingServices"], AST.Suspend, suspend, - S["~annotate.in"], S["~type.make.in"], S["Iso"], + S["~type.parameters"], S["~type.make"], S["~type.mutability"], S["~type.optionality"], @@ -2377,9 +2370,9 @@ export interface refine extends S["EncodingServices"], S["ast"], refine, - Annotations.Bottom>, S["~type.make.in"], T, + S["~type.parameters"], T, S["~type.mutability"], S["~type.optionality"], @@ -2436,9 +2429,9 @@ export interface decodingMiddleware extends S["EncodingServices"], S["ast"], decodingMiddleware, - S["~annotate.in"], S["~type.make.in"], S["Iso"], + S["~type.parameters"], S["~type.make"], S["~type.mutability"], S["~type.optionality"], @@ -2479,9 +2472,9 @@ export interface encodingMiddleware extends RE, S["ast"], encodingMiddleware, - S["~annotate.in"], S["~type.make.in"], S["Iso"], + S["~type.parameters"], S["~type.make"], S["~type.mutability"], S["~type.optionality"], @@ -2562,9 +2555,9 @@ export interface decodeTo, - To["~annotate.in"], To["~type.make.in"], To["Iso"], + To["~type.parameters"], To["~type.make"], To["~type.mutability"], To["~type.optionality"], @@ -2685,9 +2678,9 @@ export interface withConstructorDefault, - S["~annotate.in"], S["~type.make.in"], S["Iso"], + S["~type.parameters"], S["~type.make"], S["~type.mutability"], S["~type.optionality"], @@ -2965,7 +2958,6 @@ export interface TaggedUnion> extends { [K in keyof Cases]: Cases[K]["EncodingServices"] }[keyof Cases], AST.UnionType, TaggedUnion, - Annotations.Bottom<{ [K in keyof Cases]: Cases[K]["Type"] }[keyof Cases]>, { [K in keyof Cases]: Cases[K]["~type.make"] }[keyof Cases] > { @@ -3063,8 +3055,8 @@ export function Option(value: A): Option { }) ), arbitrary: { - _tag: "Declaration", - declaration: ([value]) => (fc, ctx) => { + _tag: "Override", + override: ([value]) => (fc, ctx) => { return fc.oneof( ctx?.isSuspend ? { maxDepth: 2, depthIdentifier: "Option" } : {}, fc.constant(Option_.none()), @@ -3073,12 +3065,12 @@ export function Option(value: A): Option { } }, equivalence: { - _tag: "Declaration", - declaration: ([value]) => Option_.getEquivalence(value) + _tag: "Override", + override: ([value]) => Option_.getEquivalence(value) }, format: { - _tag: "Declaration", - declaration: ([value]) => + _tag: "Override", + override: ([value]) => Option_.match({ onNone: () => "none()", onSome: (t) => `some(${value(t)})` @@ -3241,8 +3233,8 @@ export function Result( }) ), arbitrary: { - _tag: "Declaration", - declaration: ([success, failure]) => (fc, ctx) => { + _tag: "Override", + override: ([success, failure]) => (fc, ctx) => { return fc.oneof( ctx?.isSuspend ? { maxDepth: 2, depthIdentifier: "Result" } : {}, success.map(Result_.succeed), @@ -3251,12 +3243,12 @@ export function Result( } }, equivalence: { - _tag: "Declaration", - declaration: ([success, failure]) => Result_.getEquivalence(success, failure) + _tag: "Override", + override: ([success, failure]) => Result_.getEquivalence(success, failure) }, format: { - _tag: "Declaration", - declaration: ([success, failure]) => + _tag: "Override", + override: ([success, failure]) => Result_.match({ onSuccess: (t) => `success(${success(t)})`, onFailure: (t) => `failure(${failure(t)})` @@ -3350,16 +3342,16 @@ export function Redacted(value: S, options?: { } ), arbitrary: { - _tag: "Declaration", - declaration: ([value]) => () => value.map((a) => Redacted_.make(a, { label: options?.label })) + _tag: "Override", + override: ([value]) => () => value.map((a) => Redacted_.make(a, { label: options?.label })) }, format: { - _tag: "Declaration", - declaration: () => globalThis.String + _tag: "Override", + override: () => globalThis.String }, equivalence: { - _tag: "Declaration", - declaration: ([value]) => Redacted_.getEquivalence(value) + _tag: "Override", + override: ([value]) => Redacted_.getEquivalence(value) } } ) @@ -3454,8 +3446,8 @@ export function CauseFailure(error: E, defect: D): }) ), arbitrary: { - _tag: "Declaration", - declaration: ([error, defect]) => (fc, ctx) => { + _tag: "Override", + override: ([error, defect]) => (fc, ctx) => { return fc.oneof( ctx?.isSuspend ? { maxDepth: 2, depthIdentifier: "Cause.Failure" } : {}, fc.constant(Cause_.failureInterrupt()), @@ -3466,8 +3458,8 @@ export function CauseFailure(error: E, defect: D): } }, equivalence: { - _tag: "Declaration", - declaration: ([error, defect]) => (a, b) => { + _tag: "Override", + override: ([error, defect]) => (a, b) => { if (a._tag !== b._tag) return false switch (a._tag) { case "Fail": @@ -3480,8 +3472,8 @@ export function CauseFailure(error: E, defect: D): } }, format: { - _tag: "Declaration", - declaration: ([error, defect]) => (t) => { + _tag: "Override", + override: ([error, defect]) => (t) => { switch (t._tag) { case "Fail": return `Fail(${error(t.error)})` @@ -3546,16 +3538,16 @@ export function Cause(error: E, defect: D): Cause< }) ), arbitrary: { - _tag: "Declaration", - declaration: ([failures]) => () => failures.map(Cause_.fromFailures) + _tag: "Override", + override: ([failures]) => () => failures.map(Cause_.fromFailures) }, equivalence: { - _tag: "Declaration", - declaration: ([failures]) => (a, b) => failures(a.failures, b.failures) + _tag: "Override", + override: ([failures]) => (a, b) => failures(a.failures, b.failures) }, format: { - _tag: "Declaration", - declaration: ([failures]) => (t) => `Cause(${failures(t.failures)})` + _tag: "Override", + override: ([failures]) => (t) => `Cause(${failures(t.failures)})` } } ) @@ -3586,8 +3578,8 @@ export const Error: Error = instanceOf(globalThis.Error, { title: "Error", defaultJsonSerializer: () => link()(ErrorJsonEncoded, Transformation.error()), arbitrary: { - _tag: "Declaration", - declaration: () => (fc) => fc.string().map((message) => new globalThis.Error(message)) + _tag: "Override", + override: () => (fc) => fc.string().map((message) => new globalThis.Error(message)) } }) @@ -3634,7 +3626,7 @@ export const Defect: Defect = Union([ defaultJsonSerializer: () => link()(Any, defectTransformation), arbitrary: { _tag: "Override", - override: (fc) => fc.json() + override: () => (fc) => fc.json() } }), defectTransformation @@ -3722,8 +3714,8 @@ export function Exit(value: A, erro }) ), arbitrary: { - _tag: "Declaration", - declaration: ([value, cause]) => (fc, ctx) => + _tag: "Override", + override: ([value, cause]) => (fc, ctx) => fc.oneof( ctx?.isSuspend ? { maxDepth: 2, depthIdentifier: "Exit" } : {}, value.map((v) => Exit_.succeed(v)), @@ -3731,8 +3723,8 @@ export function Exit(value: A, erro ) }, equivalence: { - _tag: "Declaration", - declaration: ([value, cause]) => (a, b) => { + _tag: "Override", + override: ([value, cause]) => (a, b) => { if (a._tag !== b._tag) return false switch (a._tag) { case "Success": @@ -3743,8 +3735,8 @@ export function Exit(value: A, erro } }, format: { - _tag: "Declaration", - declaration: ([value, cause]) => (t) => { + _tag: "Override", + override: ([value, cause]) => (t) => { switch (t._tag) { case "Success": return `Exit.Success(${value(t.value)})` @@ -3833,8 +3825,8 @@ export function Map(key: Key, value: Value): }) ), arbitrary: { - _tag: "Declaration", - declaration: ([key, value]) => (fc, ctx) => { + _tag: "Override", + override: ([key, value]) => (fc, ctx) => { return fc.oneof( ctx?.isSuspend ? { maxDepth: 2, depthIdentifier: "Map" } : {}, fc.constant([]), @@ -3843,8 +3835,8 @@ export function Map(key: Key, value: Value): } }, equivalence: { - _tag: "Declaration", - declaration: ([key, value]) => { + _tag: "Override", + override: ([key, value]) => { const entries = Arr.getEquivalence( Equivalence.make<[Key["Type"], Value["Type"]]>(([ka, va], [kb, vb]) => key(ka, kb) && value(va, vb)) ) @@ -3854,8 +3846,8 @@ export function Map(key: Key, value: Value): } }, format: { - _tag: "Declaration", - declaration: ([key, value]) => (t) => { + _tag: "Override", + override: ([key, value]) => (t) => { const size = t.size if (size === 0) { return "Map(0) {}" @@ -3880,9 +3872,9 @@ export interface Opaque extends S["EncodingServices"], S["ast"], S["~rebuild.out"], - S["~annotate.in"], S["~type.make.in"], S["Iso"], + S["~type.parameters"], S["~type.make"], S["~type.mutability"], S["~type.optionality"], @@ -3910,7 +3902,7 @@ export function Opaque() { /** * @since 4.0.0 */ -export interface instanceOf extends declareConstructor { +export interface instanceOf extends declare { readonly "~rebuild.out": this } @@ -3975,12 +3967,12 @@ export const URL: URL = instanceOf( }) ), arbitrary: { - _tag: "Declaration", - declaration: () => (fc) => fc.webUrl().map((s) => new globalThis.URL(s)) + _tag: "Override", + override: () => (fc) => fc.webUrl().map((s) => new globalThis.URL(s)) }, equivalence: { - _tag: "Declaration", - declaration: () => (a, b) => a.toString() === b.toString() + _tag: "Override", + override: () => (a, b) => a.toString() === b.toString() } } ) @@ -4011,8 +4003,8 @@ export const Date: Date = instanceOf( }) ), arbitrary: { - _tag: "Declaration", - declaration: () => (fc, ctx) => fc.date(ctx?.constraints?.DateConstraints) + _tag: "Override", + override: () => (fc, ctx) => fc.date(ctx?.constraints?.DateConstraints) } } ) @@ -4071,8 +4063,8 @@ export const Duration: Duration = declare( }) ), arbitrary: { - _tag: "Declaration", - declaration: () => (fc) => + _tag: "Override", + override: () => (fc) => fc.oneof( fc.constant(Duration_.infinity), fc.bigInt({ min: 0n }).map(Duration_.nanos), @@ -4080,12 +4072,12 @@ export const Duration: Duration = declare( ) }, format: { - _tag: "Declaration", - declaration: () => globalThis.String + _tag: "Override", + override: () => globalThis.String }, equivalence: { - _tag: "Declaration", - declaration: () => Duration_.Equivalence + _tag: "Override", + override: () => Duration_.Equivalence } } ) @@ -4201,6 +4193,7 @@ export function fromJsonString(schema: S): fromJsonString { "description": "a string that will be decoded as JSON" } case "draft-2020-12": + case "openApi3.1": return { "type": "string", "description": "a string that will be decoded as JSON", @@ -4293,9 +4286,9 @@ export interface Class, S>, - Annotations.Bottom, S["~type.make.in"], S["Iso"], + readonly [S], Self, S["~type.mutability"], S["~type.optionality"], @@ -4389,7 +4382,7 @@ function makeClass< static makeUnsafe(input: S["~type.make.in"], options?: MakeOptions): Self { return new this(input, options) } - static annotate(annotations: Annotations.Bottom) { + static annotate(annotations: Annotations.Declaration) { return this.rebuild(AST.annotate(this.ast, annotations)) } static annotateKey(annotations: Annotations.Key) { @@ -4443,13 +4436,12 @@ function getClassSchemaFactory( [AST.ClassTypeId]: ([from]: readonly [AST.AST]) => new AST.Link(from, transformation), defaultIsoSerializer: ([from]: readonly [Schema]) => new AST.Link(from.ast, transformation), arbitrary: { - _tag: "Declaration", - declaration: ([from]: readonly [FastCheck.Arbitrary]) => () => - from.map((args) => new self(args)) + _tag: "Override", + override: ([from]: readonly [FastCheck.Arbitrary]) => () => from.map((args) => new self(args)) }, format: { - _tag: "Declaration", - declaration: ([from]: readonly [Format]) => (t: Self) => `${self.identifier}(${from(t)})` + _tag: "Override", + override: ([from]: readonly [Format]) => (t: Self) => `${self.identifier}(${from(t)})` } }, annotations) ) @@ -4465,6 +4457,10 @@ function getClassSchemaFactory( } } +function isStruct(schema: Struct.Fields | Struct): schema is Struct { + return isSchema(schema) +} + /** * @category Constructors * @since 4.0.0 @@ -4485,7 +4481,7 @@ export const Class: { schema: Struct.Fields | Struct, annotations?: Annotations.Declaration]> ): ExtendableClass, Brand> => { - const struct = isSchema(schema) ? schema : Struct(schema) + const struct = isStruct(schema) ? schema : Struct(schema) return makeClass(Data.Class, id, struct, annotations) } @@ -4516,7 +4512,7 @@ export const ErrorClass: { schema: Struct.Fields | Struct, annotations?: Annotations.Declaration]> ): ErrorClass, Cause_.YieldableError & Brand> => { - const struct = isSchema(schema) ? schema : Struct(schema) + const struct = isStruct(schema) ? schema : Struct(schema) return makeClass(core.Error, id, struct, annotations) } @@ -4625,8 +4621,8 @@ export const Uint8Array: Uint8Array = instanceOf(globalThis.Uint8Array (fc) => fc.uint8Array() + _tag: "Override", + override: () => (fc) => fc.uint8Array() } }) @@ -4657,19 +4653,19 @@ export const DateTimeUtc: DateTimeUtc = declare( } ), arbitrary: { - _tag: "Declaration", - declaration: () => (fc, ctx) => + _tag: "Override", + override: () => (fc, ctx) => fc.date({ noInvalidDate: true, ...ctx?.constraints?.DateConstraints }).map((date) => DateTime.fromDateUnsafe(date) ) }, format: { - _tag: "Declaration", - declaration: () => (utc) => utc.toString() + _tag: "Override", + override: () => (utc) => utc.toString() }, equivalence: { - _tag: "Declaration", - declaration: () => DateTime.Equivalence + _tag: "Override", + override: () => DateTime.Equivalence } } ) @@ -4766,9 +4762,9 @@ export interface declareConstructor, - Annotations.Bottom, T, - Iso + Iso, + TypeParameters > { readonly "~rebuild.out": this diff --git a/packages/effect/src/schema/ToArbitrary.ts b/packages/effect/src/schema/ToArbitrary.ts index b2e3661f5d..b461f904f7 100644 --- a/packages/effect/src/schema/ToArbitrary.ts +++ b/packages/effect/src/schema/ToArbitrary.ts @@ -20,16 +20,6 @@ import type * as Schema from "./Schema.ts" * @since 4.0.0 */ export declare namespace Annotation { - /** - * @since 4.0.0 - */ - export type Declaration> = { - readonly _tag: "Declaration" - readonly declaration: ( - typeParameters: { readonly [K in keyof TypeParameters]: FastCheck.Arbitrary } - ) => (fc: typeof FastCheck, context?: Context) => FastCheck.Arbitrary - } - /** * @since 4.0.0 */ @@ -71,7 +61,7 @@ export declare namespace Annotation { /** * @since 4.0.0 */ - export type Any = + export type FastCheckConstraint = | StringConstraints | NumberConstraints | BigIntConstraints @@ -83,7 +73,7 @@ export declare namespace Annotation { */ export type Constraint = { readonly _tag: "Constraint" - readonly constraint: Any + readonly constraint: FastCheckConstraint } /** @@ -103,9 +93,11 @@ export declare namespace Annotation { /** * @since 4.0.0 */ - export type Override = { + export type Override> = { readonly _tag: "Override" - readonly override: (fc: typeof FastCheck, context?: Context) => FastCheck.Arbitrary + readonly override: ( + typeParameters: { readonly [K in keyof TypeParameters]: FastCheck.Arbitrary } + ) => (fc: typeof FastCheck, context?: Context) => FastCheck.Arbitrary } } @@ -146,10 +138,9 @@ const arbitraryMemoMap = new WeakMap>() function getAnnotation( annotations: Annotations.Annotations | undefined ): - | Annotation.Declaration> | Annotation.Constraint | Annotation.Constraints - | Annotation.Override + | Annotation.Override> | undefined { return annotations?.arbitrary as any @@ -224,7 +215,7 @@ const combiner: Combiner.Combiner = Struct.getCombiner({ function merge( constraints: Annotation.Constraints["constraints"], - constraint: Annotation.Any + constraint: Annotation.FastCheckConstraint ): Annotation.Constraints["constraints"] { const _tag = constraint._tag const c = constraints[_tag] @@ -277,15 +268,13 @@ const go = memoize((ast: AST.AST): LazyArbitrary => { const annotation = getAnnotation(ast.annotations) if (annotation) { switch (annotation._tag) { - case "Declaration": { + case "Override": { if (AST.isDeclaration(ast)) { const typeParameters = ast.typeParameters.map(go) - return (fc, ctx) => annotation.declaration(typeParameters.map((tp) => tp(fc, resetContext(ctx))))(fc, ctx) + return (fc, ctx) => annotation.override(typeParameters.map((tp) => tp(fc, resetContext(ctx))))(fc, ctx) } - throw new Error("Declaration annotation found on non-declaration AST") + return annotation.override([]) } - case "Override": - return annotation.override case "Constraint": case "Constraints": throw new Error("Constraint annotation found on non-constrained AST") diff --git a/packages/effect/src/schema/ToEquivalence.ts b/packages/effect/src/schema/ToEquivalence.ts index 08425e4d0f..2482b586f9 100644 --- a/packages/effect/src/schema/ToEquivalence.ts +++ b/packages/effect/src/schema/ToEquivalence.ts @@ -17,20 +17,12 @@ export declare namespace Annotation { /** * @since 4.0.0 */ - export type Declaration> = { - readonly _tag: "Declaration" - readonly declaration: ( + export type Override> = { + readonly _tag: "Override" + readonly override: ( typeParameters: { readonly [K in keyof TypeParameters]: Equivalence.Equivalence } ) => Equivalence.Equivalence } - - /** - * @since 4.0.0 - */ - export type Override = { - readonly _tag: "Override" - readonly override: () => Equivalence.Equivalence - } } /** @@ -54,9 +46,7 @@ export function override(override: () => Equivalence.Equiv } } -function getAnnotation( - ast: AST.AST -): Annotation.Declaration> | Annotation.Override | undefined { +function getAnnotation(ast: AST.AST): Annotation.Override> | undefined { return Annotations.get(ast)?.["equivalence"] as any } @@ -66,16 +56,10 @@ const go = memoize((ast: AST.AST): Equivalence.Equivalence => { // --------------------------------------------- const annotation = getAnnotation(ast) if (annotation) { - switch (annotation._tag) { - case "Declaration": { - if (AST.isDeclaration(ast)) { - return annotation.declaration(ast.typeParameters.map(go)) - } - throw new Error("Declaration annotation found on non-declaration AST") - } - case "Override": - return annotation.override() + if (AST.isDeclaration(ast)) { + return annotation.override(ast.typeParameters.map(go)) } + return annotation.override([]) } switch (ast._tag) { case "NeverKeyword": diff --git a/packages/effect/src/schema/ToFormat.ts b/packages/effect/src/schema/ToFormat.ts index 1b8ca99b51..3d126c1625 100644 --- a/packages/effect/src/schema/ToFormat.ts +++ b/packages/effect/src/schema/ToFormat.ts @@ -17,20 +17,12 @@ export declare namespace Annotation { /** * @since 4.0.0 */ - export type Declaration> = { - readonly _tag: "Declaration" - readonly declaration: ( + export type Override> = { + readonly _tag: "Override" + readonly override: ( typeParameters: { readonly [K in keyof TypeParameters]: Format } ) => Format } - - /** - * @since 4.0.0 - */ - export type Override = { - readonly _tag: "Override" - readonly override: () => Format - } } /** @@ -47,9 +39,7 @@ export function override(override: () => Format } } -function getAnnotation( - ast: AST.AST -): Annotation.Declaration> | Annotation.Override | undefined { +function getAnnotation(ast: AST.AST): Annotation.Override> | undefined { return Annotations.get(ast)?.["format"] as any } @@ -66,16 +56,10 @@ export const defaultReducerAlg: AST.ReducerAlg> = { // --------------------------------------------- const annotation = getAnnotation(ast) if (annotation) { - switch (annotation._tag) { - case "Declaration": { - if (AST.isDeclaration(ast)) { - return Option.some(annotation.declaration(ast.typeParameters.map(reduce))) - } - throw new Error("Declaration annotation found on non-declaration AST") - } - case "Override": - return Option.some(annotation.override()) + if (AST.isDeclaration(ast)) { + return Option.some(annotation.override(ast.typeParameters.map(reduce))) } + return Option.some(annotation.override([])) } return Option.none() }, diff --git a/packages/effect/src/unstable/ai/McpSchema.ts b/packages/effect/src/unstable/ai/McpSchema.ts index 43cd534514..a94bef5a3b 100644 --- a/packages/effect/src/unstable/ai/McpSchema.ts +++ b/packages/effect/src/unstable/ai/McpSchema.ts @@ -2050,9 +2050,9 @@ export interface Param extends S["EncodingServices"], S["ast"], Param, - S["~annotate.in"], S["~type.make.in"], S["Iso"], + S["~type.parameters"], S["~type.make"], S["~type.mutability"], S["~type.optionality"], diff --git a/packages/effect/src/unstable/http/Headers.ts b/packages/effect/src/unstable/http/Headers.ts index 1377085845..331c5d6e3b 100644 --- a/packages/effect/src/unstable/http/Headers.ts +++ b/packages/effect/src/unstable/http/Headers.ts @@ -83,8 +83,8 @@ export const HeadersSchema: HeadersSchema = Schema.declare( { title: "Headers", equivalence: { - _tag: "Declaration", - declaration: () => Equivalence + _tag: "Override", + override: () => Equivalence }, defaultIsoSerializer: () => Schema.link()( diff --git a/packages/effect/src/unstable/http/UrlParams.ts b/packages/effect/src/unstable/http/UrlParams.ts index ba48b96b8e..3d40f85de9 100644 --- a/packages/effect/src/unstable/http/UrlParams.ts +++ b/packages/effect/src/unstable/http/UrlParams.ts @@ -158,8 +158,8 @@ export const UrlParamsSchema: UrlParamsSchema = Schema.declare( { title: "UrlParams", equivalence: { - _tag: "Declaration", - declaration: () => Equivalence + _tag: "Override", + override: () => Equivalence }, defaultIsoSerializer: () => Schema.link()( diff --git a/packages/effect/src/unstable/httpapi/HttpApiSchema.ts b/packages/effect/src/unstable/httpapi/HttpApiSchema.ts index a056bd14ff..c81af7f68e 100644 --- a/packages/effect/src/unstable/httpapi/HttpApiSchema.ts +++ b/packages/effect/src/unstable/httpapi/HttpApiSchema.ts @@ -153,9 +153,9 @@ export interface Param extends S["EncodingServices"], S["ast"], Param, - S["~annotate.in"], S["~type.make.in"], S["Iso"], + S["~type.parameters"], S["~type.make"], S["~type.mutability"], S["~type.optionality"], @@ -518,7 +518,7 @@ export interface EmptyErrorClass extends never, AST.Declaration, EmptyErrorClass, // TODO: Fix this - Annotations.Bottom + readonly [] // TODO: Fix this > { new(): { readonly _tag: Tag } & YieldableError diff --git a/packages/effect/src/unstable/rpc/RpcSchema.ts b/packages/effect/src/unstable/rpc/RpcSchema.ts index 65420e08a3..84092350ff 100644 --- a/packages/effect/src/unstable/rpc/RpcSchema.ts +++ b/packages/effect/src/unstable/rpc/RpcSchema.ts @@ -2,7 +2,6 @@ * @since 4.0.0 */ import * as Predicate from "../../data/Predicate.ts" -import type * as Annotations from "../../schema/Annotations.ts" import type * as AST from "../../schema/AST.ts" import * as Schema from "../../schema/Schema.ts" import * as Stream_ from "../../stream/Stream.ts" @@ -41,8 +40,7 @@ export interface Stream extends A["DecodingServices"] | E["DecodingServices"], A["EncodingServices"] | E["EncodingServices"], AST.Declaration, - Stream, - Annotations.Bottom> + Stream > { readonly "~rebuild.out": this diff --git a/packages/effect/src/unstable/schema/VariantSchema.ts b/packages/effect/src/unstable/schema/VariantSchema.ts index ba448bbd71..714a82bafc 100644 --- a/packages/effect/src/unstable/schema/VariantSchema.ts +++ b/packages/effect/src/unstable/schema/VariantSchema.ts @@ -221,9 +221,9 @@ export interface Class< S["EncodingServices"], AST.Declaration, Schema.decodeTo, S>, - Annotations.Bottom, S["~type.make.in"], S["Iso"], + readonly [S], Self, S["~type.mutability"], S["~type.optionality"], @@ -349,7 +349,7 @@ export const make = < ) => ( fields: Fields & Struct.Validate, annotations?: - | Annotations.Bottom>]> + | Annotations.Declaration>]> | undefined ) => [Self] extends [never] ? MissingSelfGeneric : @@ -377,7 +377,7 @@ export const make = < function Class(identifier: string) { return function( fields: Struct.Fields, - annotations?: Annotations.Bottom>]> + annotations?: Annotations.Declaration>]> ) { const variantStruct = Struct(fields) const schema = extract(variantStruct, options.defaultVariant, { diff --git a/packages/effect/test/schema/Differ.test.ts b/packages/effect/test/schema/Differ.test.ts index 0f32aba3d1..53f4711b51 100644 --- a/packages/effect/test/schema/Differ.test.ts +++ b/packages/effect/test/schema/Differ.test.ts @@ -227,7 +227,7 @@ describe("Differ", () => { }) it("roundtrip", () => { - roundtrip(Schema.Any.annotate({ arbitrary: { _tag: "Override", override: (fc) => fc.json() } })) + roundtrip(Schema.Any.annotate({ arbitrary: { _tag: "Override", override: () => (fc) => fc.json() } })) roundtrip(Schema.String) roundtrip(Schema.Number) roundtrip(Schema.Boolean) diff --git a/packages/effect/test/schema/ToEquivalence.test.ts b/packages/effect/test/schema/ToEquivalence.test.ts index 5d48dd19f6..b3c1c7695a 100644 --- a/packages/effect/test/schema/ToEquivalence.test.ts +++ b/packages/effect/test/schema/ToEquivalence.test.ts @@ -2,7 +2,7 @@ import { DateTime, Duration } from "effect" import { Equivalence, Option, Redacted, Result } from "effect/data" import { Check, Schema, ToEquivalence } from "effect/schema" import { describe, it } from "vitest" -import { assertFalse, assertTrue, throws } from "../utils/assert.ts" +import { assertFalse, assertTrue } from "../utils/assert.ts" describe("ToEquivalence", () => { it("String", () => { @@ -378,13 +378,6 @@ describe("ToEquivalence", () => { }) describe("Annotations", () => { - it("should throw on non-declaration ASTs", () => { - const schema = Schema.String.annotate({ - equivalence: { _tag: "Declaration", declaration: () => Equivalence.make((a, b) => a === b) } - }) - throws(() => ToEquivalence.make(schema), new Error("Declaration annotation found on non-declaration AST")) - }) - describe("Override annotation", () => { it("String", () => { const schema = Schema.String.pipe( diff --git a/packages/effect/test/schema/ToFormat.test.ts b/packages/effect/test/schema/ToFormat.test.ts index 874c609f7c..cde123fd14 100644 --- a/packages/effect/test/schema/ToFormat.test.ts +++ b/packages/effect/test/schema/ToFormat.test.ts @@ -2,7 +2,7 @@ import { DateTime, Duration } from "effect" import { Option, Redacted, Result } from "effect/data" import { Check, Schema, ToFormat } from "effect/schema" import { describe, it } from "vitest" -import { strictEqual, throws } from "../utils/assert.ts" +import { strictEqual } from "../utils/assert.ts" describe("ToFormat", () => { it("Any", () => { @@ -467,13 +467,6 @@ describe("ToFormat", () => { }) describe("Annotations", () => { - it("should throw on non-declaration ASTs", () => { - const schema = Schema.String.annotate({ - format: { _tag: "Declaration", declaration: () => (s: string) => s.toUpperCase() } - }) - throws(() => ToFormat.make(schema), new Error("Declaration annotation found on non-declaration AST")) - }) - describe("Override annotation", () => { it("String", () => { const schema = Schema.String.pipe(ToFormat.override(() => (s) => s.toUpperCase()))