Navigation Menu

Skip to content

Commit

Permalink
Add validate documentation to the README
Browse files Browse the repository at this point in the history
  • Loading branch information
Dom Harrington committed Aug 23, 2012
1 parent da7986d commit 33caa21
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions README.md
Expand Up @@ -80,9 +80,52 @@ var stripped = contactSchema.stripUnknownProperties({

### Validate an object against the schema

Validation is easy in schemata, just call **validate()** on your schema passing in the object to validate:

```js
contactSchema.validate(objectToValidate, function(errors){
// errors
});
```

Validators are assigned to a property of the schema by adding them as an array to the **validators** property of the object as follows (this is an extension of the example at the top):

```js
name: {
name: 'Full Name',
validators: [validator1, validator2]
}
```

Validators are objects with the following structure:

```js
{
validate: function(name, value, callback) {}
}
```

The callback must be called with a falsy value (such as undefined or null) if the validation passes, or with an Error object with the appropriate error message if it fails validation.

A full validator example:

```js
var required = {
validate: function(name, value, callback) {
return callback(value ? undefined : new Error(name + ' is required'));
}
};

name: {
name: 'Full Name',
validators: [required]
}
```

If any of the validators fail then the errors will be returned in the callback from **validate()** with the object key being the field name and the value being the error message.

For a comprehensive set of validators including: email, integer, string length, required & UK postcode. Check out [piton-validity](https://github.com/serby/piton-validity).

### Cast an object to the types defined in the schema

```js
Expand Down

0 comments on commit 33caa21

Please sign in to comment.