io-ts-excess
Version:
Decoder for io-ts to check, that object is exactly the same
25 lines • 889 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const Either_1 = require("fp-ts/lib/Either");
const t = require("io-ts");
function getExcessProps(props, r) {
const ex = [];
for (const k of Object.keys(r)) {
if (!props.hasOwnProperty(k)) {
ex.push(k);
}
}
return ex;
}
function excess(codec) {
const r = new t.InterfaceType(codec.name, codec.is, (i, c) => Either_1.either.chain(t.UnknownRecord.validate(i, c), (rec) => {
const ex = getExcessProps(codec.props, rec);
return ex.length > 0
? t.failure(i, c, `Invalid value ${JSON.stringify(i)} supplied to : ${codec.name}, excess properties: ${JSON.stringify(ex)}`)
: codec.validate(i, c);
}), codec.encode, codec.props);
return r;
}
exports.excess = excess;
exports.default = excess;
//# sourceMappingURL=index.js.map