ajv这个包已经更新到7.0版本了,用法和课中6.0版本讲的不一样,会报错
来源:3-3 json-schema的fomart和自定义format

入门级全栈专家
2020-12-18
哭了,刨了一小时的坑,干前端掉头发是真的快
写回答
2回答
-
xiexin
2021-02-21
我查了官方文档,可以这样写
const Ajv = require('ajv').default const addFormats = require('ajv-formats') const schema = { type: 'object', properties: { name: { type: 'string', format: 'email', // minLength: 2, }, age: { type: 'number', }, pets: { type: 'array', }, isWorker: { type: 'boolean', }, }, required: ['name', 'age'], } const data = { name: 'sam@163.com', age: 18, pets: ['mama', 'mimi'], isWorker: true, } const ajv = new Ajv() // options can be passed, e.g. {allErrors: true} addFormats(ajv, ['email']) // list 为要在schema的json串中使用的keyword,用什么就写什么,这里之用到email就写email const validate = ajv.compile(schema) const valid = validate(data) if (!valid) console.log(validate.errors)
10 -
Jokcy
2020-12-19
啊这。。。万年不更新的ajv都在这个时间更新新版本么
032021-02-21
相似问题