Open tohagan opened 4 years ago
Actually I think there a more functional approach that you'd probably recommend.
We just add a strictObject(decoders)
function that invokes object(decoders)
.
Here's my untested JavaScript (not TypeScript) version
const checkStrict = (decoders) => {
return new Decoder((json) => {
for (const key in json) {
if (!decoders.hasOwnProperty(key)) {
return Result.err({message: `an undefined key '${key}' is present in the object`});
}
}
return Result.ok(json);
});
}
// like object() but checks that only declared fields are used.
export const strictObject = (decoders) => object(decoders).andThen(checkStrict(decoders));
Alas .. That won't work because you've made the Decoder() constructor private.
I'm also looking for such a feature. In keeping with the library perhaps there could be an only
combinator, so a decoder could be defined as:
const decoder = only(object({a: string()})
where this decoder would throw on {a: "hello", b: "world"} but not {a: "hello world"}.
Just discovered this awesome library. Just what I was looking for! Thanks very much.
I had a look at the code for the object decoder and noted that it iterates over the fields of the fields of the object specification and returns as a result only these fields. That great for cleaning JSON input values, however I often need to check that fields in the input
json
object are only those defined by the object decoder. To support this I think we need astrict
argument on the object() decoder that checks and reports an error if any additional fields are found in thejson
object.