【发布时间】:2019-12-27 19:23:02
【问题描述】:
如何为 json 模式添加正则表达式验证,其中属性是动态创建的,格式为 api 模式,并且只能包含所有大写/小写字母、数字、斜杠 (/) 和花括号 {}
例如:
/someapi/v1/{用户名}
/someApi/v1/{用户名}
这是我的 Uschema:
{
"type": "object",
"patternProperties": {
"^[a-zA-Z0-9{}/]": {
"additionalProperties": false,
"type": "object",
"patternProperties": {
"^(EMP|MGR|ACCT)$": {
"additionalProperties": false,
"type": "object"
}
}
}
}
}
这里是 JSON 和使用 ajv(规范 7)的结果
我的 JSON 示例:
{"/employee/v1/{empId}":{"EMP":{}}} - PASS
{"/employee/v1/{empId}":{"E1MP":{}}} - FAIL (I can understand as E1MP is there)
{"/employee/v1/{empId}/<fooBar>":{"E1MP":{}}} - FAIL
(I can understand as E1MP is there but didn't complain about < >
brackets as it was the first entry to validate)
{"/employee/v1/{empId}/<fooBar>":{"EMP":{}}} - PASS (?)
(Actually it should FAIL ???, Why it is considering the < > brackets though I have the regex in place for the outer parent property.)
此外,如果我调整外部正则表达式以验证任何空白空间,例如:^[a-zA-Z0-9{}/\s],它不会抱怨任何空间错误:
{"/emp loye e/v1/{empI d}/{f ooBar}":{"EMP":{}}} -- PASS? (Actually it shoud FAIL ???)
【问题讨论】:
-
你有正确的方法,但你错过了如何应用子模式。今晚我会尽力为您提供解决方案=]
-
感谢您的回复。同时,我应该如何理解您的评论“如何应用子模式”?
-
我已更新以提供更清晰的信息。给您带来不便,敬请见谅
-
随着您更新的问题,我的评论不再适用。
标签: json jsonschema json-schema-validator ajv