schema-inspector-anyOf

1.0.1 • Public • Published

schema-inspector-anyOf

Custom-Checker anyOf for Schema-Inspector

This ES6 module will give you the ability to validate a collection of items against any-of sub schemas.

Install

$ npm i schema-inspector-anyOf

Example:

import SchemaInspector from 'schema-inspector';
import anyOf from 'schema-inspector-anyOf';

const SubSchema1 = {
    type: 'object',
    properties: {
        customSchemaObject1: {
            type: 'string'
        }
    }
};

const SubSchema2 = {
    type: 'object',
    properties: {
        customSchemaObject2: {
            type: 'string'
        }
    }
};

const SampleSchema = {
    type: 'object',
    properties: {
        name: {
            type: 'string'
        },
        items: {
            type: 'array',
            $anyOf: [SubSchema1, SubSchema2],
            exec: anyOf
        }
    }
};

const sampleData = {
    name: 'MyObject',
    items: [
        {
            customSchemaObject1: 'Candidate 1'
        },
        {
            customSchemaObject2: 'Candidate 2'
        }
    ]
};

SchemaInspector.validate(SampleSchema, sampleData); // Valid

sampleData.items.push({customSchemaObject3: 'Candidate 3'});
SchemaInspector.validate(SampleSchema, sampleData); // Invalid: candidate at index 2 Property @.customSchemaObject2: is missing and not optional

Test

$ npm test

Dependencies (1)

Dev Dependencies (4)

Package Sidebar

Install

npm i schema-inspector-anyOf

Weekly Downloads

1

Version

1.0.1

License

MIT

Last publish

Collaborators

  • joergpatz