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

Optional field with regex not working #350

Closed
edwinhaver opened this issue Jan 3, 2020 · 3 comments
Closed

Optional field with regex not working #350

edwinhaver opened this issue Jan 3, 2020 · 3 comments

Comments

@edwinhaver
Copy link

I have a form with optional fields using for instance the email regEx. This works fine if you validate it and do not fill out the optional email fields but once you have filled it out and want to clear it in the next update it does of course not get cleared as empty fields are removed by default. If you set removeEmptyStrings to false then it won't pass the validation as it is not a valid email address. Does anyone know what the solution for that is if you want an empty string in the database or want the attribute removed in the database?

BTW, I am using both simpl-schema and collection2.

@aldeed
Copy link
Collaborator

aldeed commented Mar 24, 2020

@edwinhaver I think what you are saying is that there should be some option that makes an empty string be valid when regEx option is used and the field is optional. That seems useful.

There are ways that you can work around this, such as by writing your own regular expression that allows empty strings or by adjusting all the empty strings to be null before inserting/validating.

@donstephan
Copy link

I just dealt with this here and wanted to share some thoughts and a workaround. I looked into introducing this into simpl-schema but there is a test case explicitly written to not provide this functionality. Not sure what issue 409 (conflict on put?) but here's a workaround that I use to make this work.

ogImage: {
    label: "OG Image",
    type: String,
    optional: true,
    custom() {
      if (!!this.value && !SimpleSchema.RegEx.Url.test(this.value)) {
        return {
          type: SimpleSchema.ErrorTypes.FAILED_REGULAR_EXPRESSION,
          regExp: SimpleSchema.RegEx.Url.toString()
        };
      }
    },
  },

Here's the 409 test case that's in there: https://github.com/aldeed/simpl-schema/blob/d440902f75d10ab05eef8fa7d189408042ac2a54/package/lib/SimpleSchema_regEx.tests.js#L6

Happy to introduce a fix into simpl-schema but definitely going to need a bit more clarity around that 409 test case to make sure it is introduced correctly. In the time being, the workarounds are more then fine and there are plenty of approaches to making this work as is.

@aldeed aldeed closed this as completed in d85435a Apr 8, 2021
@github-actions
Copy link

github-actions bot commented Apr 8, 2021

🎉 This issue has been resolved in version 1.12.0 🎉

The release is available on:

If this makes you happy, please consider becoming a sponsor.

Your semantic-release bot 📦🚀

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

No branches or pull requests

3 participants