我正在尝试使用 ajv 模块来验证一些输入。我让它与常规 JSON Schema 一起工作但我想验证多个路由并使用链接数据来构建文档,但我很困惑如何设置它。这是我的架构:
{
"$schema": "http://json-schema.org/draft-04/hyper-schema#",
"title": "Questions",
"type": "object",
"definitions": {
"companyId": {
"type": "string",
"minLength": 3,
"maxLength": 20
}
},
"links":[
{
"title": "List",
"href": "/questions",
"method": "POST",
"rel": "self",
"schema": {
"properties": {
"companyId": {
"$ref": "#/definitions/companyId"
}
},
"required": ["companyId"]
}
}
]
}
我的代码:
const schema = require('./schemas/questions.json');
const hyperSchema = require('../schemas/hyper-schema.json');
const Ajv = require('ajv');
const ajv = new Ajv({ allErrors: true, v5: true });
ajv.addMetaSchema(hyperSchema, undefined, true);
const validate = ajv.compile(schema);
const valid = validate(input);
console.log(valid)
我的问题是,一旦我加载了我的模式,我如何告诉 ajv 链接模式以验证什么?我将有多个具有不同输入的路由(链接)进行验证。
另外,模式设置是否正确?