superstruct icon indicating copy to clipboard operation
superstruct copied to clipboard

Include constraint arguments in errors

Open nblackburn opened this issue 4 years ago • 2 comments

When using refinements, I have noticed there is no way to get the arguments passed to it in the errors meaning it's impossible to recreate the StructError.message that is returned.

const confirmation = object({
    email: size(string(), 3, 254)
});

Should this fail, the error includes the min and max thresholds which is critical to the clarity of the error.

`string` expected with a length between `3` and `254` but received one with a size of 2

Being as it is not recommended to rely on these errors, I think it would be good for these to be included so they can be used in creating errors of our own.

nblackburn avatar Jan 03 '21 00:01 nblackburn

Hey @nblackburn I see what you mean, I think I'm okay with that, I'm unsure exactly how typing the errors would work, but that might be alright for now.

Would you be down to submit a PR that adds that to size as well as min/max? They can return an object { message, min, max } and { message, threshold } instead of the current message string.

ianstormtaylor avatar Jan 12 '21 02:01 ianstormtaylor

+1

ajiho avatar Jan 06 '24 16:01 ajiho