Signature
Description
Accepts objects with fields matching the given decoders. Extra fields that exist on the input object are ignored and will not be returned in the decoded result. This is the most permissive object decoder and is useful when you want to validate specific fields but don’t care about extra properties in the input.Type Inference
The return type automatically infers the shape based on the decoder map:UndefinedToOptional
Fields that can beundefined are automatically marked as optional with a ? modifier:
Parameters
| Parameter | Type | Description |
|---|---|---|
decoders | Record<string, Decoder<unknown>> | An object mapping field names to their respective decoders |
Returns
ADecoder<ObjectDecoderType<Ds>> that validates the specified fields and returns only those fields in the decoded object.
Behavior
- Extra fields: Ignored (not included in output)
- Missing required fields: Causes decode failure
- Missing optional fields: Allowed
- Invalid field values: Causes decode failure with field-specific errors
Examples
Basic Usage
Optional Fields
Nested Objects
Error Handling
Empty Object
Error Messages
The decoder provides detailed error messages:- Missing single field:
Missing key: 'fieldName' - Missing multiple fields:
Missing keys: 'field1', 'field2' - Invalid field value: Includes the nested decoder’s error message
- Not an object:
Must be an object(from the underlyingpojodecoder)
Implementation Notes
- Built on top of the
pojodecoder using.chain() - Computes the set of known keys at decoder definition time for performance
- Treats explicit
undefinedvalues the same as missing keys - Collects all field errors before returning to provide comprehensive error messages
- Only includes fields in the output if their values are not
undefined
Related Decoders
exact- Likeobject()but rejects extra fieldsinexact- Likeobject()but passes through extra fieldspojo- Accepts any plain object without validation
