doubledash
v0.4.2
Published
A schema-based object parser
Downloads
10
Maintainers
Readme
Doubledash
A schema-based object parser
- Installation
- Example
- API
- License
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
- schema#plugin()
- schema#use()
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.0required: Boolean
Will throw an error when a required option is not setdefault: 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 ofkey
during parsing.overwrite: Boolean
When true, this will overwrite the previous function atkey
with functionfunction
.
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 thatuse
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