1.0.1 • Published 8 years ago

schema-inspector-anyOf v1.0.1

Weekly downloads
5
License
MIT
Repository
github
Last release
8 years ago

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