Json验证针对JSON模式失败

时间:2019-02-27 09:36:45

标签: java json

我使用com.github.fge.jsonschema.main.JsonSchema来验证json。

这是json模式:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "title": "Team data",
  "description": "Validation schema",
  "type": "object",
  "additionalProperties": false,
  "required": [
  ],
  "properties": {
    "name": {
      "type": "string",
      "minLength": 2,
      "maxLength": 255,
      "description": "Name"
    }
  }
}

这是用于验证架构的json:

{"name" : "name"}

这是有效的,当我使用在线验证器进行检查时,但在测试中却出现错误:

Caused by: com.github.fge.jsonschema.core.exceptions.InvalidSchemaException: fatal: invalid JSON Schema, cannot continue
Syntax errors:
[ {
  "level" : "error",
  "message" : "array must have at least one element",
  "domain" : "syntax",
  "schema" : {
    "loadingURI" : "#",
    "pointer" : ""
  },
  "keyword" : "required"
} ]
    level: "fatal"

    at com.github.fge.jsonschema.processors.validation.InstanceValidator.process(InstanceValidator.java:114) ~[json-schema-validator-2.2.10.jar:?]
    at com.github.fge.jsonschema.processors.validation.ValidationProcessor.process(ValidationProcessor.java:56) ~[json-schema-validator-2.2.10.jar:?]
    at com.github.fge.jsonschema.processors.validation.ValidationProcessor.process(ValidationProcessor.java:34) ~[json-schema-validator-2.2.10.jar:?]
    at com.github.fge.jsonschema.core.processing.ProcessingResult.of(ProcessingResult.java:79) ~[json-schema-core-1.2.10.jar:?]
    at com.github.fge.jsonschema.main.JsonSchemaImpl.doValidate(JsonSchemaImpl.java:77) ~[json-schema-validator-2.2.10.jar:?]
    at com.github.fge.jsonschema.main.JsonSchemaImpl.validate(JsonSchemaImpl.java:100) ~[json-schema-validator-2.2.10.jar:?]
    at com.github.fge.jsonschema.main.JsonSchemaImpl.validate(JsonSchemaImpl.java:110) ~[json-schema-validator-2.2.10.jar:?]...

我看不到错误在哪里。

1 个答案:

答案 0 :(得分:0)

好吧,错误似乎出在您的方案中,而不是在要验证的文件中。必需的属性是数组类型,但是数组没有元素。

当您删除属性required或提供至少一个字符串元素以指示应要求哪些属性时,验证就会成功:

"required": [
    "name"
]

JSON Schema documentation的草案4起,该数组必须至少包含一个元素。