DefectSchema for unexpected defect values represented as unknown with a JSON
encoded form.
When to use
Use when you need a schema for Cause defects or other unexpected failures
whose runtime value may be any value.
Details
The encoded side is Json. During decoding, JSON objects with a string
message property are decoded into JavaScript Error values, preserving a
non-default name and any string stack. Other JSON values decode
unchanged.
During encoding, JavaScript Error values encode to JSON objects with
name, message, and optional cause properties. Pass
{ includeStack: true } to include string stack traces in encoded Error
defects, or { excludeCause: true } to omit causes. Other values are
serialized through Effect's JSON formatter and then parsed back into JSON
when possible.
Gotchas
This schema is for carrying defects across JSON boundaries, not for preserving every JavaScript value exactly. Some values cannot round-trip unchanged:
- A non-
Errorobject such as{ message: "boom" }encodes as an error-shaped JSON object and decodes back as anError. - JSON serialization normalizes unsupported values. For example,
undefinedarray elements encode asnull, unsupported object properties are omitted, and circular references are dropped. - Values that cannot be represented as JSON fall back to Effect's formatted string representation.
export interface Defect extends interface decodeTo<To extends Constraint, From extends Constraint, RD = never, RE = never>Creates a schema that transforms from a source schema to a target schema.
When to use
Use when decoding should change the schema's decoded type or encoded shape,
with an optional custom bidirectional transformation.
Details
Call it with the target schema to and then pipe the source schema from
into the returned function. The resulting schema decodes from
From["Encoded"] to To["Type"] and encodes from To["Type"] back to
From["Encoded"].
When no transformation is provided, SchemaTransformation.passthrough() is
used, so From["Type"] must already be compatible with To["Encoded"].
The resulting schema combines decoding and encoding services from both
schemas and any custom transformation.
Gotchas
In a custom transformation, decode maps From["Type"] to To["Encoded"]
and is used on the encoding path, while encode maps To["Encoded"] to
From["Type"] and is used on the decoding path.
Example (Transforming strings to numbers with a schema transformation)
import { Schema, SchemaGetter } from "effect"
const NumberFromString = Schema.String.pipe(
Schema.decodeTo(
Schema.Number,
{
decode: SchemaGetter.transform((s) => Number(s)),
encode: SchemaGetter.transform((n) => String(n))
}
)
)
const result = Schema.decodeUnknownSync(NumberFromString)("123")
// result: 123
Type-level representation returned by
decodeTo
.
decodeTo<Unknown, typeof const Json: Codec<Json>const Json: {
Encoded: E;
DecodingServices: RD;
EncodingServices: RE;
Rebuild: Codec<T, E, RD, RE>;
Type: T;
Iso: Iso;
ast: Ast;
annotate: (annotations: Annotations.Bottom<Json, any>) => Codec<Json, Json, never, never>;
annotateKey: (annotations: Annotations.Key<Json>) => Codec<Json, Json, never, never>;
check: (checks_0: SchemaAST.Check<Json>, ...checks: Array<SchemaAST.Check<Json>>) => Codec<Json, Json, never, never>;
rebuild: (ast: SchemaAST.AST) => Codec<Json, Json, never, never>;
make: (input: unknown, options?: MakeOptions) => Json;
makeOption: (input: unknown, options?: MakeOptions) => Option_.Option<Json>;
makeEffect: (input: unknown, options?: MakeOptions) => Effect.Effect<Json, SchemaError, never>;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
}
Recursive TypeScript type for any valid immutable JSON value: null,
number, boolean, string, a readonly array of Json values, or a
readonly record of string → Json. For the corresponding schema, see the
Json
const.
Schema that accepts and validates any immutable JSON-compatible value.
Example (Validating a JSON value)
import { Schema } from "effect"
const result = Schema.decodeUnknownOption(Schema.Json)({ key: [1, true, null] })
console.log(result._tag) // "Some"
Json> {
readonly "Rebuild": Defect
}
const const defectSchemaCache: Array<
Defect | undefined
>
defectSchemaCache: interface Array<T>Array<Defect | undefined> = []
/**
* Schema for unexpected defect values represented as `unknown` with a JSON
* encoded form.
*
* **When to use**
*
* Use when you need a schema for `Cause` defects or other unexpected failures
* whose runtime value may be any value.
*
* **Details**
*
* The encoded side is {@link Json}. During decoding, JSON objects with a string
* `message` property are decoded into JavaScript `Error` values, preserving a
* non-default `name` and any string `stack`. Other JSON values decode
* unchanged.
*
* During encoding, JavaScript `Error` values encode to JSON objects with
* `name`, `message`, and optional `cause` properties. Pass
* `{ includeStack: true }` to include string stack traces in encoded `Error`
* defects, or `{ excludeCause: true }` to omit causes. Other values are
* serialized through Effect's JSON formatter and then parsed back into JSON
* when possible.
*
* **Gotchas**
*
* This schema is for carrying defects across JSON boundaries, not for
* preserving every JavaScript value exactly. Some values cannot round-trip
* unchanged:
*
* - A non-`Error` object such as `{ message: "boom" }` encodes as an
* error-shaped JSON object and decodes back as an `Error`.
* - JSON serialization normalizes unsupported values. For example,
* `undefined` array elements encode as `null`, unsupported object properties
* are omitted, and circular references are dropped.
* - Values that cannot be represented as JSON fall back to Effect's formatted
* string representation.
*
* @see {@link Error} for a schema that only accepts JavaScript `Error` values.
* @category constructors
* @since 4.0.0
*/
export function function Defect(
options?: ErrorOptions
): Defect
Schema for unexpected defect values represented as unknown with a JSON
encoded form.
When to use
Use when you need a schema for Cause defects or other unexpected failures
whose runtime value may be any value.
Details
The encoded side is
Json
. During decoding, JSON objects with a string
message property are decoded into JavaScript Error values, preserving a
non-default name and any string stack. Other JSON values decode
unchanged.
During encoding, JavaScript Error values encode to JSON objects with
name, message, and optional cause properties. Pass
{ includeStack: true } to include string stack traces in encoded Error
defects, or { excludeCause: true } to omit causes. Other values are
serialized through Effect's JSON formatter and then parsed back into JSON
when possible.
Gotchas
This schema is for carrying defects across JSON boundaries, not for
preserving every JavaScript value exactly. Some values cannot round-trip
unchanged:
- A non-
Error object such as { message: "boom" } encodes as an
error-shaped JSON object and decodes back as an Error.
- JSON serialization normalizes unsupported values. For example,
undefined array elements encode as null, unsupported object properties
are omitted, and circular references are dropped.
- Values that cannot be represented as JSON fall back to Effect's formatted
string representation.
Defect(options: ErrorOptionsoptions?: ErrorOptions): Defect {
const const key: ErrorOptionsKeykey = const getErrorOptionsKey: (
options?: ErrorOptions
) => ErrorOptionsKey
getErrorOptionsKey(options: ErrorOptionsoptions)
const const cached: Defect | undefinedcached = const defectSchemaCache: Array<
Defect | undefined
>
defectSchemaCache[const key: ErrorOptionsKeykey]
if (const cached: Defect | undefinedcached !== var undefinedundefined) {
return const cached: Defectconst cached: {
Rebuild: Defect;
Type: To["Type"];
Encoded: From["Encoded"];
DecodingServices: To["DecodingServices"] | From["DecodingServices"] | RD;
EncodingServices: To["EncodingServices"] | From["EncodingServices"] | RE;
Iso: To["Iso"];
from: From;
to: To;
ast: Ast;
annotate: (annotations: Annotations.Bottom<unknown, readonly []>) => Defect;
annotateKey: (annotations: Annotations.Key<unknown>) => Defect;
check: (checks_0: SchemaAST.Check<unknown>, ...checks: Array<SchemaAST.Check<unknown>>) => Defect;
rebuild: (ast: SchemaAST.Unknown) => Defect;
make: (input: unknown, options?: MakeOptions) => unknown;
makeOption: (input: unknown, options?: MakeOptions) => Option_.Option<unknown>;
makeEffect: (input: unknown, options?: MakeOptions) => Effect.Effect<unknown, SchemaError, never>;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
}
cached
}
const const schema: decodeTo<
Unknown,
Codec<Json, Json, never, never>,
never,
never
>
const schema: {
Type: To["Type"];
Encoded: From["Encoded"];
DecodingServices: To["DecodingServices"] | From["DecodingServices"] | RD;
EncodingServices: To["EncodingServices"] | From["EncodingServices"] | RE;
Iso: To["Iso"];
from: From;
to: To;
Rebuild: Rebuild;
ast: Ast;
annotate: (annotations: Annotations.Bottom<unknown, readonly []>) => decodeTo<Unknown, Codec<Json, Json, never, never>, never, never>;
annotateKey: (annotations: Annotations.Key<unknown>) => decodeTo<Unknown, Codec<Json, Json, never, never>, never, never>;
check: (checks_0: SchemaAST.Check<unknown>, ...checks: Array<SchemaAST.Check<unknown>>) => decodeTo<Unknown, Codec<Json, Json, never, never>, never, never>;
rebuild: (ast: SchemaAST.Unknown) => decodeTo<Unknown, Codec<Json, Json, never, never>, never, never>;
make: (input: unknown, options?: MakeOptions) => unknown;
makeOption: (input: unknown, options?: MakeOptions) => Option_.Option<unknown>;
makeEffect: (input: unknown, options?: MakeOptions) => Effect.Effect<unknown, SchemaError, never>;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
}
schema = const Json: Codec<Json>const Json: {
Encoded: E;
DecodingServices: RD;
EncodingServices: RE;
Rebuild: Codec<T, E, RD, RE>;
Type: T;
Iso: Iso;
ast: Ast;
annotate: (annotations: Annotations.Bottom<Json, any>) => Codec<Json, Json, never, never>;
annotateKey: (annotations: Annotations.Key<Json>) => Codec<Json, Json, never, never>;
check: (checks_0: SchemaAST.Check<Json>, ...checks: Array<SchemaAST.Check<Json>>) => Codec<Json, Json, never, never>;
rebuild: (ast: SchemaAST.AST) => Codec<Json, Json, never, never>;
make: (input: unknown, options?: MakeOptions) => Json;
makeOption: (input: unknown, options?: MakeOptions) => Option_.Option<Json>;
makeEffect: (input: unknown, options?: MakeOptions) => Effect.Effect<Json, SchemaError, never>;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
}
Recursive TypeScript type for any valid immutable JSON value: null,
number, boolean, string, a readonly array of Json values, or a
readonly record of string → Json. For the corresponding schema, see the
Json
const.
Schema that accepts and validates any immutable JSON-compatible value.
Example (Validating a JSON value)
import { Schema } from "effect"
const result = Schema.decodeUnknownOption(Schema.Json)({ key: [1, true, null] })
console.log(result._tag) // "Some"
Json.pipe(function decodeTo<Unknown, Constraint, never, never>(to: Unknown, transformation: {
readonly decode: SchemaGetter.Getter<unknown, unknown, never>;
readonly encode: SchemaGetter.Getter<unknown, unknown, never>;
}): (from: Constraint) => decodeTo<Unknown, Constraint, never, never> (+1 overload)
Creates a schema that transforms from a source schema to a target schema.
When to use
Use when decoding should change the schema's decoded type or encoded shape,
with an optional custom bidirectional transformation.
Details
Call it with the target schema to and then pipe the source schema from
into the returned function. The resulting schema decodes from
From["Encoded"] to To["Type"] and encodes from To["Type"] back to
From["Encoded"].
When no transformation is provided, SchemaTransformation.passthrough() is
used, so From["Type"] must already be compatible with To["Encoded"].
The resulting schema combines decoding and encoding services from both
schemas and any custom transformation.
Gotchas
In a custom transformation, decode maps From["Type"] to To["Encoded"]
and is used on the encoding path, while encode maps To["Encoded"] to
From["Type"] and is used on the decoding path.
Example (Transforming strings to numbers with a schema transformation)
import { Schema, SchemaGetter } from "effect"
const NumberFromString = Schema.String.pipe(
Schema.decodeTo(
Schema.Number,
{
decode: SchemaGetter.transform((s) => Number(s)),
encode: SchemaGetter.transform((n) => String(n))
}
)
)
const result = Schema.decodeUnknownSync(NumberFromString)("123")
// result: 123
decodeTo(const Unknown: Unknownconst Unknown: {
Rebuild: Rebuild;
Iso: Iso;
ast: Ast;
Type: T;
Encoded: E;
DecodingServices: RD;
EncodingServices: RE;
annotate: (annotations: Annotations.Bottom<unknown, readonly []>) => Unknown;
annotateKey: (annotations: Annotations.Key<unknown>) => Unknown;
check: (checks_0: SchemaAST.Check<unknown>, ...checks: Array<SchemaAST.Check<unknown>>) => Unknown;
rebuild: (ast: SchemaAST.Unknown) => Unknown;
make: (input: unknown, options?: MakeOptions) => unknown;
makeOption: (input: unknown, options?: MakeOptions) => Option_.Option<unknown>;
makeEffect: (input: unknown, options?: MakeOptions) => Effect.Effect<unknown, SchemaError, never>;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
}
Type-level representation of
Unknown
.
Schema for the unknown type. Accepts any value without validation.
When to use
Use as a top schema when you need to accept any input while preserving
TypeScript's unknown safety at use sites.
Unknown, import SchemaTransformationSchemaTransformation.const defectFromJson: (
options?: ErrorOptions
) => SchemaTransformation.Transformation<
unknown,
Json,
never,
never
>
defectFromJson(const getErrorOptions: (
key: ErrorOptionsKey
) => ErrorOptions | undefined
getErrorOptions(const key: ErrorOptionsKeykey))))
const defectSchemaCache: Array<
Defect | undefined
>
defectSchemaCache[const key: ErrorOptionsKeykey] = const schema: decodeTo<
Unknown,
Codec<Json, Json, never, never>,
never,
never
>
const schema: {
Type: To["Type"];
Encoded: From["Encoded"];
DecodingServices: To["DecodingServices"] | From["DecodingServices"] | RD;
EncodingServices: To["EncodingServices"] | From["EncodingServices"] | RE;
Iso: To["Iso"];
from: From;
to: To;
Rebuild: Rebuild;
ast: Ast;
annotate: (annotations: Annotations.Bottom<unknown, readonly []>) => decodeTo<Unknown, Codec<Json, Json, never, never>, never, never>;
annotateKey: (annotations: Annotations.Key<unknown>) => decodeTo<Unknown, Codec<Json, Json, never, never>, never, never>;
check: (checks_0: SchemaAST.Check<unknown>, ...checks: Array<SchemaAST.Check<unknown>>) => decodeTo<Unknown, Codec<Json, Json, never, never>, never, never>;
rebuild: (ast: SchemaAST.Unknown) => decodeTo<Unknown, Codec<Json, Json, never, never>, never, never>;
make: (input: unknown, options?: MakeOptions) => unknown;
makeOption: (input: unknown, options?: MakeOptions) => Option_.Option<unknown>;
makeEffect: (input: unknown, options?: MakeOptions) => Effect.Effect<unknown, SchemaError, never>;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
}
schema
return const schema: decodeTo<
Unknown,
Codec<Json, Json, never, never>,
never,
never
>
const schema: {
Type: To["Type"];
Encoded: From["Encoded"];
DecodingServices: To["DecodingServices"] | From["DecodingServices"] | RD;
EncodingServices: To["EncodingServices"] | From["EncodingServices"] | RE;
Iso: To["Iso"];
from: From;
to: To;
Rebuild: Rebuild;
ast: Ast;
annotate: (annotations: Annotations.Bottom<unknown, readonly []>) => decodeTo<Unknown, Codec<Json, Json, never, never>, never, never>;
annotateKey: (annotations: Annotations.Key<unknown>) => decodeTo<Unknown, Codec<Json, Json, never, never>, never, never>;
check: (checks_0: SchemaAST.Check<unknown>, ...checks: Array<SchemaAST.Check<unknown>>) => decodeTo<Unknown, Codec<Json, Json, never, never>, never, never>;
rebuild: (ast: SchemaAST.Unknown) => decodeTo<Unknown, Codec<Json, Json, never, never>, never, never>;
make: (input: unknown, options?: MakeOptions) => unknown;
makeOption: (input: unknown, options?: MakeOptions) => Option_.Option<unknown>;
makeEffect: (input: unknown, options?: MakeOptions) => Effect.Effect<unknown, SchemaError, never>;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
}
schema
}