serializr icon indicating copy to clipboard operation
serializr copied to clipboard

deserialize doesn't throw errors if beforeDeserialize passes errors to callback

Open yifanwww opened this issue 2 years ago • 2 comments

This doc implies that in beforeDeserialize we can do callback(err) to stop the deserialization and get a throwed error, just like we do it in afterDeserialize.

However, the real behavior is different. It behaves like the field/prop doesn't exist and just skips it.

For example:

// example 1

const schema = createSimpleSchema({
    a: primitive({ beforeDeserialize: (done) => done('err') }),
    b: raw({ beforeDeserialize: (done) => done('err') }),
    c: identifier({ beforeDeserialize: (done) => done('err') }),
    d: date({ beforeDeserialize: (done) => done('err') }),
});

deserialize(schema, { a: 1, b: 1, c: 1, d: 1 });

// example 2

const schema = createSimpleSchema({
    a: object(
        createSimpleSchema({
            b: primitive({ beforeDeserialize: (done) => done('err') }),
        }),
    ),
});

deserialize(schema, { a: { b: 1 } });

We would expect that it should throw an error err, but it doesn't.

I checked the code, here it just ignores any errors from beforeDeserialize, is there any reason or is it a bug?

yifanwww avatar Sep 20 '23 14:09 yifanwww

Bug probably, until the unit test say different :)

mweststrate avatar Sep 20 '23 15:09 mweststrate

actually the error handling here is intentionally graceful. The consequence of an error is just a missing datapoint in the json, not a forcefully broken process. Otherwise it would not be possible to parse the whole dataset and identify all issues in one go.

1R053 avatar May 17 '24 21:05 1R053