Skip to content
New issue

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

const keyword is unknown #245

Open
brunm1 opened this issue Nov 22, 2017 · 1 comment
Open

const keyword is unknown #245

brunm1 opened this issue Nov 22, 2017 · 1 comment

Comments

@brunm1
Copy link

brunm1 commented Nov 22, 2017

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
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants