ajv-validator / ajv-errors

Custom error messages in JSON Schemas for Ajv validator
https://ajv.js.org
MIT License
282 stars 18 forks source link

Create different error for each validation per attribute #143

Closed Aviel212 closed 2 years ago

Aviel212 commented 2 years ago

In cases where I have a double check, FE: type: { type: 'string', minLength: 1, maxLength: 45, errorMessage: 'should be between 1 to 45' }, I want to have a different error for smaller than 1 and a different error for larger than 45.

FE in the following way - { type: 'string', validations: [ {minLength: 1, errorMessage: 'should be larger than 1' }, { maxLength: 45, errorMessage: 'should be smaller than 45' } ] }

rkoval commented 2 years ago

if i'm understanding the ask, i believe i came here wanting the same thing. after digging through the source, i realized this is possible by just defining errorMessage inline with each property/validation (i couldn't find anything in the docs explaining this). the tests in this repo house some good examples for this. e.g.: https://github.com/ajv-validator/ajv-errors/blob/7efe8c29c3ea461eef29335e0199742dbc18e791/spec/object.spec.ts#L72-L86

epoberezkin commented 2 years ago

Yes - thank you - the tests is usually the first place I also go to to see how things work with any new libraries :)

epoberezkin commented 2 years ago

Closing it.