20 — Schema transformations
Transforms make the encoded representation and trusted domain type explicit. V4 provides common codecs such as Schema.NumberFromString and Schema.fromJsonString; custom transformations use Schema.decodeTo with SchemaTransformation.
The exercises decode a string port into an integer and encode it back, then decode a JSON string before validating its object shape. Conversion failures remain Schema failures rather than thrown exceptions.
The full test suite
The index.test.ts for this lesson, verbatim from steps/20-schema-transformations/.
import { describe, expect, it } from "vite-plus/test"import { Schema } from "effect"
describe("Schema transformations", () => { it("keeps encoded and domain types distinct", () => { const Port = Schema.NumberFromString.pipe(Schema.check(Schema.isInt())) expect(Schema.decodeUnknownSync(Port)("8080")).toBe(8080) expect(Schema.encodeSync(Port)(8080)).toBe("8080") })
it("decodes JSON before validating its domain shape", () => { const Payload = Schema.fromJsonString(Schema.Struct({ active: Schema.Boolean })) expect(Schema.decodeUnknownSync(Payload)('{"active":true}')).toEqual({ active: true }) })})