我正在使用 AJV(JS JSON Schema Validator),并且正在尝试找到一种方法来扩展它支持的类型。
我收到此错误是因为在架构中我有一个自定义类型(我在 python 中验证的 DocumentReference - jsonschema 也是如此)
Error: schema is invalid: data.properties['allow'].properties['custom_signature'].type should be equal to one of the allowed values, data.properties['allow'].properties['custom_signature'].type[0] should be equal to one of the allowed values, data.properties['allow'].properties['custom_signature'].type should match some schema in anyOf
at Ajv.validateSchema (ajv.js?ea76:183)
at Ajv._addSchema (ajv.js?ea76:312)
at Ajv.compile (ajv.js?ea76:112)
at eval (configs.js?76ed:66)
这是架构的一个小示例:
"custom_signature": {
"type": [
"DocumentReference",
"object",
"null"
]
},
在 python jsonschema 中有一种方法可以扩展类型并定义您想要如何验证它们,AJV 中是否有一些等价物?
var json = {
"type": "object",
"properties": {
"custom_signature": {
"type": [
"DocumentReference",
"null",
"object"
]
}
}
};
const ajv = new Ajv({
allErrors: true
});
console.log(ajv);
const validate = ajv.compile(json);
console.log(validate({'custom_signature': {}}));
<script src="https://cdnjs.cloudflare.com/ajax/libs/ajv/6.4.0/ajv.min.js"></script>