JSPM

json-schema-filter

0.1.0
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 1804
  • Score
    100M100P100Q107805F
  • License MIT

Filters (removes) objects from document based on passed json-schema

Package Exports

  • json-schema-filter

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (json-schema-filter) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

#json-schema-filter

Filters (removes) objects recursivly from document based on passed json-schema

** Note that this does NOT validate your document againts the schema, use someting like JSV npm after calling.**

For performance reasons it is assumed that the json-schema contains less items then the document, that is you are going to be removing from the document in most or all cases, if not I would recomend another solution which bases its recursive evalution on the document instead.

Install

$ npm install json-schema-filter

Usage...

var filter = require('json-schema-filter');

var schema = {
    "title": "Example Schema",
    "type": "object",
    "properties": {
      "firstName": {
        "type": "string"
      },
      "lastName": {
        "type": "string"
      },
      "age": {
        "description": "Age in years",
        "type": "integer",
        "minimum": 0
      },
      "contacts": {
        "type": "array",
        "id": "http://jsonschema.net/contacts",
        "required": false,
        "items": {
          "type": "object",
          "id": "http://jsonschema.net/contacts/0",
          "required": false,
          "properties": {
            "phone": {
              "type": "string",
              "required": false
            }
          }
        }
      }
    },
    "required": ["firstName", "lastName"]
};

var document = {firstName: 'John', lastName: 'Dow!', shouldNot: 'see this!'};

var results = filter(schema, document);   

console.log(results);  // # {firstName: 'John', lastName: 'Dow!'}

// Works on nested objects and arrays as well...
document = {
    firstName: 'Johnny',
    lastName: 'Dowsky',
    contacts: [
        {phone: '303943', shouldNot: 'see this!'},
        {phone; '399494'}
    ]
}
var nestedResults = filter(schema, document);

console.log(results);  // # {firstName: 'Johnny', lastName: 'Dowski', contacts: [{phone: '303943', phone: '399494'}]}