Reputation:
I use com.github.fge.jsonschema.main.JsonSchema to validate json.
This is json schema:
{
"$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"
}
}
}
And this is json to validate against schema:
{"name" : "name"}
This is valid when I use online validator to check, but in test I get an error:
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:?]...
I can't see where the error is.
Upvotes: 2
Views: 4772
Reputation: 23037
Well, the error seems to be in your scheme instead of in the file to validate. The required property is of the array type, but the array has no element.
The validation succeeds when you either remove the property required
, or provide at least one string element indicating which properties should be required:
"required": [
"name"
]
Since Draft 4 of the JSON Schema documentation, the array must have at least one element.
Upvotes: 1