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

allowEmpty: true doesn't work with pattern #101

Open
elmccarthy opened this issue May 11, 2015 · 0 comments
Open

allowEmpty: true doesn't work with pattern #101

elmccarthy opened this issue May 11, 2015 · 0 comments

Comments

@elmccarthy
Copy link

short version

If a property is defined with allowEmpty: true, but also has a value specified for pattern, revalidator will consider an empty value for that property to be an error. required: false works as expected with pattern.
Depending on your reading of the documentation, this might not be a bug, but I would expect allowEmpty: true to permit the empty string.

code

var revalidator = require('revalidator'),
    schema = {
        properties: {
            empty_or_re: {
                required: false,
                allowEmpty: true,
                pattern: /^ok/i
            }
        }
    };
[
    {},
    {empty_or_re: ''},
    {empty_or_re: 'okay'},
    {empty_or_re: 'nope'}
].forEach(function(form_data) {
    var validation = revalidator.validate(form_data, schema)
    console.dir({
        form_data: form_data,
        valid: validation.valid,
        errors: validation.errors
    });
});

output

{ form_data: {}, valid: true, errors: [] }
{ form_data: { empty_or_re: '' },
  valid: false,
  errors: 
   [ { attribute: 'pattern',
       property: 'empty_or_re',
       expected: /^ok/i,
       actual: '',
       message: 'invalid input' } ] }
{ form_data: { empty_or_re: 'okay' }, valid: true, errors: [] }
{ form_data: { empty_or_re: 'nope' },
  valid: false,
  errors: 
   [ { attribute: 'pattern',
       property: 'empty_or_re',
       expected: /^ok/i,
       actual: 'nope',
       message: 'invalid input' } ] }
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

1 participant