mafintosh / is-my-json-valid

A JSONSchema validator that uses code generation to be extremely fast
MIT License
964 stars 111 forks source link

Include name of the validation rule in errors? #39

Open cspotcode opened 9 years ago

cspotcode commented 9 years ago

I was thinking of using this to validate data and then show friendly errors to the user, so I'd have to map the errors from is-my-json-valid onto some user-friendly messages. I could do this if each errors object contained the name of the rule in question ("minLength", "required", etc) Is that something that could be added to the "verbose" mode?

EDIT added example:

// validator.errors
{ field: 'data.test',
    message: 'has less length than allowed',
    value: 'hello',
    rule: 'minLength' },
{ field: 'data.requiredProp',
    message: 'is required',
    value: undefined,
    rule: 'required' } ]
danieljuhl commented 9 years ago

That would be possible, yes. @mafintosh what do you think? Any reasons not to add the technical validation message?

danieljuhl commented 9 years ago

This could be made as part of #38. It would use schema instead of rule, and it would have some more information, but it should be easy to extract only the rule part of the full schema reference.