superstruct icon indicating copy to clipboard operation
superstruct copied to clipboard

Working with `refine` and `partial`

Open jm42 opened this issue 4 years ago • 0 comments

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)

jm42 avatar Apr 23 '21 18:04 jm42