Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const user = { firstname: 'x', lastname: 'x', email: 'x', password: 'x' }
- var schema = {
- type: 'object',
- required: ['firstname', 'lastname', 'email', 'password'],
- allOf: [{
- properties: {
- firstname: { type: 'string', minLength: 2, maxLength: 35 },
- lastname: {type: 'string', minLength: 2, maxLength: 35},
- email: {type: 'string', format: 'email'},
- password: {type: 'string', minLength: 2, maxLength: 35}
- },
- additionalProperties: false
- }],
- errorMessage: {
- properties: {
- firstname: 'test custom message'
- }
- }
- };
- var validate = ajv.compile(schema);
- console.log(validate(user)); // false
- console.log(validate.errors); // processed errors
- // outputs
- [ { keyword: 'minLength',
- dataPath: '.firstname',
- schemaPath: '#/allOf/0/properties/firstname/minLength',
- params: { limit: 2 },
- message: 'should NOT be shorter than 2 characters' },
- { keyword: 'minLength',
- dataPath: '.lastname',
- schemaPath: '#/allOf/0/properties/lastname/minLength',
- params: { limit: 2 },
- message: 'should NOT be shorter than 2 characters' },
- { keyword: 'format',
- dataPath: '.email',
- schemaPath: '#/allOf/0/properties/email/format',
- params: { format: 'email' },
- message: 'should match format "email"' },
- { keyword: 'minLength',
- dataPath: '.password',
- schemaPath: '#/allOf/0/properties/password/minLength',
- params: { limit: 2 },
- message: 'should NOT be shorter than 2 characters' } ]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement