tdegrunt / jsonschema

JSON Schema validation
Other
1.82k stars 262 forks source link

add schema.examples (new in draft 6) #353

Open kussmaul opened 2 years ago

kussmaul commented 2 years ago

JSON Schema draft 6 adds an examples array "to provide an array of examples that validate against the schema": https://json-schema.org/understanding-json-schema/reference/generic.html#annotations

Please add Schema.examples, and perhaps validateExamples(schema) to validate each example against the schema, similar to this code from my Jasmine tests.

  type      Schema    = JsonSchema.Schema // & { examples : [] };
  let       Validator = require('jsonschema').Validator;
  let       validator = new Validator();
  function  validateExamples(schema : Schema) {
    (schema as any).examples.map((e : any) => {
      const result = validator.validate(e, schema);
      expect(result.errors).toEqual([]);
      if (result.errors.length) { console.log('validate()', e, result); }
    });
  }