We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Schema:
{ "$schema": "http://json-schema.org/draft-06/schema#", "title": "Gapa Message", "description": "A message containing data about an inbound or outbound request", "type": "object", "properties": { "timestamp": { "type": "number" }, "method": { "type": "string" }, "url": { "type": "string" } }, "oneOf": [ { "properties": { "type": { "const": "inbound" }, "sender": { "type": "string" } }, "required": [ "sender" ] }, { "properties": { "type": { "const": "outbound" }, "receiver": { "type": "string" } }, "required": [ "receiver" ] } ], "required": [ "timestamp", "method", "url", "type" ] }
JSON Data:
{"method":"PUT","sender":"mars","type":"inbound","url":"/test/server/hello/world","timestamp":1511356774358}
Java Code:
import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.github.fge.jsonschema.exceptions.ProcessingException; import com.github.fge.jsonschema.main.JsonSchema; import com.github.fge.jsonschema.main.JsonSchemaFactory; import com.github.fge.jsonschema.report.ProcessingReport; import java.io.IOException; public class SchemaValidation { public static void main(String[] args) throws IOException, ProcessingException { //use jackson to convert String into JsonNode ObjectMapper mapper = new ObjectMapper(); JsonNode schemaJsonNode = mapper.readTree(SchemaValidation.class.getResourceAsStream("/message-schema.json")); //Copy of dummy message: JsonNode messageJsonNode = mapper.readTree("{\"method\":\"PUT\",\"sender\":\"mars\",\"type\":\"inbound\"," + "\"url\":\"/test/server/hello/world\",\"timestamp\":1511356774358}"); //create schema final JsonSchema schema = JsonSchemaFactory.byDefault().getJsonSchema(schemaJsonNode); //validate ProcessingReport report = schema.validate(messageJsonNode); report.forEach(processingMessage -> System.out.println(processingMessage.toString())); if(!report.isSuccess()) { throw new AssertionError("Validation should be successful"); } } }
Output:
{level="warning", schema={"loadingURI":"#","pointer":"/oneOf/0/properties/type"}, domain="syntax", message="unknown keyword(s) found; ignored", ignored=["const"]} {level="warning", schema={"loadingURI":"#","pointer":"/oneOf/1/properties/type"}, domain="syntax", message="unknown keyword(s) found; ignored", ignored=["const"]}
It works if I replace const with enum:
{ "$schema": "http://json-schema.org/draft-06/schema#", "title": "Gapa Message", "description": "A message containing data about an inbound or outbound request", "type": "object", "properties": { "timestamp": { "type": "number" }, "method": { "type": "string" }, "url": { "type": "string" } }, "oneOf": [ { "properties": { "type": { "enum": ["inbound"] }, "sender": { "type": "string" } }, "required": [ "sender" ] }, { "properties": { "type": { "enum": ["outbound"] }, "receiver": { "type": "string" } }, "required": [ "receiver" ] } ], "required": [ "timestamp", "method", "url", "type" ] }
Is const not supported in this implementation? Spec: http://json-schema.org/latest/json-schema-validation.html#rfc.section.6.1.3
The text was updated successfully, but these errors were encountered:
const was added in draft-06, see #226 for draft support discussions.
const
Sorry, something went wrong.
No branches or pull requests
Schema:
JSON Data:
Java Code:
Output:
It works if I replace const with enum:
Is const not supported in this implementation?
Spec: http://json-schema.org/latest/json-schema-validation.html#rfc.section.6.1.3
The text was updated successfully, but these errors were encountered: