Signature
Description
Likeobject(), but will reject inputs that contain extra fields that are not specified explicitly.
Use this decoder when you want strict validation that ensures the input contains exactly the fields you expect, no more and no less.
Type Inference
The return type is identical toobject() since both return the same shape:
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 rejects any extra fields.
Behavior
- Extra fields: Causes decode failure
- Missing required fields: Causes decode failure
- Missing optional fields: Allowed
- Invalid field values: Causes decode failure with field-specific errors
Examples
Basic Usage
Multiple Extra Fields
With Optional Fields
Nested Exact Objects
API Validation
Empty Object
Error Messages
The decoder provides clear error messages:- Single extra field:
Unexpected extra keys: 'fieldName' - Multiple extra fields:
Unexpected extra keys: 'field1', 'field2' - Missing required fields: Same as
object()-Missing key: 'fieldName' - Invalid field values: Includes the nested decoder’s error message
- Not an object:
Must be an object
When to Use
Useexact() when:
- Validating API requests where extra fields might indicate client errors
- Parsing configuration files where typos should be caught
- Processing data where unexpected fields could cause bugs
- Ensuring data conforms to a strict schema
- Security is a concern and extra fields could be malicious
When Not to Use
Avoidexact() when:
- Working with evolving APIs where new fields might be added
- You want forward compatibility with future schema versions
- Processing data from external sources you don’t control
- Extra fields are harmless and can be safely ignored
object() instead.
Implementation Notes
- Checks for extra keys before running the field decoders
- Uses the
pojo.reject()method to pre-validate the key set - Defers to
object()for the actual field decoding after key validation - Computes the set of allowed keys at decoder definition time for performance
Related Decoders
object- Likeexact()but ignores extra fieldsinexact- Likeobject()but passes through extra fieldspojo- Accepts any plain object without validation
