everit-org / json-schema

JSON Schema validator for java, based on the org.json API
Apache License 2.0
856 stars 282 forks source link

Validating the syntax of a schema #273

Open ben-manes opened 5 years ago

ben-manes commented 5 years ago

Similar to #272, but for schema validation. The improved detail helps when schemas are dynamically defined by the application.

An example from the older library,

{
  "level": "error",
  "message": "\"boolean2\" is not a valid primitive type (valid values are: [array, boolean, integer, null, number, object, string])",
  "domain": "syntax",
  "schema": {
    "loadingURI": "#",
    "pointer": "/properties/document/properties/processed"
  },
  "keyword": "type",
  "found": "boolean2",
  "valid": [
    "array",
    "boolean",
    "integer",
    "null",
    "number",
    "object",
    "string"
  ]
}

whereas this library produces,

{
  "schemaLocation": "#/properties/document/properties/processed",
  "message": "#/properties/document/properties/processed: unknown type: [boolean2]"
}

Similarly for a missing definition,

{
  "schemaLocation":"#/definitions",
  "message":"#/definitions: key [edge2] not found"
}

The json output requires more work to translate into an application error. Ideally the details would be broken down enough that translation would be straightforward.

erosb commented 5 years ago

Hello, the best way to validate a schema is validating it against its meta-schema (so you get a ValidationException instead of a SchemaException).

ben-manes commented 5 years ago

I agree that is the correct first pass. However that does not resolve characteristics like missing definitions. A full resolution of the schema is required when ensuring correctness.