SchemaSpec
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
- SchemaSpec
- new SchemaSpec()
- instance
- static
- .conditions :
object
- .conditions :
new SchemaSpec()
Creates a new schema specification object.
SchemaSpec
schemaSpec.all(conditions) ⇒ 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
Param | Type | Description |
---|---|---|
conditions | function | Array.<function()> |
A single or array of conditions to apply |
SchemaSpec
schemaSpec.property(name, [conditions]) ⇒ 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
Param | Type | Description |
---|---|---|
name | string |
The name of the property |
[conditions] | function | Array.<function()> |
A single or array of conditions to apply |
SchemaSpec
schemaSpec.validate(object) ⇒ 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.
Param | Type | Description |
---|---|---|
object | object |
The object to be validated |
object
SchemaSpec.conditions : Contains condition functions to be used for validation
Kind: static property of SchemaSpec
Properties
Name | Type | Description |
---|---|---|
null | function |
Asserts the value is null |
undefined | function |
Asserts the value is undefined |
string | function |
Asserts the value is a string |
number | function |
Asserts the value is a number |
boolean | function |
Asserts the value is a boolean |
function | function |
Asserts the value is a function |
object | function |
Asserts the value is an object |
array | function |
Asserts the value is an array |
integer | function |
Asserts the value is an integer |
empty | function |
Asserts the value is empty |
equal.to(value) | function |
Asserts the value is equal to the provided value |
length(length) | function |
Asserts the value length is equal to the provided length |
min.length(length) | function |
Asserts the value is greater then or equal to the provided length |
max.length(length) | function |
Asserts the value is less than or equal to the provided length |
greater.than(value) | function |
Asserts the value is greater than the provided value |
less.than(value) | function |
Asserts the value is less that the provided value |
arrayOf(conditions) | function |
Asserts value is an array and all values match the provided condition(s) |
schema(SchemaSpec) | function |
Asserts the value is an object and passes validation with the provided SchemaSpec |
either(conditionA).or(conditionB) | function |
Asserts either conditionA passes, or conditionB passes |
not.null | function |
Asserts the value is not null |
not.undefined | function |
Asserts the value is not undefined |
not.string | function |
Asserts the value is not a string |
not.number | function |
Asserts the value is not a number |
not.boolean | function |
Asserts the value is not a boolean |
not.function | function |
Asserts the value is not a function |
not.object | function |
Asserts the value is not an object |
not.array | function |
Asserts the value is not an array |
not.integer | function |
Asserts the value is not an integer |
not.empty | function |
Asserts the value is not empty |
not.equal.to(value) | function |
Asserts the value is not equal to the provided value |
not.length(length) | function |
Asserts the value length is not the provided length |