io-ts
io-ts copied to clipboard
T.Exact applied to embedded objects
trafficstars
T.exact only strips fields of the object at the level it is applied, this makes sense as you wouldn't necessarily want it applying elsewhere.
Is it possible to create a T.deep_exact which applies to all embedded codecs down the tree?
import "mocha"
import chai from "chai"
import * as T from "io-ts"
import * as E from "fp-ts/lib/Either"
const test_base_codec = T.type({
expected: T.string
})
const test_parent_codec = T.type({
child: test_base_codec
})
const base_object = {
expected: 'as usual',
}
const base_object_with_extra_field = {
...base_object,
extra: 'will be stripped'
}
const parent_object = {
child: base_object
}
const parent_object_with_extra_field = {
child: base_object_with_extra_field
}
describe.only('io-ts', function () {
describe('exact', function () {
it('should strip extra fields from a object when decoding', function () {
const result = T.exact(test_base_codec).decode(base_object_with_extra_field)
chai.expect(result).to.deep.equal(E.right(base_object))
})
it.only('should strip extra fields from embedded objects when decoding', function () {
const result = T.deep_exact(test_parent_codec).decode(parent_object_with_extra_field)
chai.expect(result).to.deep.equal(E.right(parent_object))
})
})
})
Any suggestions on how to approach this?