Socket
Socket
Sign inDemoInstall

json-struct

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

json-struct

Simple tool to validate JSON structures


Version published
Weekly downloads
4
increased by300%
Maintainers
1
Weekly downloads
 
Created
Source

Install

npm i json-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

Package last updated on 10 Feb 2016

Did you know?

Socket

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc