Json Schema validation: do not allow fields other than those declared in schema [duplicate]

喜你入骨 提交于 2020-11-30 16:45:54

问题


Suppose that I have schema like

fname: string
lname: string
age: string

None of them are required. User can send me any of those attributes above but nothing else that is not declared. They can pass me fname, lname and age or all. But if they pass me all and additional property like middle_name the message should be rejected.

How would I define a schema like this?


回答1:


You can create a json-schema and use the option:

additionalProperties = false

That way you only allow the attributes defined in properties. In your case:

{
    "properties": {
        "fname": {"type": "string"},
        "lname": {"type": "string"},
        "age": {"type": "string"}
    },
    "additionalProperties": false
}


来源:https://stackoverflow.com/questions/27224310/json-schema-validation-do-not-allow-fields-other-than-those-declared-in-schema

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!