Nodejs 中的 AJV 验证
AJV validation in Nodejs
如果设置了 allRequired 关键字,我会收到错误消息。如果没有给出则没有错误。文档说必须将此关键字提供给对象类型。如果没有给出验证通过,即使输入错误(通过 Postman 测试)
这是架构,它是从另一个文件导出的:
const ticketSchema = {
type: 'object',
properties: {
firstName: {
type: 'string',
minLength: 1,
},
lastName: {
type: 'string',
minLength: 1,
},
ticketId: {
type: 'string',
minLength: 6,
maxLength: 6,
},
},
allRequired: true, // <---- error occurs here
};
export default ticketSchema;
错误信息:
Error: strict mode: unknown keyword: "allRequired"
验证
const ajv = new Ajv();
const validateTicket = ajv.compile(ticketSchema);
const ticket = {
'first-name': firstName,
'last-name': lastName,
'route-id': routeID,
};
const valid = validateTicket(ticket);
if (!valid) {
res.status(422).send(validateTicket.errors);
return;
}
JSON 架构中没有 allRequired
属性。我在 specification 中查找了它。它不在那里。那么,您参考了哪个文档?
据我所知没有allRequired
属性
但是,如果您想要制作所有需要的属性,您需要指定一个名为 required
的 属性。它是一个以必填字段名称作为元素的数组。因此,在您的情况下,它将是:
const ticketSchema = {
type: 'object',
properties: {
firstName: {
type: 'string',
minLength: 1,
},
lastName: {
type: 'string',
minLength: 1,
},
ticketId: {
type: 'string',
minLength: 6,
maxLength: 6,
},
},
required: ['firstName', 'lastName', 'ticketId']
};
更新:
allRequired
不是 JSON 规范的一部分。它是 ajv-keywords
模块的一部分。您需要按如下方式对其进行初始化:
const Ajv = require('ajv');
const ajv = new Ajv();
require('ajv-keywords')(ajv);
const validateTicket = ajv.compile(ticketSchema);
const ticket = {
'first-name': firstName,
'last-name': lastName,
'route-id': routeID,
};
const valid = validateTicket(ticket);
if (!valid) {
res.status(422).send(validateTicket.errors);
return;
}
更新
还有其他错误。 key
名称必须与方案中给定的名称相同,否则验证将无法进行。
示例:
const scheme = {
type: 'object',
properties: {
firstName: {
type: 'string',
},
lastName: {
type: 'string',
},
},
};
// correct - key names are the same
data = {
firstName: 'Alex',
lastName: 'Smith',
};
// incorrect - key names aren't the same
data = {
'first-name': 'Alex',
'last-name': 'Smith',
};
如果设置了 allRequired 关键字,我会收到错误消息。如果没有给出则没有错误。文档说必须将此关键字提供给对象类型。如果没有给出验证通过,即使输入错误(通过 Postman 测试)
这是架构,它是从另一个文件导出的:
const ticketSchema = {
type: 'object',
properties: {
firstName: {
type: 'string',
minLength: 1,
},
lastName: {
type: 'string',
minLength: 1,
},
ticketId: {
type: 'string',
minLength: 6,
maxLength: 6,
},
},
allRequired: true, // <---- error occurs here
};
export default ticketSchema;
错误信息:
Error: strict mode: unknown keyword: "allRequired"
验证
const ajv = new Ajv();
const validateTicket = ajv.compile(ticketSchema);
const ticket = {
'first-name': firstName,
'last-name': lastName,
'route-id': routeID,
};
const valid = validateTicket(ticket);
if (!valid) {
res.status(422).send(validateTicket.errors);
return;
}
JSON 架构中没有 allRequired
属性。我在 specification 中查找了它。它不在那里。那么,您参考了哪个文档?
据我所知没有allRequired
属性
但是,如果您想要制作所有需要的属性,您需要指定一个名为 required
的 属性。它是一个以必填字段名称作为元素的数组。因此,在您的情况下,它将是:
const ticketSchema = {
type: 'object',
properties: {
firstName: {
type: 'string',
minLength: 1,
},
lastName: {
type: 'string',
minLength: 1,
},
ticketId: {
type: 'string',
minLength: 6,
maxLength: 6,
},
},
required: ['firstName', 'lastName', 'ticketId']
};
更新:
allRequired
不是 JSON 规范的一部分。它是 ajv-keywords
模块的一部分。您需要按如下方式对其进行初始化:
const Ajv = require('ajv');
const ajv = new Ajv();
require('ajv-keywords')(ajv);
const validateTicket = ajv.compile(ticketSchema);
const ticket = {
'first-name': firstName,
'last-name': lastName,
'route-id': routeID,
};
const valid = validateTicket(ticket);
if (!valid) {
res.status(422).send(validateTicket.errors);
return;
}
更新
还有其他错误。 key
名称必须与方案中给定的名称相同,否则验证将无法进行。
示例:
const scheme = {
type: 'object',
properties: {
firstName: {
type: 'string',
},
lastName: {
type: 'string',
},
},
};
// correct - key names are the same
data = {
firstName: 'Alex',
lastName: 'Smith',
};
// incorrect - key names aren't the same
data = {
'first-name': 'Alex',
'last-name': 'Smith',
};