Skip to content

Effect SchemaType-safe Validation & Transformation

Define once. Decode, validate, encode, and transform data with full TypeScript inference. From the creators of Effect.

Effect Schema

Quick Example ​

typescript
import { Schema } from "effect"

// Define a schema
const User = Schema.Struct({
  name: Schema.String,
  email: Schema.String.pipe(Schema.pattern(/@/)),
  age: Schema.Number.pipe(Schema.int(), Schema.between(0, 120))
})

// TypeScript infers the type automatically
type User = typeof User.Type
// { readonly name: string; readonly email: string; readonly age: number }

// Decode unknown data safely
const result = Schema.decodeUnknownSync(User)({
  name: "Alice",
  email: "alice@example.com",
  age: 30
})

// Encode back to plain objects
const encoded = Schema.encodeSync(User)(result)

Why Effect Schema? ​

Beyond Simple Validation ​

Most validation libraries stop at "is this valid?". Effect Schema goes further:

  • Decode: Transform external data into your internal types (e.g., string dates β†’ Date objects)
  • Validate: Check data already in your internal format
  • Encode: Transform internal types back to external formats (e.g., Date objects β†’ ISO strings)

The Type/Encoded Duality ​

Every schema in Effect Schema has two types:

  • Type (A): Your application's internal representation
  • Encoded (I): The external/serialized representation
typescript
import { Schema } from "effect"

// Schema<Date, string> β€” Type is Date, Encoded is string
const DateFromString = Schema.DateFromString

// Decode: string β†’ Date
Schema.decodeSync(DateFromString)("2024-01-15") // Date object

// Encode: Date β†’ string
Schema.encodeSync(DateFromString)(new Date()) // "2024-01-15T..."

This bidirectional design means you define your transformations once and get both directions automatically.

Ready to Start? ​

Released under the MIT License.