Basic Object Validation
Theobject() decoder validates that an input is a plain JavaScript object and validates its fields according to the provided schema.
Extra Fields are Ignored
By default,object() ignores extra fields that aren’t specified in the schema. Only the fields you define are validated and returned.
Exact Objects
Useexact() to reject objects with extra fields.
Inexact Objects
Useinexact() to pass through extra fields unvalidated.
unknown in TypeScript, so you’ll need to validate them separately if you want to use them.
Plain Objects (POJO)
Thepojo decoder accepts any plain JavaScript object without validating its contents.
Optional Fields
Useoptional() or nullish() for optional object fields.
Optional Fields with Defaults
Nested Objects
You can nest object decoders to validate complex structures.Adding Hardcoded Fields
Usealways() to add fields that always return a constant value, regardless of the input.
Disallowing Fields
Usenever() to explicitly reject certain fields.
Working with Records and Mappings
For objects with dynamic keys, userecord() or mapping().
Error Messages
When validation fails, decoders provide detailed error messages:Type Inference
TypeScript automatically infers the correct type from your decoder:Next Steps
- Learn about array validation for lists and tuples
- Explore transformations to modify validated data
- Add refinements for custom validation logic
