Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Defaulted is not coerced through validate #1292

Open
BuyMyBeard opened this issue Dec 19, 2024 · 2 comments
Open

Defaulted is not coerced through validate #1292

BuyMyBeard opened this issue Dec 19, 2024 · 2 comments

Comments

@BuyMyBeard
Copy link

Take the following code snippet:

const [err, data] = validate({str: 'str'}, object({str: string(), def: defaulted(string(), '')}));
console.log(err);
console.log(data);

The previous code will have this error message in the err object: StructError: At path: def -- Expected a string, but received: undefined.

Now take this code snippet instead:

create({str: 'str'}, object({str: string(), def: defaulted(string(), '')}));

This code works perfectly.

According to the tsdoc included with validate Validate a value against a struct, returning an error if invalid, or the value (with potential coercion) if valid., the value should still be coerced by defaulted, just like create.

@BuyMyBeard
Copy link
Author

Found out there is an optional parameter for coerce on validate. I will leave this open, since I think the doc comment on validate is misleading. It should be more clear that it only coerces if the coerce parameter is set to true.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants
@BuyMyBeard and others