Schema.js
Defining a Schema
Condensed Syntax
var schema = name:String age:Number phones: number:String mobile:Boolean
Explicit Syntax
var person = type: Object properties: name: type:String age: type:Number phones: type: Array items: type: Object properties: number: type:String mobile: type:Boolean )
These two syntaxes may be mixed within the same schema definition. The expanded syntax is required to specify additional options (instead of just types, array items, and object properties) for a field.
Field Options
Example
name: first: type:String name:'First Name' required:true last: type:String name:'Last Name' required:true age: type:Number default:18
List of Options
type Function|Array<Function>
The constructor function for the type of the value. May use javascript types or other constructor functions (String
, Number
, Date
, bson.ObjectID
, Schema
, etc.) or an Array of constructor functions.
default
The default value for a field if no value is specfied.
The type of the default value must match the above type.
required Boolean|Object
If type is Object, an Array listing required properties.
If type is not Object, a Boolean indicating whether a value is required for this field.
properties Object
Required if type is Object. Enforces the schema on the subkeys of this field.
items Object
Required if type is Array. Enforces the schema on all elements of the Array.
validators Array<Function>
Array of custom validator functions matching the signature function(value)
. A validator should throw
if it fails, otherwise return.
Validating an Object
var schema = name:String age:Number schema//returns true schema//errors because age is invalid. "old" is not a Number
API
new Schema(definition)
definition
An object defining fields to be added to the schema. See Defining a Schema and Field Options.
schema.validate(value)
value
An value to check against the schema. See Validating an Object.
Schema.applyDefaults(value)
value
A value to which the defaults for the schema will be applied.
schema.extend(definition)
definition
An object defining fields to be added to the schema. See Defining a Schema and Field Options.
schema.extendWhen(query, definition)
query
An object defining a MongoDB-like query. Powered by sift. Supports the following operators: $in, $nin, $exists, $gte, $gt, $lte, $lt, $eq, $neq, $mod, $all, $and, $or, $nor, $not, $size, $type, $regex.
definition
An object defining fields to be added to the schema. See Defining a Schema and Field Options.
Example
schema.extendWhen()
allows you to modify the schema based on the values of the object that you are validating. For example, we can require a credit card number and cvv, but only if the purchase is being made by credit card.
var purchase = amount:Number payment:String purchasepurchase purchase// errors because card.number and card.cvv are required purchase
type:
var schema = result:Boolean|Function schema
comparisons:
var purchase = amount:Number purchase
nested field modification
var person = name: first:String middle:String last: type:String required:true person name: first: type:String required:true )