java-json-tools / json-schema-validator

A JSON Schema validation implementation in pure Java, which aims for correctness and performance, in that order
http://json-schema-validator.herokuapp.com/
Other
1.62k stars 399 forks source link

const keyword is unknown #245

Open brunm1 opened 6 years ago

brunm1 commented 6 years ago

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

handrews commented 6 years ago

const was added in draft-06, see #226 for draft support discussions.