errorMessage提示语句不是定义好的
来源:1-1 课前须知,这里有你需要了解的一切

慕娘8101045
2022-04-05
我的errorMessage似乎没有生效,提示的是:应当通过 "errorMessage 关键词校验"
test代码:`var Ajv = require(‘ajv’)
const addFormats = require(‘ajv-formats’)
const localize = require(‘ajv-i18n’)
const addErrors = require(‘ajv-errors’)
var ajv = new Ajv({ allErrors: true, jsonPointers: true })
addFormats(ajv)
addErrors(ajv)
// ajv.addFormat(‘test’, (data) => {
// console.log('data :>> ', data)
// return data === ‘haha’
// })
ajv.addKeyword(‘test’, {
validate(schema, data) {
console.log('schema, data :>> ', schema, data)
if (schema) return true
else return false
},
// compile(sch, parentSchema) {
// console.log('sch,parentSchema :>> ', sch, parentSchema)
// return () => true
// },
// metaSchema: {
// type: ‘boolean’,
// },
macro() {
return {
minLength: 10,
}
},
})
const schema = {
type: ‘object’,
properties: {
foo: {
type: ‘string’,
minLength: 10,
errorMessage: {
type: ‘必须是字符串’,
minLength: ‘长度不可小于10’,
},
},
bar: {
type: ‘string’,
// test: false,
},
},
required: [‘foo’],
additionalProperties: false,
}
var validate = ajv.compile(schema)
var valid = validate({
foo: 11,
bar: ‘haha’,
})
if (!valid) {
localize.zh(validate.errors)
console.log('validate.errors :>> ', validate.errors)
}
`
1回答
-
Jokcy
2022-04-16
你装的ajv和ajv-errors是什么版本?
00
相似问题