validate.js 637 B

123456789101112131415161718192021222324252627282930313233
  1. const { exit } = require('./exit')
  2. // proxy to joi for option validation
  3. exports.createSchema = fn => {
  4. const joi = require('joi')
  5. let schema = fn(joi)
  6. if (typeof schema === 'object' && typeof schema.validate !== 'function') {
  7. schema = joi.object(schema)
  8. }
  9. return schema
  10. }
  11. exports.validate = (obj, schema, cb) => {
  12. const { error } = schema.validate(obj)
  13. if (error) {
  14. cb(error.details[0].message)
  15. if (process.env.VUE_CLI_TEST) {
  16. throw error
  17. } else {
  18. exit(1)
  19. }
  20. }
  21. }
  22. exports.validateSync = (obj, schema) => {
  23. const { error } = schema.validate(obj)
  24. if (error) {
  25. throw error
  26. }
  27. }