Json模式验证:除模式中声明的字段外,不允许其他字段 [英] Json Schema validation: do not allow fields other than those declared in schema

查看:84
本文介绍了Json模式验证:除模式中声明的字段外,不允许其他字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有类似的模式

fname: string
lname: string
age: string

都不是必需的.用户可以向我发送以上任何属性,但没有其他没有声明的属性.他们可以通过我fnamelnameage或全部.但是,如果他们将我全部以及其他属性(例如middle_name)传递给我,则该消息将被拒绝.

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?

推荐答案

您可以创建 json-schema 并使用以下选项:

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
}

这篇关于Json模式验证:除模式中声明的字段外,不允许其他字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆