Socket
Book a DemoInstallSign in
Socket

adhere-core

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

adhere-core

Make objects adhere to a specification

Source
npmnpm
Version
0.3.1
Version published
Weekly downloads
1
-50%
Maintainers
1
Weekly downloads
 
Created
Source

Note: This is the core version of adhere. It contains only the essentials making it easy to understand what adhere does.

Check out the common version of adhere. It contains functions your are likely to need in your projects.

var post = {
    id: '1234abcd',
    title: 'My Summer Adventures',
    tag: 1
};

// Create a contract.
var validator = adhere({
    // The title will be coerced into a string.
    title: coerceString,
    // The tags property is expected to be an array. Each item is coerced into a string and expected to be a tag. 
    tag: [coerceString, expectTag]
});

// Apply the contract to the post object.
validator(post);
// post.id = '1234abcd'
// post.title = 'My Summer Adventures'
// post.tag = '1'

// A function that tests if a given string is considered a tag. 
function expectTag(string) {
    if (!/[a-z0-1]+/.test(string)) throw Exception('Invalid tag.')
    return string
}

// A function that will always output a string. 
function coerceString(value) {
    return '' + value
}

Adhere accepts transforms which are just functions of the form:

function identity(value, key, obj) {
    return value
}

This means you can normalize your data by returning a different value. To filter erroneous data I recommend to throw exceptions from your transforms. For example:

function expectObject(value, key, obj) {
    if (Object.prototype.toString.call(value) !== '[object Object'])
        throw new Error('Expected an object')
    return value
}

Keywords

adhere

FAQs

Package last updated on 01 Sep 2015

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