我正在尝试使用ajv模块来验证一些输入。我让它使用常规的JSON,但我想验证多个路由,并使用链接数据构建文档,但我不知道如何设置它。下面是我的模式:
{
"$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链接什么模式来验证?我将有多个路线(链接)与不同的输入验证。
此外,架构设置是否正确?
发布于 2016-12-15 18:00:42
如果其他人需要这个,我使用了这样一个JSON指针:
const ajv = new Ajv({ allErrors: true, removeAdditional: true, v5: true });
ajv.addMetaSchema(hyperSchema, undefined, true);
ajv.addSchema(schema, 'questions.json');
const valid = ajv.validate({ $ref: 'questions.json#/links/0/schema' }, input);https://stackoverflow.com/questions/41144740
复制相似问题