Advertisement
Guest User

app.js

a guest
Aug 14th, 2018
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.52 KB | None | 0 0
  1. const user = { firstname: 'x', lastname: 'x', email: 'x', password: 'x' }
  2.  
  3. var schema = {
  4. type: 'object',
  5. required: ['firstname', 'lastname', 'email', 'password'],
  6. allOf: [{
  7. properties: {
  8. firstname: { type: 'string', minLength: 2, maxLength: 35 },
  9. lastname: {type: 'string', minLength: 2, maxLength: 35},
  10. email: {type: 'string', format: 'email'},
  11. password: {type: 'string', minLength: 2, maxLength: 35}
  12. },
  13. additionalProperties: false
  14. }],
  15. errorMessage: {
  16. properties: {
  17. firstname: 'test custom message'
  18. }
  19. }
  20. };
  21.  
  22. var validate = ajv.compile(schema);
  23. console.log(validate(user)); // false
  24. console.log(validate.errors); // processed errors
  25.  
  26.  
  27. // outputs
  28. [ { keyword: 'minLength',
  29. dataPath: '.firstname',
  30. schemaPath: '#/allOf/0/properties/firstname/minLength',
  31. params: { limit: 2 },
  32. message: 'should NOT be shorter than 2 characters' },
  33. { keyword: 'minLength',
  34. dataPath: '.lastname',
  35. schemaPath: '#/allOf/0/properties/lastname/minLength',
  36. params: { limit: 2 },
  37. message: 'should NOT be shorter than 2 characters' },
  38. { keyword: 'format',
  39. dataPath: '.email',
  40. schemaPath: '#/allOf/0/properties/email/format',
  41. params: { format: 'email' },
  42. message: 'should match format "email"' },
  43. { keyword: 'minLength',
  44. dataPath: '.password',
  45. schemaPath: '#/allOf/0/properties/password/minLength',
  46. params: { limit: 2 },
  47. message: 'should NOT be shorter than 2 characters' } ]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement