Design Philosophy
Schema v4 adopts three core principles:- Lightweight by default — Only import the features you need, keeping your bundle small
- Familiar API — Naming conventions and patterns are consistent with popular validation libraries
- Explicit — You choose which features to use. Nothing is included implicitly
Key Changes
Elementary Schemas
Built-in schemas for primitives remain largely the same:Validation Rules with .check()
Validation rules are now applied with.check(...) instead of separate schema constructors:
String Validation Rules
Number Validation Rules
Struct Schemas
Struct schemas useSchema.Struct with field definitions:
Optional Fields
Optional fields are defined withSchema.optionalKey or Schema.optional:
Mutable Fields
Fields are readonly by default. UseSchema.mutableKey for mutable properties:
Decoding Defaults
Provide default values during decoding withSchema.withDecodingDefault:
Tagged Structs
Tagged structs include a_tag field for discriminated unions:
Tagged Unions
Define tagged unions with a single call:Arrays and Tuples
Records
Records define dynamic key-value mappings:Transformations
Transformations convert values between types during decoding and encoding:Deriving Schemas
Struct Derivations
Tuple Derivations
Opaque Types and Classes
Create distinct TypeScript types backed by schemas:Template Literals
Define structured string patterns:Validation and Parsing
Decoding
Encoding
Migration Strategy
When migrating Schema code from v3 to v4:- Replace constraint constructors with
.check()— ConvertSchema.minLength(5)toSchema.String.check(Schema.isMinLength(5)) - Update field definitions — Use
Schema.optionalKeyandSchema.mutableKeyfor optional/mutable fields - Explicit transformations — Use
SchemaTransformationmodule for type conversions - Tagged unions — Consider
Schema.TaggedUnionfor discriminated unions - Review error messages — v4 provides clearer validation errors
Summary
Schema v4 represents a significant evolution in Effect’s validation and transformation capabilities:- More explicit API with
.check()for validation rules - Better tree-shaking and bundle size optimization
- Clearer distinction between optional and mutable fields
- Enhanced support for tagged unions and opaque types
- More predictable transformation behavior