<S extends ConstraintDecoder<unknown>>(
self: S,
options?: {
readonly leafHook?: SchemaIssue.LeafHook | undefined
readonly checkHook?: SchemaIssue.CheckHook | undefined
readonly parseOptions?: SchemaAST.ParseOptions | undefined
}
): StandardSchemaV1<S["Encoded"], S["Type"]> & SReturns a "Standard Schema" object conforming to the Standard Schema v1 specification.
Details
This function creates a schema whose validate method attempts to decode and
validate the provided input synchronously. If the underlying Schema
includes any asynchronous components (e.g., asynchronous message resolutions
or checks), then validation will necessarily return a Promise instead.
Example (Creating a standard schema from a regular schema)
import { Schema } from "effect"
// Define custom hook functions for error formatting
const leafHook = (issue: any) => {
switch (issue._tag) {
case "InvalidType":
return "Expected different type"
case "InvalidValue":
return "Invalid value provided"
case "MissingKey":
return "Required property missing"
case "UnexpectedKey":
return "Unexpected property found"
case "Forbidden":
return "Operation not allowed"
case "OneOf":
return "Multiple valid options available"
default:
return "Validation error"
}
}
// Create a standard schema from a regular schema
const PersonSchema = Schema.Struct({
name: Schema.NonEmptyString,
age: Schema.Number.check(Schema.isBetween({ minimum: 0, maximum: 150 }))
})
const standardSchema = Schema.toStandardSchemaV1(PersonSchema, {
leafHook
})
// The standard schema can be used with any Standard Schema v1 compatible library
const validResult = standardSchema["~standard"].validate({
name: "Alice",
age: 30
})
console.log(validResult) // { value: { name: "Alice", age: 30 } }
const invalidResult = standardSchema["~standard"].validate({
name: "",
age: 200
})
console.log(invalidResult) // { issues: [{ path: ["name"], message: "..." }, { path: ["age"], message: "..." }] }export function function toStandardSchemaV1<
S extends ConstraintDecoder<unknown>
>(
self: S,
options?: {
readonly leafHook?:
| SchemaIssue.LeafHook
| undefined
readonly checkHook?:
| SchemaIssue.CheckHook
| undefined
readonly parseOptions?:
| SchemaAST.ParseOptions
| undefined
}
): StandardSchemaV1<S["Encoded"], S["Type"]> & S
Returns a "Standard Schema" object conforming to the Standard Schema
v1 specification.
Details
This function creates a schema whose validate method attempts to decode and
validate the provided input synchronously. If the underlying Schema
includes any asynchronous components (e.g., asynchronous message resolutions
or checks), then validation will necessarily return a Promise instead.
Example (Creating a standard schema from a regular schema)
import { Schema } from "effect"
// Define custom hook functions for error formatting
const leafHook = (issue: any) => {
switch (issue._tag) {
case "InvalidType":
return "Expected different type"
case "InvalidValue":
return "Invalid value provided"
case "MissingKey":
return "Required property missing"
case "UnexpectedKey":
return "Unexpected property found"
case "Forbidden":
return "Operation not allowed"
case "OneOf":
return "Multiple valid options available"
default:
return "Validation error"
}
}
// Create a standard schema from a regular schema
const PersonSchema = Schema.Struct({
name: Schema.NonEmptyString,
age: Schema.Number.check(Schema.isBetween({ minimum: 0, maximum: 150 }))
})
const standardSchema = Schema.toStandardSchemaV1(PersonSchema, {
leafHook
})
// The standard schema can be used with any Standard Schema v1 compatible library
const validResult = standardSchema["~standard"].validate({
name: "Alice",
age: 30
})
console.log(validResult) // { value: { name: "Alice", age: 30 } }
const invalidResult = standardSchema["~standard"].validate({
name: "",
age: 200
})
console.log(invalidResult) // { issues: [{ path: ["name"], message: "..." }, { path: ["age"], message: "..." }] }
toStandardSchemaV1<function (type parameter) S in toStandardSchemaV1<S extends ConstraintDecoder<unknown>>(self: S, options?: {
readonly leafHook?: SchemaIssue.LeafHook | undefined;
readonly checkHook?: SchemaIssue.CheckHook | undefined;
readonly parseOptions?: SchemaAST.ParseOptions | undefined;
}): StandardSchemaV1<S["Encoded"], S["Type"]> & S
S extends interface ConstraintDecoder<out T, out RD = never>Lightweight structural constraint for APIs that need decoder type views but
do not need the full schema protocol.
When to use
Use when you need to preserve a schema's decoded type and decoding services,
but the API does not constrain the encoded type, encoding services, or call
schema methods such as annotate, check, rebuild, make, or
makeEffect.
ConstraintDecoder<unknown>>(
self: S extends ConstraintDecoder<unknown>self: function (type parameter) S in toStandardSchemaV1<S extends ConstraintDecoder<unknown>>(self: S, options?: {
readonly leafHook?: SchemaIssue.LeafHook | undefined;
readonly checkHook?: SchemaIssue.CheckHook | undefined;
readonly parseOptions?: SchemaAST.ParseOptions | undefined;
}): StandardSchemaV1<S["Encoded"], S["Type"]> & S
S,
options: {
readonly leafHook?:
| SchemaIssue.LeafHook
| undefined
readonly checkHook?:
| SchemaIssue.CheckHook
| undefined
readonly parseOptions?:
| SchemaAST.ParseOptions
| undefined
}
options?: {
readonly leafHook?: SchemaIssue.LeafHook | undefinedleafHook?: import SchemaIssueSchemaIssue.type LeafHook = (
issue: SchemaIssue.Leaf
) => string
Callback type used to format
Leaf
issues into strings.
When to use
Use when customizing how
makeFormatterStandardSchemaV1
renders
terminal issues.
LeafHook | undefined
readonly checkHook?: SchemaIssue.CheckHook | undefinedcheckHook?: import SchemaIssueSchemaIssue.type CheckHook = (
issue: SchemaIssue.Filter
) => string | undefined
Callback type used to format
Filter
issues into strings.
When to use
Use when customizing how
makeFormatterStandardSchemaV1
renders
filter failures.
Details
- Returns
string to override the message, or undefined to fall back to
the default formatting.
CheckHook | undefined
readonly parseOptions?: SchemaAST.ParseOptions | undefinedparseOptions?: import SchemaASTSchemaAST.ParseOptions | undefined
}
): import StandardSchemaV1StandardSchemaV1<function (type parameter) S in toStandardSchemaV1<S extends ConstraintDecoder<unknown>>(self: S, options?: {
readonly leafHook?: SchemaIssue.LeafHook | undefined;
readonly checkHook?: SchemaIssue.CheckHook | undefined;
readonly parseOptions?: SchemaAST.ParseOptions | undefined;
}): StandardSchemaV1<S["Encoded"], S["Type"]> & S
S["Encoded"], function (type parameter) S in toStandardSchemaV1<S extends ConstraintDecoder<unknown>>(self: S, options?: {
readonly leafHook?: SchemaIssue.LeafHook | undefined;
readonly checkHook?: SchemaIssue.CheckHook | undefined;
readonly parseOptions?: SchemaAST.ParseOptions | undefined;
}): StandardSchemaV1<S["Encoded"], S["Type"]> & S
S["Type"]> & function (type parameter) S in toStandardSchemaV1<S extends ConstraintDecoder<unknown>>(self: S, options?: {
readonly leafHook?: SchemaIssue.LeafHook | undefined;
readonly checkHook?: SchemaIssue.CheckHook | undefined;
readonly parseOptions?: SchemaAST.ParseOptions | undefined;
}): StandardSchemaV1<S["Encoded"], S["Type"]> & S
S {
const const decodeUnknownEffect: (
input: unknown,
options?: SchemaAST.ParseOptions
) => Effect.Effect<S["Type"], SchemaIssue.Issue>
decodeUnknownEffect = import SchemaParserSchemaParser.function decodeUnknownEffect<
S extends Schema.Constraint
>(
schema: S,
options?: SchemaAST.ParseOptions
): (
input: unknown,
options?: SchemaAST.ParseOptions
) => Effect.Effect<
S["Type"],
SchemaIssue.Issue,
S["DecodingServices"]
>
Creates an effectful decoder for unknown input.
When to use
Use when you need to decode untyped boundary input in an Effect whose
failure channel is SchemaIssue.Issue, while preserving transformations
and service requirements.
Details
The returned function succeeds with the schema's decoded Type or fails with a
SchemaIssue.Issue. Decoding service requirements are preserved in the returned
Effect. Parse options may be provided when creating the decoder and overridden
when applying it.
decodeUnknownEffect(self: S extends ConstraintDecoder<unknown>self) as (
input: unknowninput: unknown,
options: SchemaAST.ParseOptionsoptions?: import SchemaASTSchemaAST.ParseOptions
) => import EffectEffect.type Effect.Effect = /*unresolved*/ anyEffect<function (type parameter) S in toStandardSchemaV1<S extends ConstraintDecoder<unknown>>(self: S, options?: {
readonly leafHook?: SchemaIssue.LeafHook | undefined;
readonly checkHook?: SchemaIssue.CheckHook | undefined;
readonly parseOptions?: SchemaAST.ParseOptions | undefined;
}): StandardSchemaV1<S["Encoded"], S["Type"]> & S
S["Type"], import SchemaIssueSchemaIssue.type Issue =
| SchemaIssue.Leaf
| SchemaIssue.Filter
| SchemaIssue.Encoding
| SchemaIssue.Pointer
| SchemaIssue.Composite
| SchemaIssue.AnyOf
The root discriminated union of all validation error nodes.
When to use
Use when typing the error channel in Effect<A, Issue, R> results from
schema parsing, or when writing custom formatters or issue-tree walkers.
Details
Every node has a _tag field for pattern-matching. The union includes both
terminal
Leaf
types and composite types that wrap inner issues:
Filter
,
Encoding
,
Pointer
,
Composite
,
AnyOf
. All Issue instances have a toString() that delegates to
the default formatter, so String(issue) produces a human-readable message.
Issue>
const const parseOptions: SchemaAST.ParseOptionsconst parseOptions: {
errors: "first" | "all" | undefined;
onExcessProperty: "ignore" | "error" | "preserve" | undefined;
propertyOrder: "none" | "original" | undefined;
disableChecks: boolean | undefined;
concurrency: number | "unbounded" | undefined;
}
parseOptions: import SchemaASTSchemaAST.ParseOptions = { ParseOptions.errors?: "first" | "all" | undefinedControls how many parsing errors are reported.
Details
The default, "first", stops at the first error. Set the option to "all"
to collect every parsing error, which can help with debugging or with
presenting more complete error messages to a user.
errors: "all", ...options: {
readonly leafHook?:
| SchemaIssue.LeafHook
| undefined
readonly checkHook?:
| SchemaIssue.CheckHook
| undefined
readonly parseOptions?:
| SchemaAST.ParseOptions
| undefined
}
options?.parseOptions?: SchemaAST.ParseOptions | undefinedparseOptions }
const const formatter: SchemaIssue.Formatter<StandardSchemaV1.FailureResult>formatter = import SchemaIssueSchemaIssue.function makeFormatterStandardSchemaV1(options?: {
readonly leafHook?: LeafHook | undefined
readonly checkHook?: CheckHook | undefined
}): Formatter<StandardSchemaV1.FailureResult>
Creates a
Formatter
that produces a StandardSchemaV1.FailureResult.
When to use
Use when you need schema parse errors in
Standard Schema V1
format, optionally customizing leaf or check issue rendering.
Details
- Returns a
Formatter<StandardSchemaV1.FailureResult>.
- Each leaf issue is flattened into
{ message, path } entries.
Pointer paths are accumulated to produce full property paths.
- Falls back to
defaultLeafHook
/
defaultCheckHook
when no
hooks are provided.
Example (Creating a Standard Schema V1 formatter)
import { SchemaIssue } from "effect"
const formatter = SchemaIssue.makeFormatterStandardSchemaV1()
makeFormatterStandardSchemaV1(options: {
readonly leafHook?:
| SchemaIssue.LeafHook
| undefined
readonly checkHook?:
| SchemaIssue.CheckHook
| undefined
readonly parseOptions?:
| SchemaAST.ParseOptions
| undefined
}
options)
const const validate: StandardSchemaV1<
S["Encoded"],
S["Type"]
>
validate: import StandardSchemaV1StandardSchemaV1<function (type parameter) S in toStandardSchemaV1<S extends ConstraintDecoder<unknown>>(self: S, options?: {
readonly leafHook?: SchemaIssue.LeafHook | undefined;
readonly checkHook?: SchemaIssue.CheckHook | undefined;
readonly parseOptions?: SchemaAST.ParseOptions | undefined;
}): StandardSchemaV1<S["Encoded"], S["Type"]> & S
S["Encoded"], function (type parameter) S in toStandardSchemaV1<S extends ConstraintDecoder<unknown>>(self: S, options?: {
readonly leafHook?: SchemaIssue.LeafHook | undefined;
readonly checkHook?: SchemaIssue.CheckHook | undefined;
readonly parseOptions?: SchemaAST.ParseOptions | undefined;
}): StandardSchemaV1<S["Encoded"], S["Type"]> & S
S["Type"]>["~standard"]["validate"] = (value: unknownvalue: unknown) => {
const const scheduler: Scheduler.MixedSchedulerconst scheduler: {
executionMode: "sync" | "async";
setImmediate: (f: () => void) => () => void;
shouldYield: (fiber: Fiber.Fiber<unknown, unknown>) => boolean;
makeDispatcher: () => MixedSchedulerDispatcher;
}
scheduler = new import SchedulerScheduler.MixedScheduler()
const const fiber: Fiber<any, never>const fiber: {
id: number;
currentOpCount: number;
getRef: <A>(ref: Context.Reference<A>) => A;
context: Context.Context<never>;
setContext: (context: Context.Context<never>) => void;
currentScheduler: Scheduler;
currentDispatcher: SchedulerDispatcher;
currentSpan: AnySpan | undefined;
currentLogLevel: LogLevel;
minimumLogLevel: LogLevel;
currentStackFrame: StackFrame | undefined;
maxOpsBeforeYield: number;
currentPreventYield: boolean;
addObserver: (cb: (exit: Exit<A, E>) => void) => () => void;
interruptUnsafe: (fiberId?: number | undefined, annotations?: Context.Context<never> | undefined) => void;
pollUnsafe: () => Exit<A, E> | undefined;
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; <…;
}
fiber = import EffectEffect.runFork(
import EffectEffect.match(const decodeUnknownEffect: (
input: unknown,
options?: SchemaAST.ParseOptions
) => Effect.Effect<S["Type"], SchemaIssue.Issue>
decodeUnknownEffect(value: unknownvalue, const parseOptions: SchemaAST.ParseOptionsconst parseOptions: {
errors: "first" | "all" | undefined;
onExcessProperty: "ignore" | "error" | "preserve" | undefined;
propertyOrder: "none" | "original" | undefined;
disableChecks: boolean | undefined;
concurrency: number | "unbounded" | undefined;
}
parseOptions), {
onFailure: SchemaIssue.Formatter<StandardSchemaV1.FailureResult>onFailure: const formatter: SchemaIssue.Formatter<StandardSchemaV1.FailureResult>formatter,
onSuccess: (
value: any
) => StandardSchemaV1.Result<S["Type"]>
onSuccess: (value: anyvalue): import StandardSchemaV1StandardSchemaV1.type StandardSchemaV1.Result = /*unresolved*/ anyResult<function (type parameter) S in toStandardSchemaV1<S extends ConstraintDecoder<unknown>>(self: S, options?: {
readonly leafHook?: SchemaIssue.LeafHook | undefined;
readonly checkHook?: SchemaIssue.CheckHook | undefined;
readonly parseOptions?: SchemaAST.ParseOptions | undefined;
}): StandardSchemaV1<S["Encoded"], S["Type"]> & S
S["Type"]> => ({ value: anyvalue })
}),
{ scheduler: Scheduler.MixedScheduler(property) scheduler: {
executionMode: "sync" | "async";
setImmediate: (f: () => void) => () => void;
shouldYield: (fiber: Fiber.Fiber<unknown, unknown>) => boolean;
makeDispatcher: () => MixedSchedulerDispatcher;
}
scheduler }
)
const fiber: Fiber<any, never>const fiber: {
id: number;
currentOpCount: number;
getRef: <A>(ref: Context.Reference<A>) => A;
context: Context.Context<never>;
setContext: (context: Context.Context<never>) => void;
currentScheduler: Scheduler;
currentDispatcher: SchedulerDispatcher;
currentSpan: AnySpan | undefined;
currentLogLevel: LogLevel;
minimumLogLevel: LogLevel;
currentStackFrame: StackFrame | undefined;
maxOpsBeforeYield: number;
currentPreventYield: boolean;
addObserver: (cb: (exit: Exit<A, E>) => void) => () => void;
interruptUnsafe: (fiberId?: number | undefined, annotations?: Context.Context<never> | undefined) => void;
pollUnsafe: () => Exit<A, E> | undefined;
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; <…;
}
fiber.currentDispatcher?.flush()
const const exit: anyexit = const fiber: Fiber<any, never>const fiber: {
id: number;
currentOpCount: number;
getRef: <A>(ref: Context.Reference<A>) => A;
context: Context.Context<never>;
setContext: (context: Context.Context<never>) => void;
currentScheduler: Scheduler;
currentDispatcher: SchedulerDispatcher;
currentSpan: AnySpan | undefined;
currentLogLevel: LogLevel;
minimumLogLevel: LogLevel;
currentStackFrame: StackFrame | undefined;
maxOpsBeforeYield: number;
currentPreventYield: boolean;
addObserver: (cb: (exit: Exit<A, E>) => void) => () => void;
interruptUnsafe: (fiberId?: number | undefined, annotations?: Context.Context<never> | undefined) => void;
pollUnsafe: () => Exit<A, E> | undefined;
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; <…;
}
fiber.pollUnsafe()
if (const exit: anyexit) {
return function makeStandardResult<A>(
exit: Exit_.Exit<StandardSchemaV1.Result<A>>
): StandardSchemaV1.Result<A>
makeStandardResult(const exit: Exit_.Exit<any, never>exit)
}
return new var Promise: PromiseConstructor
new <unknown>(executor: (resolve: (value: unknown) => void, reject: (reason?: any) => void) => void) => Promise<unknown>
Creates a new Promise.
Promise((resolve: (value: unknown) => voidresolve) => {
const fiber: Fiber<any, never>const fiber: {
id: number;
currentOpCount: number;
getRef: <A>(ref: Context.Reference<A>) => A;
context: Context.Context<never>;
setContext: (context: Context.Context<never>) => void;
currentScheduler: Scheduler;
currentDispatcher: SchedulerDispatcher;
currentSpan: AnySpan | undefined;
currentLogLevel: LogLevel;
minimumLogLevel: LogLevel;
currentStackFrame: StackFrame | undefined;
maxOpsBeforeYield: number;
currentPreventYield: boolean;
addObserver: (cb: (exit: Exit<A, E>) => void) => () => void;
interruptUnsafe: (fiberId?: number | undefined, annotations?: Context.Context<never> | undefined) => void;
pollUnsafe: () => Exit<A, E> | undefined;
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; <…;
}
fiber.addObserver((exit: Exit_.Exit<any, never>exit) => {
resolve: (value: unknown) => voidresolve(function makeStandardResult<A>(
exit: Exit_.Exit<StandardSchemaV1.Result<A>>
): StandardSchemaV1.Result<A>
makeStandardResult(exit: Exit_.Exit<any, never>exit))
})
})
}
if ("~standard" in self: S extends ConstraintDecoder<unknown>self) {
const const out: anyout = self: S & Record<"~standard", unknown>self as any
if ("validate" in const out: anyout["~standard"]) return const out: anyout
var Object: ObjectConstructorProvides functionality common to all JavaScript objects.
Object.ObjectConstructor.assign<any, {
validate: StandardSchemaV1<S["Encoded"], S["Type"]>;
}>(target: any, source: {
validate: StandardSchemaV1<S["Encoded"], S["Type"]>;
}): any (+3 overloads)
Copy the values of all of the enumerable own properties from one or more source objects to a
target object. Returns the target object.
assign(const out: anyout["~standard"], { validate: StandardSchemaV1<
S["Encoded"],
S["Type"]
>
validate })
return const out: anyout
} else {
return var Object: ObjectConstructorProvides functionality common to all JavaScript objects.
Object.ObjectConstructor.assign<S, {
"~standard": {
readonly version: 1;
readonly vendor: "effect";
readonly validate: StandardSchemaV1<S["Encoded"], S["Type"]>;
};
}>(target: S, source: {
"~standard": {
readonly version: 1;
readonly vendor: "effect";
readonly validate: StandardSchemaV1<S["Encoded"], S["Type"]>;
};
}): S & {
"~standard": {
readonly version: 1;
readonly vendor: "effect";
readonly validate: StandardSchemaV1<S["Encoded"], S["Type"]>;
};
} (+3 overloads)
Copy the values of all of the enumerable own properties from one or more source objects to a
target object. Returns the target object.
assign(self: S extends ConstraintDecoder<unknown>self, {
"~standard": {
version: 1version: 1,
vendor: "effect"vendor: "effect",
validate: StandardSchemaV1<
S["Encoded"],
S["Type"]
>
validate
} as type const = {
readonly version: 1;
readonly vendor: "effect";
readonly validate: StandardSchemaV1<S["Encoded"], S["Type"]>;
}
const
})
}
}