123456789101112131415161718192021222324252627282930
  1. 'use strict'
  2. var schemas = require('./schemas')
  3. var ValidationError = require('./error')
  4. var validator = require('is-my-json-valid')
  5. module.exports = function (schema, data, cb) {
  6. // default value
  7. var valid = false
  8. // validator config
  9. var validate = validator(schema, {
  10. greedy: true,
  11. verbose: true,
  12. schemas: schemas
  13. })
  14. // execute is-my-json-valid
  15. if (data !== undefined) {
  16. valid = validate(data)
  17. }
  18. // callback?
  19. if (typeof cb === 'function') {
  20. return cb(validate.errors ? new ValidationError(validate.errors) : null, valid)
  21. }
  22. return valid
  23. }