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
schemas and validations with optional fields #724
Comments
@rhadaway14, does this fixes your problem? const email = (val) => {
const emailRegex: RegExp = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
if (!emailRegex.test(val.email)) {
// email is not correct
return false;
}
if (val.altEmail) {
// altEmail is not correct
return emailRegex.test(val.altEmail);
}
return true;
}; |
No that doesn't work. The value being passed to the email validator is a String. |
here is the index.js and the package.json |
then, maybe all you need is to parse string as JSON if it is stringified document body |
I don't think that that is how it was designed to be used. const UserSchema = new Schema({ |
hi @rhadaway14 I created a PR already to solve this issue. When merged and released your code should work as expected. This was a bug due to we didn't exclude undefined, then it was being parsed as string, the above PR fix it. Also, a couple of tests were added to ensure the correct behavior. Thank you for your feedbacks |
did that PR ever get released? it looks like it is still pending merge? I'm not trying to be irritating. |
Hey @rhadaway14 apologies for the delay. We've released v2.3.4 that includes this change. Thanks so much for your patience, and for being a part of the Ottoman community. |
I have a customer who is trying to have an optional field in his schema(altEmail) that still must pass a validator when present.
When saving a document with a correct email and correct alt email, the doc is written.
When attempting to save a document with a correct email and no altEmail, it fails, as the value undefined becomes the string 'undefined' and fails the email validation.
Here is some sample code(you will need to update the username, password, scope, collection, bucket, and connection endpoints):
The text was updated successfully, but these errors were encountered: