🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
DemoInstallSign in
Socket

doubledash

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

doubledash

A schema-based object parser

0.4.2
latest
Source
npm
Version published
Weekly downloads
2
-71.43%
Maintainers
1
Weekly downloads
 
Created
Source

Doubledash Build Status Greenkeeper badge

A schema-based object parser

Installation

npm install doubledash --save

Example

/* Custom class as an example */
class CustomClass {
  constructor (hey) { this._hey = hey }
}

/* Creating the schema */
const Schema = require('doubledash')
const template = {
  option1: Boolean, // Shorthand for { type: Boolean }
  option2: { type: Number, required: true, min: 5 }, // Custom function 'min' (see below)
  option3: [{
    option3_1: { type: String, default: 'string' },
    option3_2: { type: Object, required: true },
    option3_3: CustomClass
  }] // Embedding keys into an object (see result)
}

const schema = new Schema(template) // Constructed schema

function min (rule, input, key) {
  if (input <= rule) {
    input = rule
  }
  return input
}

schema.plugin('min', min) // Add new functionality to the schema

const options = {
  option2: 1,
  option3: {
    option3_2: { },
    option3_3: new CustomClass('hi there')
  }
}

console.log(schema.use(options)) // Use the schema on the input object

/*
{ option2: 5,
  option3: {
    option3_1: 'string',
    option3_2: { },
    option3_3: CustomClass { _hey: 'hi there' }
  }
}
*/

API

Schema(template)

Schema is the exposed class. Create it by using new Schema(template).
The template argument is an object with the following notation:

const template = {
  option1: Boolean // simple option of type 'Boolean'
  option2: { type: Number, required: true } // a simple option with multiple properties
  option3: [{ // an option containing suboptions
    option3_1: { type: String, default: 'string' },
    option3_2: { type: Object, required: true }
  }]
}

Objects can have the following rules by default:

  • type: Any
    Will throw an error when input is not of this type
    Mandatory at versions below 0.4.0

  • required: Boolean
    Will throw an error when a required option is not set

  • default: Any
    Overrides input when not set

schema.plugin(key, function, overwrite)

This method plugs in your functions to the object parser, making it modular.

  • key: String
    The key you use in your schema to use the function.

  • function: Function
    Function that processes the input relative to the value of key during parsing.

  • overwrite: Boolean
    When true, this will overwrite the previous function at key with function function.
    This is useful for overwriting built-in plugins, such as type, required or default.

This function should return the processed input, or throw errors:

const template = { num: { type: Number, required: true, minimum: 5 } }
const schema = new Schema(template)

function min (rule, input, key) {
  if (input <= rule) {
    input = rule
  }
  return input
}

schema.plugin('minimum', min)

Doubledash will pass three arguments to your function whenever it reads the set key in a template.
These three arguments are:

  • rule: Any
    The given value when you create your template. In the example above, this value is 5.

  • input: Any
    The actual input of the object that use was called on.

  • key: String
    The name of the object with given input. Useful for throwing errors.

If this might still be too vague, you could check out the built-in plugins in doubledash.js, the source file of the project.

schema.use(input)

This method is used to test your input to the referencing schema.

The input argument should be an object that will be tested against the schema.
This method will return a processed object, which is in accordance to this schema.

See the example for more info.

License

MIT

Keywords

doubledash

FAQs

Package last updated on 07 Apr 2017

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