tdegrunt / jsonschema

JSON Schema validation
Other
1.83k stars 262 forks source link

Add base object #342

Open remcohaszing opened 3 years ago

remcohaszing commented 3 years ago

I would like to be able to use another object than the schema as a base object. Currently this requires iterating over the entire root object and calling addSchema for each schema found.

For example when using OpenAPI:

import { Validator } from 'jsonschema';

const validator = new Validator();

const api = {
  components: {
    schemas: {
      Foo: {
        type: 'object',
        properties: {
          bar: { $ref: '#/components/schemas/Bar' }
        }
      },
      Bar: {
        type: 'object',
        properties: {
          baz: { type: 'string' }
        }
      }
    }
  }
};

validator.validate('123', api.components.schemas.Foo, { baseObj: api });

This example is simplified. In reality JSON schemas may occur in many places inside an OpenAPI document.