<T, RE>(
codec: ConstraintCodec<T, unknown, unknown, RE>,
options?: XmlEncoderOptions
): (t: T) => Effect.Effect<string, SchemaError, RE>Derives an XML encoder from a codec.
Details
The returned function encodes a value through toCodecStringTree and returns
an Effect that succeeds with the XML string or fails with SchemaError if
codec encoding fails.
export function function toEncoderXml<T, RE>(
codec: ConstraintCodec<T, unknown, unknown, RE>,
options?: XmlEncoderOptions
): (
t: T
) => Effect.Effect<string, SchemaError, RE>
Derives an XML encoder from a codec.
Details
The returned function encodes a value through toCodecStringTree and returns
an Effect that succeeds with the XML string or fails with SchemaError if
codec encoding fails.
toEncoderXml<function (type parameter) T in toEncoderXml<T, RE>(codec: ConstraintCodec<T, unknown, unknown, RE>, options?: XmlEncoderOptions): (t: T) => Effect.Effect<string, SchemaError, RE>T, function (type parameter) RE in toEncoderXml<T, RE>(codec: ConstraintCodec<T, unknown, unknown, RE>, options?: XmlEncoderOptions): (t: T) => Effect.Effect<string, SchemaError, RE>RE>(
codec: ConstraintCodec<T, unknown, unknown, RE>(parameter) codec: {
Type: T;
Encoded: E;
DecodingServices: RD;
EncodingServices: RE;
ast: SchemaAST.AST;
Iso: unknown;
}
codec: interface ConstraintCodec<out T, out E = T, out RD = never, out RE = never>Lightweight structural constraint for APIs that need codec type views but do
not need the full schema protocol.
When to use
Use when you need to preserve decoded type, encoded type, and service
requirements for a schema value, but the API does not call schema methods
such as annotate, check, rebuild, make, or makeEffect.
ConstraintCodec<function (type parameter) T in toEncoderXml<T, RE>(codec: ConstraintCodec<T, unknown, unknown, RE>, options?: XmlEncoderOptions): (t: T) => Effect.Effect<string, SchemaError, RE>T, unknown, unknown, function (type parameter) RE in toEncoderXml<T, RE>(codec: ConstraintCodec<T, unknown, unknown, RE>, options?: XmlEncoderOptions): (t: T) => Effect.Effect<string, SchemaError, RE>RE>,
options: XmlEncoderOptions | undefinedoptions?: type XmlEncoderOptions = {
readonly rootName?: string | undefined
readonly arrayItemName?: string | undefined
readonly pretty?: boolean | undefined
readonly indent?: string | undefined
readonly sortKeys?: boolean | undefined
}
XmlEncoderOptions
) {
const const rootName: string | undefinedrootName = import InternalAnnotationsInternalAnnotations.const resolveIdentifier: (
ast: SchemaAST.AST
) => string | undefined
resolveIdentifier(codec: ConstraintCodec<T, unknown, unknown, RE>(parameter) codec: {
Type: T;
Encoded: E;
DecodingServices: RD;
EncodingServices: RE;
ast: SchemaAST.AST;
Iso: unknown;
}
codec.Constraint["ast"]: SchemaAST.ASTast) ?? import InternalAnnotationsInternalAnnotations.const resolveTitle: (
ast: SchemaAST.AST
) => string | undefined
resolveTitle(codec: ConstraintCodec<T, unknown, unknown, RE>(parameter) codec: {
Type: T;
Encoded: E;
DecodingServices: RD;
EncodingServices: RE;
ast: SchemaAST.AST;
Iso: unknown;
}
codec.Constraint["ast"]: SchemaAST.ASTast)
const const serialize: (
input: T,
options?: SchemaAST.ParseOptions
) => Effect.Effect<StringTree, SchemaError, RE>
serialize = const encodeEffect: <
S extends Constraint
>(
schema: S,
options?: SchemaAST.ParseOptions
) => (
input: S["Type"],
options?: SchemaAST.ParseOptions
) => Effect.Effect<
S["Encoded"],
SchemaError,
S["EncodingServices"]
>
Encodes a typed input (the schema's Type) against a schema, returning an
Effect that succeeds with the encoded value or fails with a
When to use
Use when you need to encode input already typed as the schema's Type in
an Effect whose failure channel is SchemaError.
Details
For unknown input use
encodeUnknownEffect
.
Options may be provided either when creating the encoder or when applying it;
application options override creation options.
encodeEffect(function toCodecStringTree<
S extends Constraint
>(schema: S): toCodecStringTree<S>
Converts a schema to the StringTree canonical codec, where every leaf value
becomes a string while preserving the original structure.
Details
Declarations are converted to undefined (unless they have a
toCodecJson or toCodec annotation).
toCodecStringTree(codec: ConstraintCodec<T, unknown, unknown, RE>(parameter) codec: {
Type: T;
Encoded: E;
DecodingServices: RD;
EncodingServices: RE;
ast: SchemaAST.AST;
Iso: unknown;
}
codec))
return (t: Tt: function (type parameter) T in toEncoderXml<T, RE>(codec: ConstraintCodec<T, unknown, unknown, RE>, options?: XmlEncoderOptions): (t: T) => Effect.Effect<string, SchemaError, RE>T): import EffectEffect.type Effect.Effect = /*unresolved*/ anyEffect<string, class SchemaErrorclass SchemaError {
message: string;
toString: () => string;
name: string;
stack: string;
cause: unknown;
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; <…;
toJSON: () => unknown;
_tag: Tag;
issue: SchemaIssue.Issue;
}
Error thrown (or returned as the error channel value) when schema decoding
or encoding fails.
Details
The issue field contains a structured
Issue
tree describing
every validation failure, including the path to the problematic value,
expected types, and actual values received. message renders the issue tree
as a human-readable string.
Use
isSchemaError
to narrow an unknown value to SchemaError.
Example (Catching a SchemaError)
import { Schema } from "effect"
try {
Schema.decodeUnknownSync(Schema.Number)("not a number")
} catch (err) {
if (Schema.isSchemaError(err)) {
console.log(err.message)
// Expected number, actual "not a number"
}
}
SchemaError, function (type parameter) RE in toEncoderXml<T, RE>(codec: ConstraintCodec<T, unknown, unknown, RE>, options?: XmlEncoderOptions): (t: T) => Effect.Effect<string, SchemaError, RE>RE> =>
const serialize: (
input: T,
options?: SchemaAST.ParseOptions
) => Effect.Effect<StringTree, SchemaError, RE>
serialize(t: Tt).pipe(import EffectEffect.map((stringTree: StringTreestringTree) => function stringTreeToXml(
value: StringTree,
options: XmlEncoderOptions
): string
stringTreeToXml(stringTree: StringTreestringTree, { rootName?: string | undefinedRoot element name for the returned XML string. Default: "root"
rootName, ...options: XmlEncoderOptions | undefinedoptions })))
}