New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

json-safeguard

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

json-safeguard

A lib that effectively safeguards data types, and automatic completion of the required words.

  • 1.1.5
  • latest
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

json-safeguard

Build Status Coverage Status NPM Package NPM Downloads Dependency Status FOSSA Status

A lib that effectively safeguards data types, and automatic completion of the required words.

Quick Start

$ npm i --save json-safeguard
const safeguard = require('json-safeguard')

const schema = {
  name: 'string'
}
const data = {
  name: 'Heleth'
}

safeguard(schema, data) /** =>
  {
    state: true,
    message: 'Verification passed',
    complated_data: {
      name: 'Heleth'
    }
  }
*/

More complicated verification

const schema = {
  name: 'string*',
  age: 'number',
  excellent: (value, data) => {
    const subjects = data.subjects || []
    const isExcellent = subjects.every(item => item.achievement >= 90)
    return isExcellent === value ? true : {
      state: false,
      messages: [
        `${data.name} is ${isExcellent ? '' : 'n\'t'} excellent`
      ]
    }
  },
  subjects: [
    {
      name: 'string*',
      achievement: 'number*'
    }
  ],
  hobby: 'string[]*'
}

const data = {
  name: 'Heleth',
  age: 23,
  excellent: false,
  subjects: [
    {
      name: 'Javascript',
      achievement: 95.5
    },
    {
      name: 'Python',
      achievement: 90
    },
    {
      name: 'Swift',
      achievement: 93
    }
  ]
}

safeguard(schema, data) /** =>
  {
    state: false,
    message: 'Heleth is excellent. | hobby: Expected a value is required.',
    completed_data: {
      name: 'Heleth',
      age: 23,
      excellent: false,
      subjects: [
        {
          name: 'Javascript',
          achievement: 95.5
        },
        {
          name: 'Python',
          achievement: 90
        },
        {
          name: 'Swift',
          achievement: 93
        }
      ]
    },
    hobby: []
  }
*/

Schema

Basic Types

Declare the type of field in the Schema to verify the data.

const schema = {
  _string: 'string'
}
TypesValueDescription
BooleanbooleanTrue or false.
NumbernumberInteger or float.
Stringstring
Array[]Array of any.
Boolean-Arrayboolean[]Array of boolean only.
Number-Arraynumber[]Array of number only.
String-Arraystring[]Array of string only.

Required and Automatic Completion

You can specify some required fields with *, such as number[]*. When the field is empty, it will automatically completion.

TypesValueDescription
Required*Check if this field exists.

For example:

const safeguard = require('json-safeguard')

const schema = {
  _string_array: 'string[]*'
}
const data = {}

safeguard(schema, data) /** =>
  {
    state: false,
    message: '_string_array: Expected a value is required.',
    completed_data: {
      _string_array: []
    }
  }
*/

Custom Verify

TypesValueDescription
Custom Verify(value, data) => anyUse a custom function to check if this field meets the requirements. Return a boolean to tell the result of the check directly. You can also return an object and set the custom message and default value, please read below for details.
const safeguard = require('json-safeguard')

const schema = {
  _number: (value, data) => value < 20
}
const data = {
  _number: 15
}

safeguard(schema, data) /** =>
  {
    state: true,
    message: '',
    completed_data: {
      _string_array: 15
    }
  }
*/

Result

PropertyDescriptionTypeDefault
stateVerification stateboolean-
messageError messagestring''
completed_dataSafe data containing required arrays and objectsobject{}

License

MIT

FOSSA Status

Keywords

FAQs

Package last updated on 15 Apr 2018

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