Every API function in HT requires a schema that defines the kind of data that function takes. While this may seem laborious at first, HT schemas are very pragmatic, combine both validation and coersion in one. This means you are free to assume that your data is valid and properly typed every time, no more manual validation or type casting required.
Schemas are inherently composible, you can nest schemas inside schemas, infact this is what happens when you add a String attribute to an Object schema anyway.
Schemas are also extensible, the default ones are defined in lib/schema.js and they are quite easy to read and create your own.
Here is an example schema:
var tagSchema = s;var movieSchema = s;
An object validator with one string attribute:
A liberal object validator that can have any attribute:
A liberal object validator that can have any attribute but specifies that a cat attribute must be an object with a name:
An object validator that requires a foo attribute but remaps it to bar in output.
You can access object keys through 'keys' on an instance of an object.
var schema = s;schemakeyshello;
A string validator with an enum restriction
A string validator with a length limit
A string validator that coerces the value
A string validator that matches a regex (regex can optionally be passed in as a string)
An array validator which can only contain Dates.
An optional array validator which can contain cats and dogs.
Note: Array validators match in precidence left to right.
var catSchema = s;var dogSchema = s;s;
var boolSchema = s;boolSchema; // -> truevar maybeSchema = s;maybeSchema; // -> true
If coerce is set, truthy and falsy values will be converted to true/false
max need to be unix epoch timestamps
If normalize is true (default) then the output email will be lowercased.
These functions are part of the schema instance you get back
Clone creates a copy of the schema.
If you pass an object as an argument to clone, it will merge your object with the schemas 'args', allowing you to set new attributes.
(Object only) If you pass an array as an argument, it will use the array values as a whitelist, and return a new Object schema with only these keys.
Note: You can pass more than one value to this function, they will be evaluated left to right.
var schema = s;schema; // this will throwvar newSchema = schema;newSchema; // this won't
Trimming an object:
var schema = s;schema;var newSchema = schema; // only return anewSchema;