Closed rhadaway14 closed 1 year ago
@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. I think this is because the schema casts it as a String before it is sent to the validator. So val.email or val.altEmail doesn't exist.
here is the index.js and the package.json I've simplified the code to help demo the issue index.txt package.txt
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. From my understanding, if I create a new object using the schema, it casts each attribute as whatever type is defined in the 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):