2017-05-15 10:52
浏览 576


Golang JSON schema validation libraries validate that required fields on the schema are present in the service request/response.

I need to validate that any field in a service request or response must be a property on the schema. If a property in the payload does not exist in the schema, the validation should fail.

For example: a GET response:

   "pet": "dog",
   "name": "Scooby",
   "licence": "123-123"

In my sample JSON schema, none of the fields are required. However, if I changed the field "pet" to "petBreed" in my service, it will not be caught by a JSON schema validator (e.g. https://github.com/xeipuuv/gojsonschema).

Making all fields required is not an option. Can anyone suggest a library in Go that will:

  1. validate that all the response fields are in the schema
  2. not fail if a field from the schema isn't in the response

图片转代码服务由CSDN问答提供 功能建议

Golang JSON模式验证库可验证服务请求/响应中是否存在模式上的必填字段。

我需要验证服务请求或响应中的任何字段必须是架构上的属性。 如果架构中有效负载中的属性不存在,则验证应失败。

例如: GET 响应: \ n

“ pet”:“ dog”,
“ name”:“ Scooby”,
“ licence”:“ 123-123” 

在我的示例JSON模式中,不需要任何字段。 但是,如果我在服务中将字段“ pet”更改为“ petBreed”,则不会被JSON模式验证器捕获(例如, https://github.com/xeipuuv/gojsonschema )。

必须填写所有必填字段。 谁能在Go中建议一个库,该库将

  1. 验证所有响应字段都在模式中
  2. 如果某个字段不失败 模式中的响应不在响应中
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douyan2002 2017-05-15 15:43

    JSON Schema defines additionalProperties for this purpose, something like this schema should work:

        "type": "object",
        "additionalProperties": false,
            "pet": ...,
            "name": ...,
            "license": ...,

    This is implemented but not documented as such in gojsonschema.

    Note that additionalProperties is a schema, not just a boolean, i.e. you can do arbitrary validation of unknown properties, not just disallow them.

    解决 无用
    打赏 举报

相关推荐 更多相似问题