Signature
Description
Likeobject(), but will pass through any extra fields on the input object unvalidated. These extra fields will be of unknown type statically.
This decoder validates the specified fields while preserving all other fields in the output, allowing you to access them with proper type safety (as unknown).
Type Inference
The return type includes both the validated fields and an index signature for extra fields:Parameters
| Parameter | Type | Description |
|---|---|---|
decoders | Record<string, Decoder<unknown>> | An object mapping field names to their respective decoders |
Returns
ADecoder<ObjectDecoderType<Ds> & Record<string, unknown>> that validates the specified fields and includes all extra fields as unknown.
Behavior
- Extra fields: Passed through unvalidated (type:
unknown) - Missing required fields: Causes decode failure
- Missing optional fields: Allowed
- Invalid field values: Causes decode failure with field-specific errors
- Validated fields: Have their proper inferred types
Examples
Basic Usage
Accessing Extra Fields Safely
API Response with Metadata
Partial Validation
Nested Inexact Objects
Handling Hard-Coded Keys
Error Messages
Error messages are the same asobject() since validation logic is identical:
- Missing required 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
When to Use
Useinexact() when:
- Working with evolving APIs where new fields are regularly added
- You need to preserve metadata or debug information
- Validating only critical fields while keeping the rest
- Processing responses that include extra context you might need
- Building adapters that pass through unknown fields to other systems
When Not to Use
Avoidinexact() when:
- You need all fields to be validated (use
object()with all decoders) - Extra fields should cause errors (use
exact()) - You don’t need the extra fields at all (use
object()for simpler types)
Type Safety Notes
Extra fields are typed asunknown, not any. This means:
Implementation Notes
- Builds on top of
pojousing.pipe() - Computes the union of all keys (validated + extra) from the input
- Validates specified fields through
object() - Merges validated fields with unvalidated extra fields
- Handles hard-coded keys that aren’t in the input
- Only includes fields in output if their values are not
undefined
Related Decoders
object- Likeinexact()but discards extra fieldsexact- Likeobject()but rejects extra fieldspojo- Accepts any plain object without validation
