[英]JS Json Schema - AJV
我正在使用AJV(JS JSON架構驗證器),並且試圖找到一種擴展其支持的類型的方法。
我收到此錯誤的原因是,在架構中,我有一個自定義類型(我也在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>
我只是制作了一個模塊來簡化一些AJV問題。 它還包括一個名為.addType()的新函數:
GitHub: https : //github.com/webarthur/super-ajv
NPM: https : //www.npmjs.com/package/super-ajv
const ajv = new Ajv()
ajv.addType('mongoid', {
compile: function () {
return function (data) {
const re = /^(?=[a-f\d]{24}$)(\d+[a-f]|[a-f]+\d)/i
return re.test(data)
}
}
})
const schema = {
properties: {
user_id: { type: 'mongoid' }
}
}
你也可以:
const schema = {
properties: {
'*name': 'string',
'*email': 'email',
'age': 'number',
'*message': 'string',
}
}
請享用!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.