superstruct
superstruct copied to clipboard
Working with `refine` and `partial`
Hello. I found that when refine comes before partial it stop working, the other way around works as expected.
const { refine, partial, object, date, string, mask } = require('superstruct')
const Model = object({
start: date(),
end: date(),
otherProp: string(),
})
function dateIntegrity(struct) {
return refine(struct, 'date integrity', (obj) => {
if (obj.start < new Date()) {
return 'Start is in the past'
}
if (obj.end < obj.start) {
return 'End is before start'
}
return true
})
}
const invalidObject = {
start: new Date('2021-10-11 16:00:00'),
end: new Date('2021-10-11 14:00:00'),
}
// Does NOT call the data integrity validation
const data0 = mask(invalidObject, partial(dateIntegrity(Model)))
console.log(data0)
// Works as expected
const data1 = mask(invalidObject, dateIntegrity(partial(Model)))
console.log(data1)