json-schema-validator icon indicating copy to clipboard operation
json-schema-validator copied to clipboard

Core meta schema validation

Open BigCheese83 opened this issue 5 years ago • 9 comments

In my project, I need to validate the JSON scheme itself. I made an example of an erroneous scheme that produces an error here, if selected "Schema Draft V7".

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "properties": {
     "name": {
         "type": 2
     }
  }
}

this code does not throw an error (set of validation messages is empty)

    // param schema contains the error example above
    public void validateSchema(ObjectNode schema) {
        JsonSchemaFactory validatorFactory = JsonSchemaFactory.builder(JsonSchemaFactory.getInstance(V7))
                .objectMapper(objectMapper)
                .build();
        JsonSchema jsonSchema = validatorFactory.getSchema(schema);
        Set<ValidationMessage> messages = jsonSchema.validate(schema);
        log.info("" + messages);
    }

For scheme draft V7, I can specify the core meta scheme, but I do not know how to do this for scheme draft 2019-09, since it consists of several vocabularies.

BigCheese83 avatar May 08 '20 10:05 BigCheese83

For each draft version, there is a meta schema. You can use it to validate the schema you are created. It is not common practice to validate the schema each time before using it to validate a JSON object.

Please refer to this question and let me know if you still have questions.

https://github.com/networknt/json-schema-validator/issues/285

stevehu avatar May 08 '20 14:05 stevehu

Thank you, I read this issue. I do not create a custom meta scheme, but use the standard for draft 2019-09. Could you give an example of the code, how can I specify a standard meta scheme for 2019-09? My problem is that the meta scheme for this version, taken from here https://json-schema.org/specification.html#meta-schemas, consists of several linked json files.

BigCheese83 avatar May 08 '20 15:05 BigCheese83

As it is discussed in the mentioned issue, there is no difference when using meta schema to validate a schema. You just need to treat the meta schema as the schema input and your scheme file as the JSON object.

stevehu avatar May 08 '20 17:05 stevehu

okay. The meta scheme for version 2019-09 contains several vocabularies whose definitions are physically located in different json files: Core Vocabulary meta-schema Applicator Vocabulary meta-schema Validation Vocabulary meta-schema Format Vocabulary meta-schema Content Vocabulary meta-schema Meta-Data Vocabulary meta-schema

Does the library support vocabularies? How do I load everything into an JSON object?

BigCheese83 avatar May 09 '20 09:05 BigCheese83

Hey @BigCheese83 how did you manage to solve it? I'm having the same issue.

stefanondisponibile avatar May 13 '23 11:05 stefanondisponibile

@stefanondisponibile #475 identifies a work-around until we add support for vocabularies.

fdutton avatar May 22 '23 12:05 fdutton

Thank you @fdutton !

stefanondisponibile avatar May 22 '23 12:05 stefanondisponibile

@fdutton The bundled 2019-09 draft meta schema: https://gist.github.com/gareth-robinson/815239b36e397e484b77141b912d1b28, in https://github.com/networknt/json-schema-validator/issues/475#issuecomment-976306236 was very helpful for validating json schema against meta schema. Could you also provide a similar json for 2020-12 draft ?

rishabh413 avatar Jun 19 '23 04:06 rishabh413