Socket
Socket
Sign inDemoInstall

json-struct

Package Overview
Dependencies
67
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    json-struct

Simple tool to validate JSON structures


Version published
Maintainers
1
Created

Readme

Source

Install

npm i js-struct --save

Example

const Schema = require('json-struct');

/* a json structure to test */
const test = {
  one: 'one',
  two: {
    two: 'two',
    three: {
      four: 4
    },
    four: {
      five: 5
    }
  }
};

/* custom schema that validates the json structure */
const schema = {

  one: {
    /* asserts that the value is equal to 'one' */
    'value is one': v => {
      return v == 'one';
    }
  },

  two: {

    /* complex nested structures work */
    three: {

      /* define as many assertions per node as you want */
      'value is two': v => {
        return v == 'two';
      },

      'is rigth length': v => {
        return v.length == 3;
      }
    },

    four: {
      five: {
        'is a number': v => {
          return typeof v == 'number';
        }
      }
    },

    five: {

      /* don't need worry about having duplicate keys are the same nest level */
      five: {
        'is greater than 4': v => {
          return v > 4;
        }
      }
    }
  }
}

const schema = new Schema(schema);
const result = schema.validate(test);

FAQs

Last updated on 10 Feb 2016

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc