mirror of
https://github.com/Effect-TS/effect.git
synced 2026-12-01 03:05:36 +08:00
Schema: refactor Annotations (#610)
This commit is contained in:
+18
-17
@@ -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<T>,
|
||||
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<Top> = 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<T, TypeParameters>
|
||||
|
||||
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):
|
||||
|
||||
|
||||
@@ -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<Annotations.Bottom<string, readonly []> | undefined>()
|
||||
const description = annotations?.description
|
||||
expect(description).type.toBe<string | undefined>()
|
||||
const examples = annotations?.examples
|
||||
expect(examples).type.toBe<ReadonlyArray<string> | undefined>()
|
||||
describe("getUnsafe", () => {
|
||||
it("String", () => {
|
||||
const schema = Schema.String
|
||||
const annotations = Annotations.getUnsafe(schema)
|
||||
expect(annotations).type.toBe<Annotations.Bottom<string, readonly []> | undefined>()
|
||||
expect(annotations?.examples).type.toBe<ReadonlyArray<string> | undefined>()
|
||||
})
|
||||
|
||||
it("URL", () => {
|
||||
const schema = Schema.URL
|
||||
const annotations = Annotations.getUnsafe(schema)
|
||||
expect(annotations).type.toBe<Annotations.Bottom<URL, readonly []> | undefined>()
|
||||
})
|
||||
|
||||
it("Option(string)", () => {
|
||||
const schema = Schema.Option(Schema.String)
|
||||
const annotations = Annotations.getUnsafe(schema)
|
||||
expect(annotations).type.toBe<Annotations.Bottom<Option.Option<string>, readonly [Schema.String]> | undefined>()
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -80,12 +80,16 @@ export interface Key<T> extends Documentation<T> {
|
||||
* @category Model
|
||||
* @since 4.0.0
|
||||
*/
|
||||
export interface Bottom<T, TypeParameters extends ReadonlyArray<Schema.Top> = readonly []> extends Documentation<T> {
|
||||
export interface Bottom<T, TypeParameters extends ReadonlyArray<Schema.Top>> extends Documentation<T> {
|
||||
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<T, TypeParameters extends ReadonlyArray<Schema.Top> = re
|
||||
| ToJsonSchema.Annotation.Constraint
|
||||
| undefined
|
||||
readonly arbitrary?:
|
||||
| ToArbitrary.Annotation.Declaration<T, TypeParameters>
|
||||
| ToArbitrary.Annotation.Override<T>
|
||||
| ToArbitrary.Annotation.Override<T, TypeParameters>
|
||||
| ToArbitrary.Annotation.Constraint
|
||||
| ToArbitrary.Annotation.Constraints
|
||||
| undefined
|
||||
}
|
||||
|
||||
/**
|
||||
* @category Model
|
||||
* @since 4.0.0
|
||||
*/
|
||||
export interface Struct<T> extends Bottom<T> {
|
||||
/**
|
||||
* The message to use when a key is unexpected.
|
||||
*/
|
||||
readonly messageUnexpectedKey?: string | undefined
|
||||
}
|
||||
|
||||
/**
|
||||
* @category Model
|
||||
* @since 4.0.0
|
||||
*/
|
||||
export interface Declaration<T, TypeParameters extends ReadonlyArray<Schema.Top> = readonly []>
|
||||
extends Documentation<T>
|
||||
extends Bottom<T, TypeParameters>
|
||||
{
|
||||
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<TypeParameters[K]["Encoded"]> }
|
||||
) => AST.Link)
|
||||
| undefined
|
||||
readonly jsonSchema?: ToJsonSchema.Annotation.Override | undefined
|
||||
readonly arbitrary?: ToArbitrary.Annotation.Declaration<T, TypeParameters> | undefined
|
||||
readonly equivalence?: ToEquivalence.Annotation.Declaration<T, TypeParameters> | undefined
|
||||
readonly format?: ToFormat.Annotation.Declaration<T, TypeParameters> | undefined
|
||||
readonly arbitrary?: ToArbitrary.Annotation.Override<T, TypeParameters> | undefined
|
||||
readonly equivalence?: ToEquivalence.Annotation.Override<T, TypeParameters> | undefined
|
||||
readonly format?: ToFormat.Annotation.Override<T, TypeParameters> | undefined
|
||||
/** @internal */
|
||||
readonly "~sentinels"?: ReadonlyArray<AST.Sentinel> | undefined
|
||||
}
|
||||
@@ -265,8 +250,8 @@ export function getBrand<T>(check: Check.Check<T>): 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
|
||||
*/
|
||||
|
||||
@@ -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<T>,
|
||||
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<Top> = 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<T, TypeParameters>
|
||||
|
||||
readonly "Type": T
|
||||
readonly "Encoded": E
|
||||
@@ -181,9 +182,9 @@ export function revealBottom<S extends Top>(
|
||||
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<S extends Top> extends
|
||||
S["EncodingServices"],
|
||||
S["ast"],
|
||||
optionalKey<S>,
|
||||
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<S extends Top> extends
|
||||
S["EncodingServices"],
|
||||
S["ast"],
|
||||
mutableKey<S>,
|
||||
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<S extends Top> extends
|
||||
never,
|
||||
S["ast"],
|
||||
typeCodec<S>,
|
||||
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<S extends Top> extends
|
||||
never,
|
||||
AST.AST,
|
||||
encodedCodec<S>,
|
||||
Annotations.Bottom<S["Encoded"], ReadonlyArray<Top>>,
|
||||
S["Encoded"],
|
||||
S["Encoded"],
|
||||
ReadonlyArray<Top>,
|
||||
S["Encoded"],
|
||||
S["~type.mutability"],
|
||||
S["~type.optionality"],
|
||||
@@ -915,9 +916,9 @@ export interface flip<S extends Top> extends
|
||||
S["DecodingServices"],
|
||||
AST.AST,
|
||||
flip<S>,
|
||||
Annotations.Bottom<S["Encoded"], ReadonlyArray<Top>>,
|
||||
S["Encoded"],
|
||||
S["Encoded"],
|
||||
ReadonlyArray<Top>,
|
||||
S["Encoded"],
|
||||
S["~encoded.mutability"],
|
||||
S["~encoded.optionality"],
|
||||
@@ -1404,7 +1405,6 @@ export interface Struct<Fields extends Struct.Fields> extends
|
||||
Struct.EncodingServices<Fields>,
|
||||
AST.TypeLiteral,
|
||||
Struct<Fields>,
|
||||
Annotations.Struct<Simplify<Struct.Type<Fields>>>,
|
||||
Simplify<Struct.MakeIn<Fields>>,
|
||||
Simplify<Struct.Iso<Fields>>
|
||||
>
|
||||
@@ -1618,7 +1618,6 @@ export interface Record$<Key extends Record.Key, Value extends Top> extends
|
||||
Record.EncodingServices<Key, Value>,
|
||||
AST.TypeLiteral,
|
||||
Record$<Key, Value>,
|
||||
Annotations.Bottom<Record.Type<Key, Value>>,
|
||||
Simplify<Record.MakeIn<Key, Value>>,
|
||||
Record.Iso<Key, Value>
|
||||
>
|
||||
@@ -1722,7 +1721,6 @@ export interface StructWithRest<
|
||||
StructWithRest.EncodingServices<S, Records>,
|
||||
AST.TypeLiteral,
|
||||
StructWithRest<S, Records>,
|
||||
Annotations.Bottom<Simplify<StructWithRest.Type<S, Records>>>,
|
||||
Simplify<StructWithRest.MakeIn<S, Records>>,
|
||||
Simplify<StructWithRest.Iso<S, Records>>
|
||||
>
|
||||
@@ -1838,7 +1836,6 @@ export interface Tuple<Elements extends Tuple.Elements> extends
|
||||
Tuple.EncodingServices<Elements>,
|
||||
AST.TupleType,
|
||||
Tuple<Elements>,
|
||||
Annotations.Bottom<Tuple.Type<Elements>>,
|
||||
Tuple.MakeIn<Elements>,
|
||||
Tuple.Iso<Elements>
|
||||
>
|
||||
@@ -1964,7 +1961,6 @@ export interface TupleWithRest<
|
||||
S["EncodingServices"] | Rest[number]["EncodingServices"],
|
||||
AST.TupleType,
|
||||
TupleWithRest<S, Rest>,
|
||||
Annotations.Bottom<TupleWithRest.Type<S["Type"], Rest>>,
|
||||
TupleWithRest.MakeIn<S["~type.make"], Rest>,
|
||||
TupleWithRest.Iso<S["Iso"], Rest>
|
||||
>
|
||||
@@ -1996,7 +1992,6 @@ export interface Array$<S extends Top> extends
|
||||
S["EncodingServices"],
|
||||
AST.TupleType,
|
||||
Array$<S>,
|
||||
Annotations.Bottom<ReadonlyArray<S["Type"]>>,
|
||||
ReadonlyArray<S["~type.make"]>,
|
||||
ReadonlyArray<S["Iso"]>
|
||||
>
|
||||
@@ -2029,7 +2024,6 @@ export interface NonEmptyArray<S extends Top> extends
|
||||
S["EncodingServices"],
|
||||
AST.TupleType,
|
||||
NonEmptyArray<S>,
|
||||
Annotations.Bottom<readonly [S["Type"], ...Array<S["Type"]>]>,
|
||||
readonly [S["~type.make"], ...Array<S["~type.make"]>],
|
||||
readonly [S["Iso"], ...Array<S["Iso"]>]
|
||||
>
|
||||
@@ -2082,10 +2076,10 @@ export interface mutable<S extends Top> extends
|
||||
S["EncodingServices"],
|
||||
S["ast"],
|
||||
mutable<S>,
|
||||
// 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$<S extends Top> extends
|
||||
S["EncodingServices"],
|
||||
S["ast"],
|
||||
readonly$<S>,
|
||||
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<Members extends ReadonlyArray<Top>> extends
|
||||
Members[number]["EncodingServices"],
|
||||
AST.UnionType<Members[number]["ast"]>,
|
||||
Union<Members>,
|
||||
Annotations.Bottom<Members[number]["Type"]>,
|
||||
Members[number]["~type.make"],
|
||||
Members[number]["Iso"]
|
||||
>
|
||||
@@ -2332,9 +2325,9 @@ export interface suspend<S extends Top> extends
|
||||
S["EncodingServices"],
|
||||
AST.Suspend,
|
||||
suspend<S>,
|
||||
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<T extends S["Type"], S extends Top> extends
|
||||
S["EncodingServices"],
|
||||
S["ast"],
|
||||
refine<T, S>,
|
||||
Annotations.Bottom<T, ReadonlyArray<Top>>,
|
||||
S["~type.make.in"],
|
||||
T,
|
||||
S["~type.parameters"],
|
||||
T,
|
||||
S["~type.mutability"],
|
||||
S["~type.optionality"],
|
||||
@@ -2436,9 +2429,9 @@ export interface decodingMiddleware<S extends Top, RD> extends
|
||||
S["EncodingServices"],
|
||||
S["ast"],
|
||||
decodingMiddleware<S, RD>,
|
||||
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<S extends Top, RE> extends
|
||||
RE,
|
||||
S["ast"],
|
||||
encodingMiddleware<S, RE>,
|
||||
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 extends Top, From extends Top, RD = never, RE = nev
|
||||
To["EncodingServices"] | From["EncodingServices"] | RE,
|
||||
To["ast"],
|
||||
decodeTo<To, From, RD, RE>,
|
||||
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 extends Top & WithoutConstructorDefaul
|
||||
S["EncodingServices"],
|
||||
S["ast"],
|
||||
withConstructorDefault<S>,
|
||||
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<Cases extends Record<string, Top>> extends
|
||||
{ [K in keyof Cases]: Cases[K]["EncodingServices"] }[keyof Cases],
|
||||
AST.UnionType<AST.TypeLiteral>,
|
||||
TaggedUnion<Cases>,
|
||||
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<A extends Top>(value: A): Option<A> {
|
||||
})
|
||||
),
|
||||
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<A extends Top>(value: A): Option<A> {
|
||||
}
|
||||
},
|
||||
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<A extends Top, E extends Top>(
|
||||
})
|
||||
),
|
||||
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<A extends Top, E extends Top>(
|
||||
}
|
||||
},
|
||||
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<S extends Top>(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<E extends Top, D extends Top>(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<E extends Top, D extends Top>(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<E extends Top, D extends Top>(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<E extends Top, D extends Top>(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<globalThis.Error>()(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<unknown>()(Any, defectTransformation),
|
||||
arbitrary: {
|
||||
_tag: "Override",
|
||||
override: (fc) => fc.json()
|
||||
override: () => (fc) => fc.json()
|
||||
}
|
||||
}),
|
||||
defectTransformation
|
||||
@@ -3722,8 +3714,8 @@ export function Exit<A extends Top, E extends Top, D extends Top>(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<A extends Top, E extends Top, D extends Top>(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<A extends Top, E extends Top, D extends Top>(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 extends Top, Value extends Top>(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 extends Top, Value extends Top>(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 extends Top, Value extends Top>(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<Self, S extends Top, Brand> 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<Self, Brand = {}>() {
|
||||
/**
|
||||
* @since 4.0.0
|
||||
*/
|
||||
export interface instanceOf<T, Iso = T> extends declareConstructor<T, T, readonly [], Iso> {
|
||||
export interface instanceOf<T, Iso = T> extends declare<T, Iso> {
|
||||
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<S extends Top>(schema: S): fromJsonString<S> {
|
||||
"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<Self, S extends Top & { readonly fields: Struct.Fields },
|
||||
S["EncodingServices"],
|
||||
AST.Declaration,
|
||||
decodeTo<declareConstructor<Self, S["Encoded"], readonly [S], S["Iso"]>, S>,
|
||||
Annotations.Bottom<Self, readonly [S]>,
|
||||
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<Self, readonly [S]>) {
|
||||
static annotate(annotations: Annotations.Declaration<Self, readonly [S]>) {
|
||||
return this.rebuild(AST.annotate(this.ast, annotations))
|
||||
}
|
||||
static annotateKey(annotations: Annotations.Key<Self>) {
|
||||
@@ -4443,13 +4436,12 @@ function getClassSchemaFactory<S extends Top>(
|
||||
[AST.ClassTypeId]: ([from]: readonly [AST.AST]) => new AST.Link(from, transformation),
|
||||
defaultIsoSerializer: ([from]: readonly [Schema<S["Type"]>]) => new AST.Link(from.ast, transformation),
|
||||
arbitrary: {
|
||||
_tag: "Declaration",
|
||||
declaration: ([from]: readonly [FastCheck.Arbitrary<S["Type"]>]) => () =>
|
||||
from.map((args) => new self(args))
|
||||
_tag: "Override",
|
||||
override: ([from]: readonly [FastCheck.Arbitrary<S["Type"]>]) => () => from.map((args) => new self(args))
|
||||
},
|
||||
format: {
|
||||
_tag: "Declaration",
|
||||
declaration: ([from]: readonly [Format<S["Type"]>]) => (t: Self) => `${self.identifier}(${from(t)})`
|
||||
_tag: "Override",
|
||||
override: ([from]: readonly [Format<S["Type"]>]) => (t: Self) => `${self.identifier}(${from(t)})`
|
||||
}
|
||||
}, annotations)
|
||||
)
|
||||
@@ -4465,6 +4457,10 @@ function getClassSchemaFactory<S extends Top>(
|
||||
}
|
||||
}
|
||||
|
||||
function isStruct(schema: Struct.Fields | Struct<Struct.Fields>): schema is Struct<Struct.Fields> {
|
||||
return isSchema(schema)
|
||||
}
|
||||
|
||||
/**
|
||||
* @category Constructors
|
||||
* @since 4.0.0
|
||||
@@ -4485,7 +4481,7 @@ export const Class: {
|
||||
schema: Struct.Fields | Struct<Struct.Fields>,
|
||||
annotations?: Annotations.Declaration<Self, readonly [Struct<Struct.Fields>]>
|
||||
): ExtendableClass<Self, Struct<Struct.Fields>, 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<Struct.Fields>,
|
||||
annotations?: Annotations.Declaration<Self, readonly [Struct<Struct.Fields>]>
|
||||
): ErrorClass<Self, Struct<Struct.Fields>, 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<ArrayBuff
|
||||
}
|
||||
),
|
||||
arbitrary: {
|
||||
_tag: "Declaration",
|
||||
declaration: () => (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<T, E, TypeParameters extends ReadonlyArray<T
|
||||
TypeParameters[number]["EncodingServices"],
|
||||
AST.Declaration,
|
||||
declareConstructor<T, E, TypeParameters, Iso>,
|
||||
Annotations.Bottom<T, TypeParameters>,
|
||||
T,
|
||||
Iso
|
||||
Iso,
|
||||
TypeParameters
|
||||
>
|
||||
{
|
||||
readonly "~rebuild.out": this
|
||||
|
||||
@@ -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<T, TypeParameters extends ReadonlyArray<Schema.Top>> = {
|
||||
readonly _tag: "Declaration"
|
||||
readonly declaration: (
|
||||
typeParameters: { readonly [K in keyof TypeParameters]: FastCheck.Arbitrary<TypeParameters[K]["Type"]> }
|
||||
) => (fc: typeof FastCheck, context?: Context) => FastCheck.Arbitrary<T>
|
||||
}
|
||||
|
||||
/**
|
||||
* @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<T> = {
|
||||
export type Override<T, TypeParameters extends ReadonlyArray<Schema.Top>> = {
|
||||
readonly _tag: "Override"
|
||||
readonly override: (fc: typeof FastCheck, context?: Context) => FastCheck.Arbitrary<T>
|
||||
readonly override: (
|
||||
typeParameters: { readonly [K in keyof TypeParameters]: FastCheck.Arbitrary<TypeParameters[K]["Type"]> }
|
||||
) => (fc: typeof FastCheck, context?: Context) => FastCheck.Arbitrary<T>
|
||||
}
|
||||
}
|
||||
|
||||
@@ -146,10 +138,9 @@ const arbitraryMemoMap = new WeakMap<AST.AST, LazyArbitrary<any>>()
|
||||
function getAnnotation(
|
||||
annotations: Annotations.Annotations | undefined
|
||||
):
|
||||
| Annotation.Declaration<any, ReadonlyArray<any>>
|
||||
| Annotation.Constraint
|
||||
| Annotation.Constraints
|
||||
| Annotation.Override<any>
|
||||
| Annotation.Override<any, ReadonlyArray<any>>
|
||||
| undefined
|
||||
{
|
||||
return annotations?.arbitrary as any
|
||||
@@ -224,7 +215,7 @@ const combiner: Combiner.Combiner<any> = 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<any> => {
|
||||
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")
|
||||
|
||||
@@ -17,20 +17,12 @@ export declare namespace Annotation {
|
||||
/**
|
||||
* @since 4.0.0
|
||||
*/
|
||||
export type Declaration<T, TypeParameters extends ReadonlyArray<Schema.Top>> = {
|
||||
readonly _tag: "Declaration"
|
||||
readonly declaration: (
|
||||
export type Override<T, TypeParameters extends ReadonlyArray<Schema.Top>> = {
|
||||
readonly _tag: "Override"
|
||||
readonly override: (
|
||||
typeParameters: { readonly [K in keyof TypeParameters]: Equivalence.Equivalence<TypeParameters[K]["Type"]> }
|
||||
) => Equivalence.Equivalence<T>
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 4.0.0
|
||||
*/
|
||||
export type Override<T> = {
|
||||
readonly _tag: "Override"
|
||||
readonly override: () => Equivalence.Equivalence<T>
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -54,9 +46,7 @@ export function override<S extends Schema.Top>(override: () => Equivalence.Equiv
|
||||
}
|
||||
}
|
||||
|
||||
function getAnnotation(
|
||||
ast: AST.AST
|
||||
): Annotation.Declaration<any, ReadonlyArray<any>> | Annotation.Override<any> | undefined {
|
||||
function getAnnotation(ast: AST.AST): Annotation.Override<any, ReadonlyArray<any>> | undefined {
|
||||
return Annotations.get(ast)?.["equivalence"] as any
|
||||
}
|
||||
|
||||
@@ -66,16 +56,10 @@ const go = memoize((ast: AST.AST): Equivalence.Equivalence<any> => {
|
||||
// ---------------------------------------------
|
||||
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":
|
||||
|
||||
@@ -17,20 +17,12 @@ export declare namespace Annotation {
|
||||
/**
|
||||
* @since 4.0.0
|
||||
*/
|
||||
export type Declaration<T, TypeParameters extends ReadonlyArray<Schema.Top>> = {
|
||||
readonly _tag: "Declaration"
|
||||
readonly declaration: (
|
||||
export type Override<T, TypeParameters extends ReadonlyArray<Schema.Top>> = {
|
||||
readonly _tag: "Override"
|
||||
readonly override: (
|
||||
typeParameters: { readonly [K in keyof TypeParameters]: Format<TypeParameters[K]["Type"]> }
|
||||
) => Format<T>
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 4.0.0
|
||||
*/
|
||||
export type Override<T> = {
|
||||
readonly _tag: "Override"
|
||||
readonly override: () => Format<T>
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -47,9 +39,7 @@ export function override<S extends Schema.Top>(override: () => Format<S["Type"]>
|
||||
}
|
||||
}
|
||||
|
||||
function getAnnotation(
|
||||
ast: AST.AST
|
||||
): Annotation.Declaration<any, ReadonlyArray<any>> | Annotation.Override<any> | undefined {
|
||||
function getAnnotation(ast: AST.AST): Annotation.Override<any, ReadonlyArray<any>> | undefined {
|
||||
return Annotations.get(ast)?.["format"] as any
|
||||
}
|
||||
|
||||
@@ -66,16 +56,10 @@ export const defaultReducerAlg: AST.ReducerAlg<Format<any>> = {
|
||||
// ---------------------------------------------
|
||||
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()
|
||||
},
|
||||
|
||||
@@ -2050,9 +2050,9 @@ export interface Param<Name extends string, S extends Schema.Top> extends
|
||||
S["EncodingServices"],
|
||||
S["ast"],
|
||||
Param<Name, S>,
|
||||
S["~annotate.in"],
|
||||
S["~type.make.in"],
|
||||
S["Iso"],
|
||||
S["~type.parameters"],
|
||||
S["~type.make"],
|
||||
S["~type.mutability"],
|
||||
S["~type.optionality"],
|
||||
|
||||
@@ -83,8 +83,8 @@ export const HeadersSchema: HeadersSchema = Schema.declare(
|
||||
{
|
||||
title: "Headers",
|
||||
equivalence: {
|
||||
_tag: "Declaration",
|
||||
declaration: () => Equivalence
|
||||
_tag: "Override",
|
||||
override: () => Equivalence
|
||||
},
|
||||
defaultIsoSerializer: () =>
|
||||
Schema.link<Headers>()(
|
||||
|
||||
@@ -158,8 +158,8 @@ export const UrlParamsSchema: UrlParamsSchema = Schema.declare(
|
||||
{
|
||||
title: "UrlParams",
|
||||
equivalence: {
|
||||
_tag: "Declaration",
|
||||
declaration: () => Equivalence
|
||||
_tag: "Override",
|
||||
override: () => Equivalence
|
||||
},
|
||||
defaultIsoSerializer: () =>
|
||||
Schema.link<UrlParams>()(
|
||||
|
||||
@@ -153,9 +153,9 @@ export interface Param<Name extends string, S extends Schema.Top> extends
|
||||
S["EncodingServices"],
|
||||
S["ast"],
|
||||
Param<Name, S>,
|
||||
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<Self, Tag> extends
|
||||
never,
|
||||
AST.Declaration,
|
||||
EmptyErrorClass<Self, Tag>, // TODO: Fix this
|
||||
Annotations.Bottom<Self>
|
||||
readonly [] // TODO: Fix this
|
||||
>
|
||||
{
|
||||
new(): { readonly _tag: Tag } & YieldableError
|
||||
|
||||
@@ -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<A extends Schema.Top, E extends Schema.Top> extends
|
||||
A["DecodingServices"] | E["DecodingServices"],
|
||||
A["EncodingServices"] | E["EncodingServices"],
|
||||
AST.Declaration,
|
||||
Stream<A, E>,
|
||||
Annotations.Bottom<Stream_.Stream<A["Type"], E["Type"]>>
|
||||
Stream<A, E>
|
||||
>
|
||||
{
|
||||
readonly "~rebuild.out": this
|
||||
|
||||
@@ -221,9 +221,9 @@ export interface Class<
|
||||
S["EncodingServices"],
|
||||
AST.Declaration,
|
||||
Schema.decodeTo<Schema.declareConstructor<Self, S["Encoded"], readonly [S], S["Iso"]>, S>,
|
||||
Annotations.Bottom<Self, readonly [S]>,
|
||||
S["~type.make.in"],
|
||||
S["Iso"],
|
||||
readonly [S],
|
||||
Self,
|
||||
S["~type.mutability"],
|
||||
S["~type.optionality"],
|
||||
@@ -349,7 +349,7 @@ export const make = <
|
||||
) => <const Fields extends Struct.Fields>(
|
||||
fields: Fields & Struct.Validate<Fields, Variants[number]>,
|
||||
annotations?:
|
||||
| Annotations.Bottom<Self, readonly [Schema.Struct<ExtractFields<Default, Fields, true>>]>
|
||||
| Annotations.Declaration<Self, readonly [Schema.Struct<ExtractFields<Default, Fields, true>>]>
|
||||
| undefined
|
||||
) => [Self] extends [never] ? MissingSelfGeneric
|
||||
:
|
||||
@@ -377,7 +377,7 @@ export const make = <
|
||||
function Class<Self>(identifier: string) {
|
||||
return function(
|
||||
fields: Struct.Fields,
|
||||
annotations?: Annotations.Bottom<Self, readonly [Schema.Struct<ExtractFields<Default, typeof fields, true>>]>
|
||||
annotations?: Annotations.Declaration<Self, readonly [Schema.Struct<ExtractFields<Default, typeof fields, true>>]>
|
||||
) {
|
||||
const variantStruct = Struct(fields)
|
||||
const schema = extract(variantStruct, options.defaultVariant, {
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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(
|
||||
|
||||
@@ -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()))
|
||||
|
||||
Reference in New Issue
Block a user