Transformation<unknown, string, never, never>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)
)export const const fromJsonString: Transformation<
unknown,
string,
never,
never
>
const fromJsonString: {
_tag: 'Transformation';
decode: SchemaGetter.Getter<T, E, RD>;
encode: SchemaGetter.Getter<E, T, RE>;
flip: () => Transformation<string, unknown, never, never>;
compose: (other: Transformation<T2, unknown, RD2, RE2>) => 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 = new constructor Transformation<unknown, string, never, never>(decode: SchemaGetter.Getter<unknown, string, never>, encode: SchemaGetter.Getter<string, unknown, never>): Transformation<unknown, string, never, never>Represents a bidirectional transformation between a decoded type T and an encoded
type E, built from a pair of Getters.
When to use
Use when you need a schema transformation that defines how a schema converts
between two representations.
- You want to compose multiple transformations into a pipeline.
- You want to flip a transformation to swap decode/encode.
Details
This is the primary building block for Schema.decodeTo, Schema.encodeTo,
Schema.decode, Schema.encode, and Schema.link. Each direction is a
SchemaGetter.Getter that handles optionality, failure, and Effect services.
- Immutable —
flip() and compose() return new instances.
flip() swaps the decode and encode getters.
compose(other) chains: this.decode then other.decode for decoding,
other.encode then this.encode for encoding.
Example (Composing two transformations)
import { SchemaTransformation } from "effect"
const trimAndLower = SchemaTransformation.trim().compose(
SchemaTransformation.toLowerCase()
)
// decode: trim then lowercase
// encode: passthrough (both directions)
Transformation<unknown, string>(
import SchemaGetterSchemaGetter.parseJson(),
import SchemaGetterSchemaGetter.stringifyJson()
)