fromJsonString<S>Returns a schema that decodes a JSON string and then decodes the parsed value using the given schema.
Details
This is useful when working with JSON-encoded strings where the actual structure of the value is known and described by an existing schema.
The resulting schema first parses the input string as JSON, and then runs the provided schema on the parsed result.
JSON Schema generation:
When using fromJsonString with draft-2020-12 or openApi3.1, the
resulting schema will be a JSON Schema with a contentSchema property that
contains the JSON Schema for the given schema.
Example (Decoding JSON strings with a schema)
import { Schema } from "effect"
const schema = Schema.Struct({ a: Schema.Number })
const schemaFromJsonString = Schema.fromJsonString(schema)
Schema.decodeUnknownSync(schemaFromJsonString)(`{"a":1,"b":2}`)
// => { a: 1 }Example (Emitting JSON Schema for a JSON string decoder)
import { Schema } from "effect"
const original = Schema.Struct({ a: Schema.String })
const schema = Schema.fromJsonString(original)
const document = Schema.toJsonSchemaDocument(schema)
console.log(JSON.stringify(document, null, 2))
// {
// "source": "draft-2020-12",
// "schema": {
// "type": "string",
// "contentMediaType": "application/json",
// "contentSchema": {
// "type": "object",
// "properties": {
// "a": {
// "type": "string"
// }
// },
// "required": [
// "a"
// ],
// "additionalProperties": false
// }
// },
// "definitions": {}
// }export interface interface fromJsonString<S extends Constraint>Returns a schema that decodes a JSON string and then decodes the parsed value
using the given schema.
Details
This is useful when working with JSON-encoded strings where the actual
structure of the value is known and described by an existing schema.
The resulting schema first parses the input string as JSON, and then runs the
provided schema on the parsed result.
JSON Schema generation:
When using fromJsonString with draft-2020-12 or openApi3.1, the
resulting schema will be a JSON Schema with a contentSchema property that
contains the JSON Schema for the given schema.
Example (Decoding JSON strings with a schema)
import { Schema } from "effect"
const schema = Schema.Struct({ a: Schema.Number })
const schemaFromJsonString = Schema.fromJsonString(schema)
Schema.decodeUnknownSync(schemaFromJsonString)(`{"a":1,"b":2}`)
// => { a: 1 }
Example (Emitting JSON Schema for a JSON string decoder)
import { Schema } from "effect"
const original = Schema.Struct({ a: Schema.String })
const schema = Schema.fromJsonString(original)
const document = Schema.toJsonSchemaDocument(schema)
console.log(JSON.stringify(document, null, 2))
// {
// "source": "draft-2020-12",
// "schema": {
// "type": "string",
// "contentMediaType": "application/json",
// "contentSchema": {
// "type": "object",
// "properties": {
// "a": {
// "type": "string"
// }
// },
// "required": [
// "a"
// ],
// "additionalProperties": false
// }
// },
// "definitions": {}
// }
Type-level representation returned by
fromJsonString
.
fromJsonString<function (type parameter) S in fromJsonString<S extends Constraint>S extends Constraint> 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<function (type parameter) S in fromJsonString<S extends Constraint>S, String> {
readonly "Rebuild": interface fromJsonString<S extends Constraint>Returns a schema that decodes a JSON string and then decodes the parsed value
using the given schema.
Details
This is useful when working with JSON-encoded strings where the actual
structure of the value is known and described by an existing schema.
The resulting schema first parses the input string as JSON, and then runs the
provided schema on the parsed result.
JSON Schema generation:
When using fromJsonString with draft-2020-12 or openApi3.1, the
resulting schema will be a JSON Schema with a contentSchema property that
contains the JSON Schema for the given schema.
Example (Decoding JSON strings with a schema)
import { Schema } from "effect"
const schema = Schema.Struct({ a: Schema.Number })
const schemaFromJsonString = Schema.fromJsonString(schema)
Schema.decodeUnknownSync(schemaFromJsonString)(`{"a":1,"b":2}`)
// => { a: 1 }
Example (Emitting JSON Schema for a JSON string decoder)
import { Schema } from "effect"
const original = Schema.Struct({ a: Schema.String })
const schema = Schema.fromJsonString(original)
const document = Schema.toJsonSchemaDocument(schema)
console.log(JSON.stringify(document, null, 2))
// {
// "source": "draft-2020-12",
// "schema": {
// "type": "string",
// "contentMediaType": "application/json",
// "contentSchema": {
// "type": "object",
// "properties": {
// "a": {
// "type": "string"
// }
// },
// "required": [
// "a"
// ],
// "additionalProperties": false
// }
// },
// "definitions": {}
// }
Type-level representation returned by
fromJsonString
.
fromJsonString<function (type parameter) S in fromJsonString<S extends Constraint>S>
}
/**
* Returns a schema that decodes a JSON string and then decodes the parsed value
* using the given schema.
*
* **Details**
*
* This is useful when working with JSON-encoded strings where the actual
* structure of the value is known and described by an existing schema.
*
* The resulting schema first parses the input string as JSON, and then runs the
* provided schema on the parsed result.
*
* JSON Schema generation:
*
* When using `fromJsonString` with `draft-2020-12` or `openApi3.1`, the
* resulting schema will be a JSON Schema with a `contentSchema` property that
* contains the JSON Schema for the given schema.
*
* **Example** (Decoding JSON strings with a schema)
*
* ```ts
* import { Schema } from "effect"
*
* const schema = Schema.Struct({ a: Schema.Number })
* const schemaFromJsonString = Schema.fromJsonString(schema)
*
* Schema.decodeUnknownSync(schemaFromJsonString)(`{"a":1,"b":2}`)
* // => { a: 1 }
* ```
*
* **Example** (Emitting JSON Schema for a JSON string decoder)
*
* ```ts
* import { Schema } from "effect"
*
* const original = Schema.Struct({ a: Schema.String })
* const schema = Schema.fromJsonString(original)
*
* const document = Schema.toJsonSchemaDocument(schema)
*
* console.log(JSON.stringify(document, null, 2))
* // {
* // "source": "draft-2020-12",
* // "schema": {
* // "type": "string",
* // "contentMediaType": "application/json",
* // "contentSchema": {
* // "type": "object",
* // "properties": {
* // "a": {
* // "type": "string"
* // }
* // },
* // "required": [
* // "a"
* // ],
* // "additionalProperties": false
* // }
* // },
* // "definitions": {}
* // }
* ```
*
* @category constructors
* @since 4.0.0
*/
export function function fromJsonString<
S extends Constraint
>(schema: S): fromJsonString<S>
Returns a schema that decodes a JSON string and then decodes the parsed value
using the given schema.
Details
This is useful when working with JSON-encoded strings where the actual
structure of the value is known and described by an existing schema.
The resulting schema first parses the input string as JSON, and then runs the
provided schema on the parsed result.
JSON Schema generation:
When using fromJsonString with draft-2020-12 or openApi3.1, the
resulting schema will be a JSON Schema with a contentSchema property that
contains the JSON Schema for the given schema.
Example (Decoding JSON strings with a schema)
import { Schema } from "effect"
const schema = Schema.Struct({ a: Schema.Number })
const schemaFromJsonString = Schema.fromJsonString(schema)
Schema.decodeUnknownSync(schemaFromJsonString)(`{"a":1,"b":2}`)
// => { a: 1 }
Example (Emitting JSON Schema for a JSON string decoder)
import { Schema } from "effect"
const original = Schema.Struct({ a: Schema.String })
const schema = Schema.fromJsonString(original)
const document = Schema.toJsonSchemaDocument(schema)
console.log(JSON.stringify(document, null, 2))
// {
// "source": "draft-2020-12",
// "schema": {
// "type": "string",
// "contentMediaType": "application/json",
// "contentSchema": {
// "type": "object",
// "properties": {
// "a": {
// "type": "string"
// }
// },
// "required": [
// "a"
// ],
// "additionalProperties": false
// }
// },
// "definitions": {}
// }
fromJsonString<function (type parameter) S in fromJsonString<S extends Constraint>(schema: S): fromJsonString<S>S extends Constraint>(schema: S extends Constraintschema: function (type parameter) S in fromJsonString<S extends Constraint>(schema: S): fromJsonString<S>S): interface fromJsonString<S extends Constraint>Returns a schema that decodes a JSON string and then decodes the parsed value
using the given schema.
Details
This is useful when working with JSON-encoded strings where the actual
structure of the value is known and described by an existing schema.
The resulting schema first parses the input string as JSON, and then runs the
provided schema on the parsed result.
JSON Schema generation:
When using fromJsonString with draft-2020-12 or openApi3.1, the
resulting schema will be a JSON Schema with a contentSchema property that
contains the JSON Schema for the given schema.
Example (Decoding JSON strings with a schema)
import { Schema } from "effect"
const schema = Schema.Struct({ a: Schema.Number })
const schemaFromJsonString = Schema.fromJsonString(schema)
Schema.decodeUnknownSync(schemaFromJsonString)(`{"a":1,"b":2}`)
// => { a: 1 }
Example (Emitting JSON Schema for a JSON string decoder)
import { Schema } from "effect"
const original = Schema.Struct({ a: Schema.String })
const schema = Schema.fromJsonString(original)
const document = Schema.toJsonSchemaDocument(schema)
console.log(JSON.stringify(document, null, 2))
// {
// "source": "draft-2020-12",
// "schema": {
// "type": "string",
// "contentMediaType": "application/json",
// "contentSchema": {
// "type": "object",
// "properties": {
// "a": {
// "type": "string"
// }
// },
// "required": [
// "a"
// ],
// "additionalProperties": false
// }
// },
// "definitions": {}
// }
Type-level representation returned by
fromJsonString
.
fromJsonString<function (type parameter) S in fromJsonString<S extends Constraint>(schema: S): fromJsonString<S>S> {
const const identifier: string | undefinedidentifier = import SchemaASTSchemaAST.const resolveIdentifier: (
ast: AST
) => string | undefined
Returns the identifier annotation from the AST node, if set.
Details
The identifier is typically set by Schema.annotations({ identifier: "..." })
and is used for error messages and schema identification.
resolveIdentifier(schema: S extends Constraintschema.Constraint["ast"]: SchemaAST.ASTast)
return const String: Stringconst String: {
Rebuild: Rebuild;
Iso: Iso;
ast: Ast;
Type: T;
Encoded: E;
DecodingServices: RD;
EncodingServices: RE;
annotate: (annotations: Annotations.Bottom<string, readonly []>) => String;
annotateKey: (annotations: Annotations.Key<string>) => String;
check: (checks_0: SchemaAST.Check<string>, ...checks: Array<SchemaAST.Check<string>>) => String;
rebuild: (ast: SchemaAST.String) => String;
make: (input: string, options?: MakeOptions) => string;
makeOption: (input: string, options?: MakeOptions) => Option_.Option<string>;
makeEffect: (input: string, options?: MakeOptions) => Effect.Effect<string, 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
String
.
Schema for string values. Validates that the input is typeof "string".
String.Bottom<string, string, never, never, String, String, string, string, readonly [], string, "readonly", "required", "no-default", "readonly", "required">.annotate(annotations: Annotations.Bottom<string, readonly []>): Stringannotate({
// Give the transport wrapper its own name so the decoded payload keeps its identifier.
Annotations.Bottom<T, TypeParameters extends ReadonlyArray<Constraint>>.identifier?: string | undefinedStable identifier for this schema node.
Details
Identifiers are used by schema tooling, including JSON Schema
generation, to name references. The default formatter also uses
identifier as the expected label for type-level failures, such as
Expected UserId, got null.
identifier does not name a failed filter or refinement. If the base
type matches and a filter fails, put expected or message on the
filter/refinement instead.
identifier: const identifier: string | undefinedidentifier === var undefinedundefined ? var undefinedundefined : `${const identifier: stringidentifier}JsonString`,
Annotations.Augment.expected?: string | undefinedHuman-readable description of what a value is expected to satisfy.
Details
For filter and refinement failures, the default formatter uses
message first, then expected, and finally falls back to <filter>.
Use this to name a failed filter in the default message:
Expected <expected>, got <actual>.
expected: "a string that will be decoded as JSON",
Annotations.Augment.contentMediaType?: string | undefinedcontentMediaType: "application/json",
contentSchema: SchemaAST.ASTcontentSchema: import SchemaASTSchemaAST.const toEncoded: anyReturns the encoded (wire-format) AST by flipping and then stripping
encodings.
Details
Equivalent to toType(flip(ast)). This gives you the AST that describes
the shape of the serialized/encoded data.
- Memoized: same input reference → same output reference.
Example (Getting the encoded AST)
import { Schema, SchemaAST } from "effect"
const schema = Schema.NumberFromString
const encodedAst = SchemaAST.toEncoded(schema.ast)
console.log(encodedAst._tag) // "String"
toEncoded(schema: S extends Constraintschema.Constraint["ast"]: SchemaAST.ASTast)
}).pipe(function decodeTo<S, Constraint, never, never>(to: S, transformation: {
readonly decode: SchemaGetter.Getter<NoInfer<S["Encoded"]>, unknown, never>;
readonly encode: SchemaGetter.Getter<unknown, NoInfer<S["Encoded"]>, never>;
}): (from: Constraint) => decodeTo<S, 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(schema: S extends Constraintschema, import SchemaTransformationSchemaTransformation.const fromJsonString: SchemaTransformation.Transformation<
unknown,
string,
never,
never
>
const fromJsonString: {
_tag: 'Transformation';
decode: SchemaGetter.Getter<T, E, RD>;
encode: SchemaGetter.Getter<E, T, RE>;
flip: () => SchemaTransformation.Transformation<string, unknown, never, never>;
compose: (other: SchemaTransformation.Transformation<T2, unknown, RD2, RE2>) => SchemaTransformation.Transformation<T2, string, RD2, RE2>;
}
Decodes a JSON string with JSON.parse and encodes a value with
JSON.stringify.
When to use
Use when you need a schema transformation to decode JSON stored or
transmitted as a string, usually before composing with another schema that
validates the parsed structure.
Details
Decode fails with InvalidValue for invalid JSON, and encode can fail with
InvalidValue when JSON.stringify cannot serialize the value.
Example (Parsing JSON)
import { Schema, SchemaTransformation } from "effect"
const schema = Schema.String.pipe(
Schema.decodeTo(Schema.Unknown, SchemaTransformation.fromJsonString)
)
fromJsonString))
}