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

Add allowedValues to params of error for "enum" keyword #282

Closed
brodeuralexis opened this issue Aug 18, 2016 · 3 comments
Closed

Add allowedValues to params of error for "enum" keyword #282

brodeuralexis opened this issue Aug 18, 2016 · 3 comments

Comments

@brodeuralexis
Copy link

I know that using verbose will show the enum values, but it is bloated with unnecessary information (especially for validating required properties). I have to make due with limited bandwith and processing power and do not want to waste them by enabling verbose mode (in the environment I am using, a request that fails validation in verbose mode can take up to 5 seconds for an object with 15 primitive properties).

What version of Ajv are you using? Does the issue happen if you use the latest version?

Version: 4.5.0
It is the latest version.

Ajv options object (see https://github.com/epoberezkin/ajv#options):

{
    allErrors: true,
    verbose: false
}

JSON Schema (please make it as small as possible to reproduce the issue):

{
    "type": "string",
    "enum": [
        "yes", "no"
    ]
}

Data (please make it as small as posssible to reproduce the issue):

"maybe"

Your code (please use options, schema and data as variables):

const ajv = new Ajv(options);

const valid = ajv.validate(schema, data);

if (!valid) {
    // send ajv.errors to the client
}

Error messages:

[
    {
        "keyword": "enum",
        "dataPath": "",
        "schemaPath": "#/enum",
        "params": {},
        "message": "should be equal to one of the allowed values"
    }
]

What results did you expect?

An indication about the possible enum values.

i.e.:

[
    {
        "keyword": "enum",
        "dataPath": "",
        "schemaPath": "#/enum",
        "params": {},
        "allowedValues": [
            "yes",
            "no"
        ],
        "message": "should be equal to one of the allowed values"
    }
]

Are you going to resolve the issue?

I might if the client for which I am using this library personnally requests it.

@epoberezkin
Copy link
Member

Thinking... In any case, if I add this, it should be inside params. I will need to review errors for other keywords.

@epoberezkin
Copy link
Member

@brodeuralexis given that quite a few keywords have their value (i.e. schema) available inside params already, there is no harm including the property allowedValues in params object, it would make error reporting more consistent with other keywords.

Please let me know if you could make a PR (docs have to be updated as well).

@epoberezkin epoberezkin changed the title Enum Values are not provided when data is invalid Add allowedValues to params of error for "enum" keyword Aug 28, 2016
@epoberezkin
Copy link
Member

in 4.6.0

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

No branches or pull requests

2 participants