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

multiple types + enum gives false negative #104

Closed
moeriki opened this issue Jan 24, 2016 · 2 comments
Closed

multiple types + enum gives false negative #104

moeriki opened this issue Jan 24, 2016 · 2 comments

Comments

@moeriki
Copy link

moeriki commented Jan 24, 2016

Hi all,

Code below gives a false negative on validation. Country can be null or any of the enum list. When validating null it returns an error.

var validate = validator({
    type: 'object',
    properties: {
        country: {
            type: [
                'string',
                'null',
            ],
            enum: [
                'BE',
                'NL',
            ],
        },
    },
});

validate({
    country: null,
});
[ { "field": "data.country", "message": "must be an enum value" } ]
@dmeznaric
Copy link

you just need to add null to enum values.

var validate = validator({
    type: 'object',
        properties: {
          country: {
            type: [
              'string',
              'null'
            ],
            enum: [
              'BE',
              'NL',
              null
            ]
          }
    },
});

validate({
    country: null,
});

@emilbayes
Copy link
Collaborator

emilbayes commented May 28, 2016

Based on the JSON Schema definition of enum I believe @dmeznaric suggestion to be correct.

However you could relax the definition of your country rule by removing the type. enum is strictly defined by it's array. You can see how the rule behave here

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

3 participants