0.1.1 • Published 8 years ago

schema-spec v0.1.1

Weekly downloads
7
License
MIT
Repository
github
Last release
8 years ago

SchemaSpec

npm version Build Status Coverage Status Dependency Status devDependency Status

SchemaSpec is a JavaScript library for defining object schemas and validating object against them. It implements UMD for CommonJS, AMD, and global based loading support.

SchemaSpec is released under the MIT license.

Examples

Basic usage

var SchemaSpec = require('schema-spec');
var is = SchemaSpec.conditions

var spec = new SchemaSpec()
    .property("id", is.number)
    .property("name", is.string);

var valid = spec.validate(person);

Multiple conditions per property

Multiple conditions can be specified as an array.

var is = SchemaSpec.conditions;

var spec = new SchemaSpec()
    .property("id", [is.number, is.min.value(0)])
    .property("name", [is.string, is.not.empty]);

Applying conditions to all properties

Conditions can be applied to all specified properties using .all().

var is = SchemaSpec.conditions, are = is

var spec = new SchemaSpec()
    .property("id")
    .property("name", is.not.empty)
    .all(are.not.undefined);

Validating nested objects

Object hierarchies can be validated by using the "schema" condition to validate properties against SchemaSpecs.

var is = SchemaSpec.conditions;

var accountSpec = new SchemaSpec()
    .property("accountNumber", is.integer);

var personSpec = new SchemaSpec()
    .property("id", is.number)
    .property("name", is.string)
    .property("account", is.schema(accountSpec));

Or Condition

Using either().or() you can validate a property against two conditions (or sets of conditions) using a logical or. In the case below the object would be considered valid is the id property is null, or if it is an integer that is greater than or equal to zero.

var is = SchemaSpec.conditions;

var spec = new SchemaSpec()
    .property("id", is.either(is.null).or([is.integer, is.min.value(0)]));

Custom conditions

You can easily create your own validation conditions. Conditions are just functions that take a single property value and return true if the value meets the condition or false if it fails.

var isNamedJohn = function(value) {
    return value === 'John';
}

var spec = new SchemaSpec().property("name", isNamedJohn);

Documentation

SchemaSpec

Kind: global class

new SchemaSpec()

Creates a new schema specification object.

schemaSpec.all(conditions) ⇒ SchemaSpec

Sets conditions to be applied to all specified properties in the object.

Kind: instance method of SchemaSpec
Returns: SchemaSpec - Returns a reference to the SchemaSpec object to support builder style calls

ParamTypeDescription
conditionsfunction | Array.<function()>A single or array of conditions to apply

schemaSpec.property(name, conditions) ⇒ SchemaSpec

Sets conditions to be applied to a property.

Kind: instance method of SchemaSpec
Returns: SchemaSpec - Returns a reference to the SchemaSpec object to support builder style calls

ParamTypeDescription
namestringThe name of the property
conditionsfunction | Array.<function()>A single or array of conditions to apply

schemaSpec.validate(object) ⇒ SchemaSpec

Validates an object against the schema spec.

Kind: instance method of SchemaSpec
Returns: SchemaSpec - Returns true if the object passes all validations, false if at least one validation fails.

ParamTypeDescription
objectobjectThe object to be validated

SchemaSpec.conditions : object

Contains condition functions to be used for validation

Kind: static property of SchemaSpec
Properties

NameTypeDescription
nullfunctionAsserts the value is null
undefinedfunctionAsserts the value is undefined
stringfunctionAsserts the value is a string
numberfunctionAsserts the value is a number
booleanfunctionAsserts the value is a boolean
functionfunctionAsserts the value is a function
objectfunctionAsserts the value is an object
arrayfunctionAsserts the value is an array
integerfunctionAsserts the value is an integer
emptyfunctionAsserts the value is empty
equal.to(value)functionAsserts the value is equal to the provided value
length(length)functionAsserts the value length is equal to the provided length
min.length(length)functionAsserts the value is greater then or equal to the provided length
max.length(length)functionAsserts the value is less than or equal to the provided length
greater.than(value)functionAsserts the value is greater than the provided value
less.than(value)functionAsserts the value is less that the provided value
arrayOf(conditions)functionAsserts value is an array and all values match the provided condition(s)
schema(SchemaSpec)functionAsserts the value is an object and passes validation with the provided SchemaSpec
either(conditionA).or(conditionB)functionAsserts either conditionA passes, or conditionB passes
not.nullfunctionAsserts the value is not null
not.undefinedfunctionAsserts the value is not undefined
not.stringfunctionAsserts the value is not a string
not.numberfunctionAsserts the value is not a number
not.booleanfunctionAsserts the value is not a boolean
not.functionfunctionAsserts the value is not a function
not.objectfunctionAsserts the value is not an object
not.arrayfunctionAsserts the value is not an array
not.integerfunctionAsserts the value is not an integer
not.emptyfunctionAsserts the value is not empty
not.equal.to(value)functionAsserts the value is not equal to the provided value
not.length(length)functionAsserts the value length is not the provided length