json-schema-validator
json-schema-validator copied to clipboard
Add quotes while printing const string value to support empty string
Below code:
import static com.networknt.schema.SpecVersion.VersionFlag.V202012;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.networknt.schema.JsonSchemaFactory;
String schema =
"{"
+ " \"type\": \"object\","
+ " \"properties\": {"
+ " \"myKey\" : {"
+ " \"type\" : \"string\","
+ " \"oneOf\" : ["
+ " { \"const\": \"\" },"
+ " { \"const\": \"a\" },"
+ " { \"const\": \"b\" }"
+ " ]"
+ " }"
+ " }"
+ "}";
String serializedData = "{ \"myKey\" : \"c\"}";
JsonNode data = new ObjectMapper().readTree(serializedData);
JsonSchemaFactory.getInstance(V202012)
.getSchema(schema)
.validate(data)
.forEach(System.out::println);
gives below validation errors:
$.myKey: should be valid to one and only one schema, but 0 are valid
$.myKey: must be a constant value
$.myKey: must be a constant value a
$.myKey: must be a constant value b
It would be very helpful if validation errors were:
$.myKey: should be valid to one and only one schema, but 0 are valid
$.myKey: must be a constant value ""
$.myKey: must be a constant value "a"
$.myKey: must be a constant value "b"
so that we can clearly see empty string.
I believe we need to change this line to:
const = {0}: must be a constant value "{1}"