Edu*_*llo 5 ruby json jsonschema
我正在尝试用ruby gem json-schema验证一些json数据.
我有以下架构:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "User",
"description": "A User",
"type": "object",
"properties": {
"name": {
"description": "The user name",
"type": "string"
},
"e-mail": {
"description": "The user e-mail",
"type": "string"
}
},
"required": ["name", "e-mail"]
}
Run Code Online (Sandbox Code Playgroud)
以及以下json数据:
{
"name": "John Doe",
"e-mail": "john@doe.com",
"username": "johndoe"
}
Run Code Online (Sandbox Code Playgroud)
并且使用此数据作为输入的JSON :: Validator.validate返回true.
不应该是假的,因为架构上没有指定用户名吗?
您需要additionalProperties在JSON Schema中定义并将其设置为false:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "User",
"description": "A User",
"type": "object",
"properties": {
"name": {
"description": "The user name",
"type": "string"
},
"e-mail": {
"description": "The user e-mail",
"type": "string"
}
},
"required": ["name", "e-mail"],
"additionalProperties": false
}
Run Code Online (Sandbox Code Playgroud)
现在验证应该false按预期返回:
require 'json'
require 'json-schema'
schema = JSON.load('...')
data = JSON.load('...')
JSON::Validator.validate(schema, data)
# => false
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2502 次 |
| 最近记录: |