如何为json模式添加正则表达式验证,该属性是在运行时以api模式的格式创建的属性,并且只能包含所有大写/小写字母,数字,斜杠(/)和花括号{} < / p>
例如:
/ someapi / v1 / {usename}
/ someApi / v1 / {userName}
这是我的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 ???)
答案 0 :(得分:0)
您的架构有两个问题。
首先,默认情况下不固定正则表达式。您需要锚定它们。 您的第一个正则表达式未完全锚定。
第二,即使完全锚定,您也不允许其他属性。
这是更新的架构:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"additionalProperties": false,
"patternProperties": {
"^[a-zA-Z0-9{}/]$": {
"additionalProperties": false,
"type": "object",
"patternProperties": {
"^(EMP|MGR|ACCT)$": {
"additionalProperties": false,
"type": "object"
}
}
}
}
}
现在,对象的任何与正则表达式都不匹配的属性都会导致验证错误。