diff --git a/.changeset/schema-codec-narrowing.md b/.changeset/schema-codec-narrowing.md new file mode 100644 index 0000000000..c47989842e --- /dev/null +++ b/.changeset/schema-codec-narrowing.md @@ -0,0 +1,5 @@ +--- +"effect": patch +--- + +Schema: add `Schema.Decoder` and `Schema.Encoder`, and accept simpler schema types in APIs that only decode, only encode, or only need the basic schema shape, closes #2536 diff --git a/packages/effect/src/Config.ts b/packages/effect/src/Config.ts index e22371cfb0..026b4e0500 100644 --- a/packages/effect/src/Config.ts +++ b/packages/effect/src/Config.ts @@ -639,7 +639,7 @@ const recur: ( * @category schemas * @since 4.0.0 */ -export function schema(codec: Schema.Codec, path?: string | ConfigProvider.Path): Config { +export function schema(codec: Schema.ConstraintCodec, path?: string | ConfigProvider.Path): Config { const codecStringTree = Schema.toCodecStringTree(codec) const decodeUnknownEffect = SchemaParser.decodeUnknownEffect(codecStringTree) const codecStringTreeEncoded = SchemaAST.toEncoded(codecStringTree.ast) diff --git a/packages/effect/src/Schema.ts b/packages/effect/src/Schema.ts index d09bb3d152..cf91a9f05f 100644 --- a/packages/effect/src/Schema.ts +++ b/packages/effect/src/Schema.ts @@ -888,31 +888,6 @@ export declare namespace Codec { export type EncodingServices = S extends { readonly "EncodingServices": infer R } ? R : never } -/** - * A schema that additionally supports optic (lens/prism) operations. - * - * **Details** - * - * `Optic` extends {@link Schema}`` with an `Iso` type that - * describes the isomorphic counterpart used by the optic layer. Crucially, - * decoding and encoding require *no* Effect services (`DecodingServices` and - * `EncodingServices` are both `never`), which means the optic can operate - * purely without an Effect runtime. - * - * Most primitive schemas (e.g. `Schema.String`, `Schema.Number`) implement - * `Optic` automatically. You normally interact with this interface through - * {@link Optic_} utilities rather than constructing it directly. - * - * @category models - * @since 4.0.0 - */ -export interface Optic extends Schema { - readonly "Iso": Iso - readonly "DecodingServices": never - readonly "EncodingServices": never - readonly "Rebuild": Optic -} - /** * A schema that tracks the decoded type `T`, the encoded type `E`, and the * Effect services required during decoding (`RD`) and encoding (`RE`). @@ -924,8 +899,8 @@ export interface Optic extends Schema { * Most concrete schemas produced by this module implement `Codec`. * * For APIs that only need one direction, prefer the narrower views: - * - {@link ConstraintDecoder}`` — decode-only - * - {@link ConstraintEncoder}`` — encode-only + * - {@link Decoder}`` — decode-only + * - {@link Encoder}`` — encode-only * - {@link Schema}`` — type-only (no encoded representation) * * **Example** (Accepting a codec that decodes to `number` from `string`) @@ -953,6 +928,52 @@ export interface Codec extends readonly "Rebuild": Codec } +/** + * A schema that tracks the decoded type `T` and the Effect services required + * during decoding (`RD`). + * + * **When to use** + * + * Use when you need to preserve a schema's decoded type and decoding service + * requirements, but do not need to constrain its encoded representation or + * encoding services. + * + * @see {@link Codec} for preserving both decoded and encoded type information. + * @see {@link Encoder} for the encode-only view. + * + * @category models + * @since 4.0.0 + */ +export interface Decoder extends Schema { + readonly "Encoded": unknown + readonly "DecodingServices": RD + readonly "EncodingServices": unknown + readonly "Rebuild": Decoder +} + +/** + * A schema that tracks the encoded type `E` and the Effect services required + * during encoding (`RE`). + * + * **When to use** + * + * Use when you need to preserve a schema's encoded type and encoding service + * requirements, but do not need to constrain its decoded representation or + * decoding services. + * + * @see {@link Codec} for preserving both decoded and encoded type information. + * @see {@link Decoder} for the decode-only view. + * + * @category models + * @since 4.0.0 + */ +export interface Encoder extends Schema { + readonly "Encoded": E + readonly "DecodingServices": unknown + readonly "EncodingServices": RE + readonly "Rebuild": Encoder +} + /** * Returns a codec widened to the full {@link Codec} interface, prompting * TypeScript to infer all four type parameters (`T`, `E`, `RD`, `RE`). @@ -982,6 +1003,31 @@ export function revealCodec(codec: Codec) { return codec } +/** + * A schema that additionally supports optic (lens/prism) operations. + * + * **Details** + * + * `Optic` extends {@link Schema}`` with an `Iso` type that + * describes the isomorphic counterpart used by the optic layer. Crucially, + * decoding and encoding require *no* Effect services (`DecodingServices` and + * `EncodingServices` are both `never`), which means the optic can operate + * purely without an Effect runtime. + * + * Most primitive schemas (e.g. `Schema.String`, `Schema.Number`) implement + * `Optic` automatically. You normally interact with this interface through + * {@link Optic_} utilities rather than constructing it directly. + * + * @category models + * @since 4.0.0 + */ +export interface Optic extends Schema { + readonly "Iso": Iso + readonly "DecodingServices": never + readonly "EncodingServices": never + readonly "Rebuild": Optic +} + export { /** * Error thrown (or returned as the error channel value) when schema decoding @@ -2757,7 +2803,7 @@ export declare namespace TemplateLiteralParser { */ export type Type = Parts extends readonly [infer Head, ...infer Tail] ? readonly [ Head extends TemplateLiteral.LiteralPart ? Head : - Head extends Codec ? T + Head extends ConstraintDecoder ? T : never, ...Type ] @@ -12612,7 +12658,8 @@ function getClassSchemaFactory( { identifier, [SchemaAST.ClassTypeId]: ([from]: readonly [SchemaAST.AST]) => new SchemaAST.Link(from, transformation), - toCodec: ([from]: readonly [Codec]) => new SchemaAST.Link(from.ast, transformation), + toCodec: ([from]: readonly [ConstraintCodec]) => + new SchemaAST.Link(from.ast, transformation), toArbitrary: ([from]: readonly [Annotations.ToArbitrary.TypeParameter]) => () => ({ arbitrary: from.arbitrary.map((args: S["Type"]) => new self(args)), terminal: from.terminal?.map((args: S["Type"]) => new self(args)) @@ -13594,8 +13641,8 @@ type XmlEncoderOptions = { * @category Canonical Codecs * @since 4.0.0 */ -export function toEncoderXml( - codec: Codec, +export function toEncoderXml( + codec: ConstraintCodec, options?: XmlEncoderOptions ) { const rootName = InternalAnnotations.resolveIdentifier(codec.ast) ?? InternalAnnotations.resolveTitle(codec.ast) @@ -13930,7 +13977,7 @@ export interface overrideToCodecIso extends * @since 4.0.0 */ export function overrideToCodecIso( - to: Codec, + to: ConstraintCodec, transformation: { readonly decode: SchemaGetter.Getter readonly encode: SchemaGetter.Getter @@ -13958,7 +14005,7 @@ export function overrideToCodecIso( * @category converting * @since 4.0.0 */ -export function toDifferJsonPatch(schema: Codec): Differ { +export function toDifferJsonPatch(schema: ConstraintCodec): Differ { const serializer = toCodecJson(schema) const get = SchemaParser.encodeSync(serializer) const set = SchemaParser.decodeSync(serializer) diff --git a/packages/effect/src/SchemaParser.ts b/packages/effect/src/SchemaParser.ts index d0f2b3589b..e80b84960a 100644 --- a/packages/effect/src/SchemaParser.ts +++ b/packages/effect/src/SchemaParser.ts @@ -170,8 +170,8 @@ export function make(schema: S) { * @category Asserting * @since 3.10.0 */ -export function is(schema: Schema.Schema): (input: I) => input is I & T { - return _is(schema.ast) +export function is(schema: S): (input: I) => input is I & S["Type"] { + return _is(schema.ast) } /** @internal */ diff --git a/packages/effect/src/testing/TestSchema.ts b/packages/effect/src/testing/TestSchema.ts index 95405b0e79..2a46d69937 100644 --- a/packages/effect/src/testing/TestSchema.ts +++ b/packages/effect/src/testing/TestSchema.ts @@ -167,7 +167,7 @@ export class Asserts { * * @see {@link arbitrary} for checking that generated values satisfy the schema */ - verifyLosslessTransformation>(this: Asserts, options?: { + verifyLosslessTransformation>(this: Asserts, options?: { readonly params?: FastCheck.Parameters<[S["Type"]]> }) { const decodeUnknownEffect = SchemaParser.decodeUnknownEffect(this.schema) @@ -272,7 +272,7 @@ export class Asserts { * * @see {@link verifyLosslessTransformation} for property-based round-trip checks */ - arbitrary>(this: Asserts) { + arbitrary>(this: Asserts) { const schema = this.schema return { verifyGeneration(options?: { diff --git a/packages/effect/src/unstable/ai/Chat.ts b/packages/effect/src/unstable/ai/Chat.ts index b9a491e9d6..cd43d462af 100644 --- a/packages/effect/src/unstable/ai/Chat.ts +++ b/packages/effect/src/unstable/ai/Chat.ts @@ -336,7 +336,7 @@ export interface Service { */ readonly generateObject: < ObjectEncoded extends Record, - ObjectSchema extends Schema.Codec, + ObjectSchema extends Schema.Encoder, Options extends NoExcessProperties, Options> >( options: Options & LanguageModel.GenerateObjectOptions, ObjectSchema> diff --git a/packages/effect/src/unstable/ai/LanguageModel.ts b/packages/effect/src/unstable/ai/LanguageModel.ts index f07ef9571f..8e6a0b2efd 100644 --- a/packages/effect/src/unstable/ai/LanguageModel.ts +++ b/packages/effect/src/unstable/ai/LanguageModel.ts @@ -124,7 +124,7 @@ export interface Service { */ readonly generateObject: < ObjectEncoded extends Record, - StructuredOutputSchema extends Schema.Codec, + StructuredOutputSchema extends Schema.Encoder, Options extends NoExcessProperties< GenerateObjectOptions, Options @@ -835,7 +835,7 @@ export const make: (params: { const generateObject = < ObjectEncoded extends Record, - StructuredOutputSchema extends Schema.Codec, + StructuredOutputSchema extends Schema.Encoder, Options extends NoExcessProperties< GenerateObjectOptions, Options @@ -1691,7 +1691,7 @@ export const generateText: { */ export const generateObject = < ObjectEncoded extends Record, - StructuredOutputSchema extends Schema.Codec, + StructuredOutputSchema extends Schema.Encoder, Options extends NoExcessProperties< GenerateObjectOptions, Options diff --git a/packages/effect/src/unstable/cli/Argument.ts b/packages/effect/src/unstable/cli/Argument.ts index ac10422f52..7eb40ac802 100644 --- a/packages/effect/src/unstable/cli/Argument.ts +++ b/packages/effect/src/unstable/cli/Argument.ts @@ -590,9 +590,13 @@ export const between: { * @since 4.0.0 */ export const withSchema: { - (schema: Schema.Codec): (self: Argument) => Argument - (self: Argument, schema: Schema.Codec): Argument -} = dual(2, (self: Argument, schema: Schema.Codec) => Param.withSchema(self, schema)) + (schema: Schema.ConstraintCodec): (self: Argument) => Argument + (self: Argument, schema: Schema.ConstraintCodec): Argument +} = dual( + 2, + (self: Argument, schema: Schema.ConstraintCodec): Argument => + Param.withSchema(self, schema) +) /** * Creates a positional choice argument with custom value mapping. diff --git a/packages/effect/src/unstable/cli/Flag.ts b/packages/effect/src/unstable/cli/Flag.ts index 9ca7bfd330..e4c6832207 100644 --- a/packages/effect/src/unstable/cli/Flag.ts +++ b/packages/effect/src/unstable/cli/Flag.ts @@ -982,6 +982,9 @@ export const orElseResult: { * @since 4.0.0 */ export const withSchema: { - (schema: Schema.Codec): (self: Flag) => Flag - (self: Flag, schema: Schema.Codec): Flag -} = dual(2, (self: Flag, schema: Schema.Codec) => Param.withSchema(self, schema)) + (schema: Schema.ConstraintCodec): (self: Flag) => Flag + (self: Flag, schema: Schema.ConstraintCodec): Flag +} = dual( + 2, + (self: Flag, schema: Schema.ConstraintCodec) => Param.withSchema(self, schema) +) diff --git a/packages/effect/src/unstable/cli/Param.ts b/packages/effect/src/unstable/cli/Param.ts index 1b242e10e2..642f81406e 100644 --- a/packages/effect/src/unstable/cli/Param.ts +++ b/packages/effect/src/unstable/cli/Param.ts @@ -1722,17 +1722,17 @@ export const withMetavar: { */ export const withSchema: { ( - schema: Schema.Codec + schema: Schema.ConstraintCodec ): ( self: Param ) => Param ( self: Param, - schema: Schema.Codec + schema: Schema.ConstraintCodec ): Param } = dual(2, ( self: Param, - schema: Schema.Codec + schema: Schema.ConstraintCodec ) => { const decodeParam = Schema.decodeUnknownEffect(schema) return mapEffect(self, (value) => diff --git a/packages/effect/src/unstable/cli/Primitive.ts b/packages/effect/src/unstable/cli/Primitive.ts index 8212c74ece..a4c59201f1 100644 --- a/packages/effect/src/unstable/cli/Primitive.ts +++ b/packages/effect/src/unstable/cli/Primitive.ts @@ -105,9 +105,9 @@ const makePrimitive = ( parse }) -const makeSchemaPrimitive = ( +const makeSchemaPrimitive = ( tag: string, - schema: Schema.Codec + schema: Schema.ConstraintDecoder ): Primitive => { const toCodecStringTree = Schema.toCodecStringTree(schema) const decode = Schema.decodeUnknownEffect(toCodecStringTree) diff --git a/packages/effect/src/unstable/encoding/Sse.ts b/packages/effect/src/unstable/encoding/Sse.ts index 4f87fb6f44..2adcf63227 100644 --- a/packages/effect/src/unstable/encoding/Sse.ts +++ b/packages/effect/src/unstable/encoding/Sse.ts @@ -80,15 +80,15 @@ export const decode = (): Channel.Channel< * @since 4.0.0 */ export interface EventCodec extends - Schema.Codec< + Schema.ConstraintCodec< any, { readonly id?: string | undefined readonly event?: string | undefined readonly data: string }, - any, - any + unknown, + unknown > {} diff --git a/packages/effect/src/unstable/http/HttpClientResponse.ts b/packages/effect/src/unstable/http/HttpClientResponse.ts index 4fabb07230..41eb60883d 100644 --- a/packages/effect/src/unstable/http/HttpClientResponse.ts +++ b/packages/effect/src/unstable/http/HttpClientResponse.ts @@ -93,10 +93,9 @@ export const schemaJson = < readonly headers?: Readonly> | undefined readonly body?: unknown }, - RD, - RE + RD >( - schema: Schema.Codec, + schema: Schema.ConstraintCodec, options?: ParseOptions | undefined ) => { const decode = Schema.decodeEffect(Schema.toCodecJson(schema).annotate({ options })) diff --git a/packages/effect/src/unstable/http/HttpIncomingMessage.ts b/packages/effect/src/unstable/http/HttpIncomingMessage.ts index efbb731903..c1b2f5ec37 100644 --- a/packages/effect/src/unstable/http/HttpIncomingMessage.ts +++ b/packages/effect/src/unstable/http/HttpIncomingMessage.ts @@ -78,10 +78,9 @@ export const schemaBodyJson = (schema: S, options?: export const schemaBodyUrlParams = < A, I extends Readonly | undefined>>, - RD, - RE + RD >( - schema: Schema.Codec, + schema: Schema.ConstraintCodec, options?: ParseOptions | undefined ) => { const decode = UrlParams.schemaRecord.pipe( @@ -98,8 +97,8 @@ export const schemaBodyUrlParams = < * @category schemas * @since 4.0.0 */ -export const schemaHeaders = >, RD, RE>( - schema: Schema.Codec, +export const schemaHeaders = >, RD>( + schema: Schema.ConstraintCodec, options?: ParseOptions | undefined ) => { const decode = Schema.decodeUnknownEffect(schema) diff --git a/packages/effect/src/unstable/http/HttpRouter.ts b/packages/effect/src/unstable/http/HttpRouter.ts index 3a52c4acd7..f3d2fe3d70 100644 --- a/packages/effect/src/unstable/http/HttpRouter.ts +++ b/packages/effect/src/unstable/http/HttpRouter.ts @@ -313,10 +313,9 @@ export const schemaJson = < readonly searchParams: Readonly | undefined>> readonly body: any }>, - RD, - RE + RD >( - schema: Schema.Codec, + schema: Schema.ConstraintCodec, options?: ParseOptions | undefined ): Effect.Effect< A, @@ -368,10 +367,9 @@ export const schemaNoBody = < readonly pathParams: Readonly> readonly searchParams: Readonly | undefined>> }>, - RD, - RE + RD >( - schema: Schema.Codec, + schema: Schema.ConstraintCodec, options?: ParseOptions | undefined ): Effect.Effect< A, @@ -410,8 +408,8 @@ export const schemaNoBody = < * @category schemas * @since 4.0.0 */ -export const schemaParams = | undefined>>, RD, RE>( - schema: Schema.Codec, +export const schemaParams = | undefined>>, RD>( + schema: Schema.ConstraintCodec, options?: ParseOptions | undefined ): Effect.Effect => { const parse = Schema.decodeUnknownEffect(schema) @@ -429,8 +427,8 @@ export const schemaParams = >, RD, RE>( - schema: Schema.Codec, +export const schemaPathParams = >, RD>( + schema: Schema.ConstraintCodec, options?: ParseOptions | undefined ): Effect.Effect => { const parse = Schema.decodeUnknownEffect(schema) diff --git a/packages/effect/src/unstable/http/HttpServerRequest.ts b/packages/effect/src/unstable/http/HttpServerRequest.ts index 83e6c3e6f9..d04626edef 100644 --- a/packages/effect/src/unstable/http/HttpServerRequest.ts +++ b/packages/effect/src/unstable/http/HttpServerRequest.ts @@ -191,8 +191,8 @@ export const upgradeChannel = (): Channel.Channel< * @category schemas * @since 4.0.0 */ -export const schemaCookies = >, RD, RE>( - schema: Schema.Codec, +export const schemaCookies = >, RD>( + schema: Schema.ConstraintCodec, options?: ParseOptions | undefined ): Effect.Effect => { const parse = Schema.decodeUnknownEffect(schema) @@ -205,8 +205,8 @@ export const schemaCookies = >, RD, RE>( - schema: Schema.Codec, +export const schemaHeaders = >, RD>( + schema: Schema.ConstraintCodec, options?: ParseOptions | undefined ): Effect.Effect => { const parse = Schema.decodeUnknownEffect(schema) @@ -222,10 +222,9 @@ export const schemaHeaders = | undefined>>, - RD, - RE + RD >( - schema: Schema.Codec, + schema: Schema.ConstraintCodec, options?: ParseOptions | undefined ): Effect.Effect => { const parse = Schema.decodeUnknownEffect(schema) @@ -242,8 +241,8 @@ export const schemaSearchParams = < * @category schemas * @since 4.0.0 */ -export const schemaBodyJson = ( - schema: Schema.Codec, +export const schemaBodyJson = ( + schema: Schema.ConstraintDecoder, options?: ParseOptions | undefined ): Effect.Effect => { const parse = HttpIncomingMessage.schemaBodyJson(schema, options) @@ -265,12 +264,15 @@ const isMultipart = (request: HttpServerRequest) => * @category schemas * @since 4.0.0 */ -export const schemaBodyForm = , RD, RE>( - schema: Schema.Codec, +export const schemaBodyForm = , RD>( + schema: Schema.ConstraintCodec, options?: ParseOptions | undefined ) => { const parseMultipart = Multipart.schemaPersisted(schema) - const parseUrlParams = HttpIncomingMessage.schemaBodyUrlParams(schema as Schema.Codec, options) + const parseUrlParams = HttpIncomingMessage.schemaBodyUrlParams( + schema as Schema.ConstraintCodec, + options + ) return Effect.flatMap(HttpServerRequest, (request): Effect.Effect< A, Multipart.MultipartError | Schema.SchemaError | HttpServerError, @@ -293,10 +295,9 @@ export const schemaBodyForm = , RD, RE export const schemaBodyUrlParams = < A, I extends Readonly | undefined>>, - RD, - RE + RD >( - schema: Schema.Codec, + schema: Schema.ConstraintCodec, options?: ParseOptions | undefined ): Effect.Effect => { const parse = HttpIncomingMessage.schemaBodyUrlParams(schema, options) @@ -315,8 +316,8 @@ export const schemaBodyUrlParams = < * @category schemas * @since 4.0.0 */ -export const schemaBodyMultipart = , RD, RE>( - schema: Schema.Codec, +export const schemaBodyMultipart = , RD>( + schema: Schema.ConstraintCodec, options?: ParseOptions | undefined ): Effect.Effect< A, @@ -342,8 +343,8 @@ export const schemaBodyMultipart = , R * @category schemas * @since 4.0.0 */ -export const schemaBodyFormJson = ( - schema: Schema.Codec, +export const schemaBodyFormJson = ( + schema: Schema.ConstraintDecoder, options?: ParseOptions | undefined ) => { const parseMultipart = Multipart.schemaJson(schema, options) diff --git a/packages/effect/src/unstable/http/HttpServerResponse.ts b/packages/effect/src/unstable/http/HttpServerResponse.ts index ef9d4343da..eef398bea6 100644 --- a/packages/effect/src/unstable/http/HttpServerResponse.ts +++ b/packages/effect/src/unstable/http/HttpServerResponse.ts @@ -313,8 +313,8 @@ export const json = ( * @category constructors * @since 4.0.0 */ -export const schemaJson = ( - schema: Schema.Codec, +export const schemaJson = ( + schema: Schema.ConstraintCodec, options?: ParseOptions | undefined ) => { const encode = Body.jsonSchema(schema, options) diff --git a/packages/effect/src/unstable/http/Multipart.ts b/packages/effect/src/unstable/http/Multipart.ts index 5d87fe6663..ae5fc527c4 100644 --- a/packages/effect/src/unstable/http/Multipart.ts +++ b/packages/effect/src/unstable/http/Multipart.ts @@ -333,8 +333,8 @@ export const SingleFileSchema: Schema.decodeTo, RD, RE>( - schema: Schema.Codec +export const schemaPersisted = , RD>( + schema: Schema.ConstraintCodec ): (input: unknown, options?: ParseOptions) => Effect.Effect => Schema.decodeUnknownEffect(schema) @@ -349,7 +349,7 @@ export const schemaPersisted = , RD, RE>( * @category schemas * @since 4.0.0 */ -export const schemaJson = (schema: Schema.Codec, options?: ParseOptions | undefined): { +export const schemaJson = (schema: Schema.ConstraintDecoder, options?: ParseOptions | undefined): { ( field: string ): (persisted: Persisted) => Effect.Effect diff --git a/packages/effect/src/unstable/httpapi/HttpApiClient.ts b/packages/effect/src/unstable/httpapi/HttpApiClient.ts index d1de48b839..8843cbe4d5 100644 --- a/packages/effect/src/unstable/httpapi/HttpApiClient.ts +++ b/packages/effect/src/unstable/httpapi/HttpApiClient.ts @@ -636,10 +636,10 @@ export const urlBuilder = (api: Api, options?: { const makeUrl = compilePath(endpoint.path) const encodeParams = endpoint.params === undefined ? undefined - : Schema.encodeSync(endpoint.params as Schema.Codec) + : Schema.encodeSync(endpoint.params as unknown as Schema.ConstraintEncoder) const encodeQuery = endpoint.query === undefined ? undefined - : Schema.encodeSync(endpoint.query as Schema.Codec) + : Schema.encodeSync(endpoint.query as unknown as Schema.ConstraintEncoder) const endpointBuilder = (request?: { readonly params?: unknown diff --git a/packages/effect/src/unstable/httpapi/HttpApiEndpoint.ts b/packages/effect/src/unstable/httpapi/HttpApiEndpoint.ts index 7b148ee037..f747a847d4 100644 --- a/packages/effect/src/unstable/httpapi/HttpApiEndpoint.ts +++ b/packages/effect/src/unstable/httpapi/HttpApiEndpoint.ts @@ -1037,8 +1037,8 @@ function makeProto< * @since 4.0.0 */ export type ParamsConstraint = - | Record> - | Schema.Codec, unknown, unknown> + | Record> + | Schema.Encoder, unknown> /** * Constraint for header schemas: each header must encode to `string | undefined`, @@ -1048,8 +1048,8 @@ export type ParamsConstraint = * @since 4.0.0 */ export type HeadersConstraint = - | Record> - | Schema.Codec, unknown, unknown> + | Record> + | Schema.Encoder, unknown> /** * Constraint for query schemas: each field must encode to `string`, an array of @@ -1059,8 +1059,8 @@ export type HeadersConstraint = * @since 4.0.0 */ export type QueryConstraint = - | Record | undefined, unknown, unknown>> - | Schema.Codec | undefined, unknown, unknown> + | Record | undefined, unknown>> + | Schema.Encoder | undefined, unknown> /** * Payload schema depends on the HTTP method: @@ -1075,7 +1075,7 @@ export type QueryConstraint = */ export type PayloadConstraint = Method extends HttpMethod.NoBody ? Record< string, - Schema.Codec | undefined, unknown, unknown> + Schema.Encoder | undefined, unknown> > : Schema.Top | ReadonlyArray diff --git a/packages/effect/src/unstable/httpapi/HttpApiSchema.ts b/packages/effect/src/unstable/httpapi/HttpApiSchema.ts index 622f08e01e..d08dbf8286 100644 --- a/packages/effect/src/unstable/httpapi/HttpApiSchema.ts +++ b/packages/effect/src/unstable/httpapi/HttpApiSchema.ts @@ -325,7 +325,7 @@ export interface StreamSse< * @since 4.0.0 */ export interface SseEventFromData extends - Schema.Codec< + Schema.ConstraintCodec< { readonly id: string | undefined readonly event: string diff --git a/packages/effect/src/unstable/reactivity/Atom.ts b/packages/effect/src/unstable/reactivity/Atom.ts index a7b5478c8f..52d1152155 100644 --- a/packages/effect/src/unstable/reactivity/Atom.ts +++ b/packages/effect/src/unstable/reactivity/Atom.ts @@ -2104,7 +2104,7 @@ export const refreshOnWindowFocus: >(self: A) => WithoutSeri * @category KeyValueStore * @since 4.0.0 */ -export const kvs = , const Mode extends "sync" | "async" = never>(options: { +export const kvs = , const Mode extends "sync" | "async" = never>(options: { readonly runtime: AtomRuntime readonly key: string readonly schema: S @@ -2169,9 +2169,12 @@ export const kvs = , const Mode extends "sync" * @category search params * @since 4.0.0 */ -export const searchParam = = never>(name: string, options?: { - readonly schema?: S | undefined -}): Writable<[S] extends [never] ? string : Option.Option> => { +export const searchParam = = never>( + name: string, + options?: { + readonly schema?: S | undefined + } +): Writable<[S] extends [never] ? string : Option.Option> => { const decode = options?.schema && Schema.decodeExit(options.schema) const encode = options?.schema && Schema.encodeExit(options.schema) return writable( @@ -2437,17 +2440,17 @@ export const isSerializable = (self: Atom): self is Atom & Serializabl * @since 4.0.0 */ export const serializable: { - , S extends Schema.Codec, any>>(options: { + , S extends Schema.ConstraintCodec, any>>(options: { readonly key: string readonly schema: S }): (self: R) => R & Serializable - , S extends Schema.Codec, any>>(self: R, options: { + , S extends Schema.ConstraintCodec, any>>(self: R, options: { readonly key: string readonly schema: S }): R & Serializable } = dual(2, , A, I>(self: R, options: { readonly key: string - readonly schema: Schema.Codec + readonly schema: Schema.ConstraintCodec }): R & Serializable => { const codecJson = Schema.toCodecJson(options.schema) return Object.assign(Object.create(Object.getPrototypeOf(self)), { diff --git a/packages/effect/test/schema/toArbitrary.test.ts b/packages/effect/test/schema/toArbitrary.test.ts index cb54d4e4a9..c55745aa94 100644 --- a/packages/effect/test/schema/toArbitrary.test.ts +++ b/packages/effect/test/schema/toArbitrary.test.ts @@ -7,7 +7,7 @@ function assertUnsupportedSchema(schema: Schema.Constraint, message: string) { throws(() => Schema.toArbitrary(schema), message) } -function verifyGeneration>(schema: S, numRuns?: number) { +function verifyGeneration>(schema: S, numRuns?: number) { const asserts = new TestSchema.Asserts(schema) if (numRuns === undefined) { asserts.arbitrary().verifyGeneration() diff --git a/packages/effect/typeperf/suites/schema/thresholds.json b/packages/effect/typeperf/suites/schema/thresholds.json index 2c94b228f9..6dbd0c55e1 100644 --- a/packages/effect/typeperf/suites/schema/thresholds.json +++ b/packages/effect/typeperf/suites/schema/thresholds.json @@ -45,10 +45,10 @@ "maxDelta": 117 }, "to-codec-string-tree": { - "maxDelta": 59 + "maxDelta": 57 }, "to-codec-array-from-single": { - "maxDelta": 96 + "maxDelta": 94 }, "brand": { "maxDelta": 82