Version 2.0.0 and higher works only with expressjs version 4.x. Booster versions <2.0.0 will work only with express version <4.0.0 and booster versions >=2.0.0 will work only with express version >=4.0.0.

Version 2.0.0 and higher also does not pass the status code to the post-processor. You can retrieve it yourself via res.statusCode. See the documentation on post-processors.

The first (and only required) argument to booster.resource() is the name of the resource. It should be all string, valid alphanumeric, and will ignore all leading and trailing whitespace. It will also ignore anything before the last slash, so:

When you do a GET to a resource collection - e.g. GET /comment as opposed to GET /comment/1 - you can pass search parameters to the query.

All query parameters passed to the request will be passed to model.find(), and by extension db.find(), except for ones specific to the controller. Controller parameters always start with $b., e.g. '$b.csview'.

PUT and PATCH return the ID of the updated object in the body of the response. Sometimes, though, you prefer to have the request return the updated object in its entirety in the body.

There has been extensive debate among the REST community which is the correct response. booster is smart enough not to take sides in this debate and support both options. By default, successful PUT/PATCH return the ID of the updated object as the body of the response.

If you want a successful PUT/PATCH to return the body - as if you did the successful PUT/PATCH and then followed it up with a GET - you have 3 options:

global: by making it the default server-side in your booster initialization settings.

booster.init({sendObject:true});

param: as part of the request in the URL, add sendObject=true to the request.

PUT http://server.com/api/user/1?sendObject=true

header: as part of the request in the headers, add a header X-Booster-SendObject: true to the request.

X-Booster-SendObject: true

PUT http://server.com/api/user/1

As a rule of thumb:

URL param takes precedence over...

HTTP header, which takes precedence over...

booster initialization setting, which takes precedence over...

booster default

The following table lays out the results of a PUT/PATCH:

booster init

http header

param

send object?

NO

sendObject=true

YES

X-Booster-SendObject: true

YES

X-Booster-SendObject: true

sendObject=true

YES

X-Booster-SendObject: false

sendObject=true

YES

X-Booster-SendObject: true

sendObject=false

NO

X-Booster-SendObject: true

sendObject=false

NO

{sendObject:true}

YES

{sendObject:true}

X-Booster-SendObject: false

NO

{sendObject:true}

sendObject=false

NO

{sendObject:true}

X-Booster-SendObject: false

sendObject=true

YES

{sendObject:true}

X-Booster-SendObject: true

sendObject=false

NO

{sendObject:false}

NO

{sendObject:false}

sendObject=true

YES

{sendObject:false}

X-Booster-SendObject: true

YES

{sendObject:false}

X-Booster-SendObject: true

sendObject=true

YES

{sendObject:false}

X-Booster-SendObject: false

sendObject=true

YES

{sendObject:false}

X-Booster-SendObject: true

sendObject=false

NO

{sendObject:false}

X-Booster-SendObject: true

sendObject=false

NO

NOTE: booster init setting {sendObject:false} is the same as not setting an init param at all.

opts is just a plain old JavaScript object with options. What goes into those options? That depends what you want to do with this resource and what path it should have.

You can have the resource accessible from multiple paths and behave differently in each of those paths, by having multiple opts. First, let's see what you can do with each opts, then we'll string multiple together.

Sometimes, you want to name your resource one thing, while the path to it should be something else. This is useful when you have multiple paths to a resource, or if the actual name of the path might conflict with an existing resource.

For example, what if you wanted to have a resource called 'user' under a 'post', but it is different than the actual user resource.

GET /post post#index()
GET /post/:post post#show()
GET /post/:post/user diffuser#index()
GET /post/:post/user/:user diffuser#show()

To enable it, you use the name option:

booster.resource('diffuser',{parent:'post',name:'user'});

This will create the above paths. Note that the name of the parameter will be :user and not :diffuser.
However, it will expect a controller file, if any, to be named diffuser.js, since that is the name of the resource.

Sometimes, you want to require that a nested resource has a property that matches the parent.

For example, if I am creating a new nested comment on post 2 as follows:

POST /post/2/comment {author:"john",content:"This is a comment"}

I might want the content to require the name of the parent as a property:

{author:"john",content:"This is a comment",post:"2"}

booster can enforce this if you tell it to! When sitting up a nested resource, just set the parentProperty to true as follows:

booster.resource('comment',{parent:'post',parentProperty:true});

If parentProperty is set to true, booster will insist that the posted body contains a property with the same name as the parent, and that its value precisely matches the value of the parent parameter. In other words, it will insist that req.params[parent] === req.body[parent]. If not, it will reject it with a 400 error.

This rule is enforced for allPOST, PUT and PATCH.

If you have:

booster.resource('post');

booster.resource('comment',{parent:'post',parentProperty:true});

each of the following examples, it will show what works and what doesn't

PATCH /post/3/comment/4 {} // PASS: missing "post" property, but PATCH is only an update, not a replace

Note that for POST or PUT, where the body is the entire new or replacement object, the property must exist, else it fails. However, for PATCH, where it only replaces those explicit fields, it the parent property is missing, it passes.

In the case of POST and PUT, if you want the field to default to the value of the parent property - in our above example, {post:"3"} - you can tell booster, "Hey, the field has to match, but if it isn't there at all, fill it in for me." Just tell the resource, in addition to setting parentProperty to true, set parentDefault to true as well:

In that case, all of the above examples where missing post caused the route to fail with a 400 will now pass, and the value will be set:

POST /post/3/comment {} // PASS: will be sent to the model as {post:"3"}

PUT /post/3/comment/4 {} // PASS: will be sent to the model as {post:"3"}

Note that if you have a property set to {mutable:false} and the same property is {parentDefault:true}, it might conflict when doing a PUT. POST will never be a problem, since it does not yet exist, and PATCH is not a problem, since it only updates the given fields.

booster will look at the update (PUT) to comment with the id "1", see that it requires the parent property ("post") but that it isn't present, so it will add {post:"3"} before updating the model. This is the equivalent of:

PUT /post/3/comment/1 {content:"Now I am not",post:"3"}

This is great, but what if you defined the comment model making post immutable:

module.exports = {

fields: {

content:{required:true,mutable:true},

post: {required:true,mutable:false},

id: {required:true,mutable:false}

}

}

After all, you do not want someone accidentally moving a comment from one post to another! But then the update of

PUT /post/3/comment/1 {content:"Now I am not",post:"3"}

will cause a 400 error, since it is trying to update the post property, and that one is immutable!

Not to worry; booster inteillgently handles this. If you actually try to update post, it will throw a 400. But if you did not set it, and you have parentDefault set, it will not set it unless the field is mutable.

What if you don't want to create a whole new resource, but have a separate property as part of a resource? For example, if you want to be able to PUT /post/:post/title and so change the title directly?

It already works! Yes, that's right. If you already created the resource booster.resource('post'), then unless you created a nested resource of exactly the same name, GET /post/:post/title will get you the title from /post/:post Similarly, you can PUT /post/:post/title to change it. But, no you cannot POST or PATCH it; they don't make much sense.

OK, so the above works great if you want /post/1/title to map to title of post 1, or /post/10/author to map to author of post 10. But what if you want all of the above and you want to map some special properties to their own handlers. For example, if a user is:

{id:"1",firstname:"john",lastname:"smith"}

so you want the following to work (and hey, booster already said you get it for free):

In other words, that special property groups is really not a property of a user object, but has its own logic. On the other hand, it behaves mightily like a property: it uses PUT and GET, and has meaning only in the context of a specific user. It isn't a first-class resource (the group and the user are), but that array of group IDs comes from somewhere else!

You know that I'm going to say, "it's easy!", right?

All you need to do is put in place that special controller file, and add properties to it.

module.exports = {

properties: {

groups: {

get: function(req,res,next) {

// do all of your get logic here

// LOGIC A

},

set: function(req,res,next) {

// do all of your set logic here

// LOGIC B

}

},

roles: {

get: function(req,res,next) {

// LOGIC C

}

},

strange: {

set: function(req,res,next) {

// LOGIC D

}

}

}

};

So when booster hits a property of a resource, like /user/1/someProperty, it says, if all of the following is true, use your function, else treat it just like a regular property:

(controller file exists) AND
(controller has "properties" key) AND
("properties" key has appropriate property name) AND
( (property name has "get" key as function if request was GET) OR
(property name has "get" key as function if request was PUT) )

Going back to the above example, here is what will happen with each type of request and why:

GET /user/1/title -> get the property "title" of the object; properties.title not defined
GET /user/1/groups -> use function for LOGIC A; properties.groups.get defined
PUT /user/1/groups -> use function for LOGIC B; properties.groups.set defined
GET /user/1/roles -> use function for LOGIC C; properties.roles.get defined
PUT /user/1/roles -> get property "roles" of the object; properties.roles.set not defined
GET /user/1/strange -> get property "strange" of the object; properties.strange.get not defined
PUT /user/1/strange -> use function for LOGIC D; properties.strange.set defined

Actually, it is even easier! A really common pattern is where a property of one resource is actually a reference to another resource that has some find restrictions. Take a look at the following:

GET /group -> get all of the groups
GET /group/10 -> get group whose ID is 10
GET /user/1/name -> get the name of user 1, normal property
GET /user/1/group -> Get all of the groups of which user "1" is a member, like GET /group?{user:1}

Since this is such a common pattern, let's make it easier for you!

booster.resource('group');

booster.resource('user',{resource:{group:["get"]}});

That is exactly the same as the following:

booster.resource('group');

booster.resource('user');

// and inside routes/user.js :

module.exports = {

properties: {

group: {

get: function(req,res,next) {

// get the groups of the user from the separate groups list and send them off

If you want a resource to exist in multiple locations, each with different (or similar) behaviour, you specify multiple options objects.

Let's say you want "post" to exist in 2 places:

GET /post post#index()
GET /post/:post post#show()
GET /api/post post#index()
GET /api/post/:post post#show()

Each "post" refers to the same resource, but is accessible at different paths. Perhaps you only allow updates at one path but not the other, e.g. if comments have a unique ID, so you can retrieve comments from anywhere, but update only via the "post":

These are the same "comment" resources, but accessible via different paths. All you need to do is have an opts for each one of them when declaring the resource.

booster.resource("post");

booster.resource('comment',{parent:"post"},{only:"show"});

The first options object {parent:"post"} sets up the path for a "comment" as child of a parent as /post/:post/comment. The second {only:"show"} sets up the path for a "comment" at the root, but only exposes "show".

Note: Normally, if you want an unmodified resource at the base path, you don't need to set options at all, just do booster.resource('comment');. However, with multiple options, booster cannot know that there is also a base path 'comment'. Thus, with multiple options, you must specify a blank options {} for base path. For example:

booster.resource("post");

booster.resource('comment',{parent:"post"},{}); // the second one is like booster.resource('comment');

The default controller provides the standard actions: index, show, create, update, patch, destroy. It interacts with the models of the same name (see models, below), and lists all of them, shows one, creates a new one, updates an existing one, or destroys one.

If you want to override one or more of the actions, just create a file with that name in controllerPath directory, either the default path or the one you provided when initializing booster. Each function should match express route signature. If you want to eliminate a route entirely, override it with a null. See the example below.

// <controllerPath>/post.js

module.exports = {

index: function(req,res,next) {

// do lots of stuff here

}

// because we only override index, all of the rest will just use the default

update: null

// because we actively made update null, the update() function and its corresponding PUT /post/:post will be disabled and return 404

And what if you want to pass some parameters to controller functions? For example, what if one of your controller functions needs to send emails, and you just happen to have a sendmail object in your app that knows how to do just that?

Well, you could require() it in each controller, but that really is rather messy, requires multiple configurations and calls (not very DRY), and would work much better if you could just inject the dependency.

booster supports that type of dependency injection. If you have configuration parameters you want available to your controllers, they are available, in true express style, on the req object as req.booster.param. You inject them by using the param property when calling booster.init()

Now, what if you don't want to entirely override the controller function, but perhaps put in a filter. You could easily just do:

module.exports = {

show: function(req,res,next) {

// handle everything here

}

}

But you didn't want to have to recreate all of the model calls, error handling, all of the benefits of the default controller. You really just wanted to inject some middleware prior to the default show() being called!

Booster gives you two really good options for this: all global filter, and filters for individual ones.

Before you start jumping up and saying, "hey, that is business logic, which should go in the models! Fat models and skinny controllers!", you are right. But sometimes your controllers need to do filtering or post-processing that is appropriate at the controller level, hence Filters.

For filters (pre-index/show/update/create/delete) and post-processing (post-index/show/update/create/delete) at the model level, see in the section on models.

If you want an individual filter to run on only one specific routing, e.g. user.index() or user.show(), you do the following:

module.exports = {

filter: {

show: function(res,res,next) {

// do your filtering here

// succeeded?

next();

// failed?

res.send(400);

// or else

next(error);

}

}

};

Of course, you will ask, why the split language? Why is all a first-level property, but each other filter is below the filter property? Well, all can go in either space. Both of the following are valid:

module.exports = {

// this one will get executed first

all: function(res,res,next) {

next();

},

filter: {

// then this one

all: function(res,res,next) {

next();

},

// and this one last

show: function(req,res,next) {

}

}

};

The order of execution is:

all()

filter.all()

filter.show() (or any other specific one)

Note: Filters will run, independent of any path you use to access the resource. So if you have the following defined for comment:

If you want to have filters that run on all resources, the above method will work - create a controller file for resourceA and another for resourceB and for resourceC, but that is pretty repetitive (and therefore not very DRY).

A better solution is "global filters". Global filters look exactly like per-resource filters, and have the exact same file structure - all, filter.all, filter.show, etc. - but apply to every resource. To enable it, when initializing booster, just tell it where it is:

A common use case is one where you want to do some post-processing before sending the response back to the client, for example, if you create with POST /users but before sending back that successful 201, you want to set up some activation stuff, perhaps using activator. Like with filters, you could override a controller method, like create, but then you lose all of the benefits.

The solution here is post-processors, methods that are called after a successful controller method, but before sending back the 200 or 201. Does booster support post-processors? Of course it does! (why are you not surprised?)

Like filters, post-processors are added as properties of the controller object.

module.exports = {

// use "post" to indicate post-processors

post: {

all: function(req,res,next) {}, // "all" will always be called

create: function(req,res,next) {}, // "create" will be called after each "create"

}

};

The order of execution is:

post.all()

post.create() (or any other specific one)

Post-processor signatures look slightly different than most handlers, since they need to know what the result of the normal controller method was.

function(req,res,next,body) {

};

req, res, next are the normal arguments to a route handler. body is the body returned by the controller method, if any. If you want the res status as set until now, you can retrieve it with the standard res.statusCode.

Your post-processor filter has 3 options:

Do nothing: if it does not alternate the response at all, just call next() as usual.

Error: as usual, you can always call next(error) to invoke expressjs's error handler.

Change the response: if it want to alternate the response, call res.send() or anything else.

What if you want to sort the results of a GET (i.e. index) - ascending or descending - by a specific field like age, or limit the results to only, say, the first 20 results?

Turns out it is pretty easy to do... because you don't need booster to do it.

One way to do it is to GET the entire thing and then sort it client-side, or do it server-side in post-processors, as we just saw.

r.get('/users').end(function(err,res){

// now we have all of the users over the network

// all users in descending order

var users = _.sortBy(res,"age").reverse();

// now get the first 20

users = users.slice(0,20);

});

But that is a royal pain, and incredibly inefficient to boot! You had to extract all of them from the database, send them all over the wire to the client (possibly a browser), then sort and reverse them all, then take the first 20. How would you like to do that with 10MM records?

You could save part of it by filtering it in the controller using post-processors:

{

post: {

index: function(req,res,next,body) {

if (res.statusCode === 200 && body && body.length > 0) {

// all users in descending order

var users = _.sortBy(body,"age").reverse();

// now get the first 20

users = users.slice(0,20);

// save it to the body

}

}

}

}

But you still need to do a lot of extra work in the post-processor, and you are still retrieving too many records over the nearby network from the data store.

The correct way to do this is to have the database do the sorting and filtering. Most database drivers support some form of sorting by a field and limiting the count. The only question, then, is how we get the correct parameters to the database driver.

The solution is simple: query parameters.

As you have seen earlier, and will see again below, every query parameter except those beginning with $b. are passed to model.find() and hence db.find() as is. In order to sort and filter, all you need to do is pass those parameters to the request query.

Here is an example. Let's say your database driver supports a search parameter sort. If sort is present, it will sort the response by the field, descending if negative:

{sort:"age"} - sort by age ascending
{sort:"-age"} - sort by age descending

Aha, you will ask, but does that not tie my REST API query directly to my database driver implementation? Where is the nimbleness I would get from indirection?!

Simple: the database "driver" that you pass to booster need not be the actual MySQL or Mongo or Oracle or whatever driver. It is just an object with a few key functions. Use your own that wraps the standard driver (that is what we do) . Then you can define your own terms and use them. Voilà.

Models are just standard representations of back-end data from the database. Like controllers, models are completely optional. If you don't provide a model file, the default will be used. If you prefer to design your own, create it in modelPath directory, either the default or the one you provided when initializing booster.

So what is in a model? Actually, a model is an automatically generated JavaScript object handler. It is driven by a config file in which you tell it which you tell booster, for this particular model, how to manage data: name in the database, id field, validations, what should be unique, etc.

name: what the name of this model should be in your database. Optional. Defaults to the name of the file, which is the name of the resource you created in booster.resource('name').

fields: what fields this model should have, and what validations exist around those fields

unique: what unique fields need to exist for this model

uniqueerror: whether or not to return an error for a unique conflict

id: what the ID field is. We need this so that we can use "unique" comparisons and other services. Optional. Defaults to "id".

presave: a function to be executed immediately prior to saving a model via update or create. Optional.

extend: an object, with functions that will extend the model. Optional. See below.

PUT update: This makes sense. You might be updating a single field, why should it reject it just because you didn't update them all? If the name and email fields are required, but you just want to update email, you should be able to PUT the follwing {email:"mynewmail@email.com"} without triggering any "missing field required" validation errors.

POST create and createoptional === true: If you flag a field as required, but also flag it as createoptional, then if you are creating it, validations will ignore the required flag. Well, that's why you set it up as createoptional in the first place, right?

If you specify a default value for a field, then if the field is unspecified, it will be set to this value. Some important points:

This only applies to PUT and POST. PATCH leaves the previous value.

This will only apply if there is no value given at all. If it is an empty string or 0 or some other "falsy" value, it will not be applied.

If the field is required, and there is a default value is specified, then if the PUT or POST value of the field is blank, the default value will be applied and the required condition will be satsified.

The filter option can be a little confusing, so here is a better explanation.

Let's say you have defined a resource play. Normal behaviour would be:

GET /play - list all of the play items

GET /play?somefield=foo - list all of the play items where somefield equals "foo"

However, you want it the other way around. You want GET /play to return only those items where somefield equals "foo", unless it explicitly gives something else.

GET /play - list all of the play items where somefield equals "foo"

GET /play?somefield=bar - list all of the play items where somefield equals "bar"

Setting the model as above with filter set on somefield will provide exactly this outcome:

somefield: {filter:{default:"foo"}}

But this creates a different problem. How do I tell the API, "send me all of the play items, whatever the value of somefield is"? Before adding the default filter, you would just do GET /play, but we added a filter to that!

The solution is to set a clear on the filter. Whatever the clear is set to, that is what, if the parameter is set, will match all. So using the setup from above:

somefield: {filter:{default:"foo",clear:"*"}}

We get the following:

GET /play - list all of the play items where somefield equals "foo"

GET /play?somefield=bar - list all of the play items where somefield equals "bar"

GET /play?somefield=* - list all of the play items

Of course, you can set the value of clear to anything you want: "*" (as in the above example), "any", "all", or "funny". Whatever works for you!

What you want is when you publish a post by changing its status from "draft" to "published", that all dependent comment elements are changed to published as well. Now, you might not want that, in which case, well, do nothing! But if you do...

Of course, you can do that by putting a post processor in the controller. For example, the controller for post might be:

That would work, but requires extra work and more error-handling than I have done here. If you want 3-4 levels of cascade - publishing a category leads to publishing child post leads to publishing child comment leads to (you get the idea) - it gets terribly messy and long. It also means that if you make the changes somewhere inside your program - i.e. not through the controller, say, by modifying the model from elsewhere, you won't catch it. What a mess!

Instead, let's go the simple route! booster allows you to say, "if I change this field to a certain value, then all children should get the same change."

This means: if we change this field's value to "published", then all instances of comment which have the value of post (since our type is post) equal to our id should have their status (since our field is status) changed to "published" as well.

Here are the parts of cascade you can set:

value: setting which value should trigger a cascade. This can be a string or an array of strings. If not set, then all value changes trigger a cascade.

children: which children who have a field named the same as our current model and a value the same as this item's id should be cascaded to. This can be a string or an array of strings. If children is not set, nothing is ever cascaded (since we have nothing to cascade it to!).

As stated earlier, a field that appears in the model for a create, update or patch, or in the data retrieved from the data store for a get or find, must be in the list of fields, or you will get an unknwon field error.

The exception is any field that starts with $b.. (Sound familiar? It should; it is the exact same flag we used for queries.

Any field whose name starts with $b. will be passed to presave and filters and post processors, but not to the validation functions or the database updates.

What could that be useful for? Well, what if you are creating a group, and business logic dictates that when a group is created, add the creator as a member.

POST /groups {name:"My Group"}

Now, how do you add the user as a member? Well, the second member looks like this:

POST /memberships {user:25}

But what about the logic for the first user? No problem, this is domain logic, so let's add it to the model post processors:

module.exports = {

fields: {

name: {required:true,validation:"string"}

},

post: {

create: function(model,models,err,res,callback) {

// assuming res = ID of newly created group

var uid = ???; // how do we get the user ID?!?!?

models.memberships.create({group:res,user:uid},callback);

}

}

}

In theory, we could do some strange "get the context" stuff, but that really does break MVC, not to mention dependency injection. The model should not have to know about things like that! Only the controller should.

So we want the controller to inject the user into the model, so the post-processor can handle it. No problem, I will use a controller filter:

module.exports = {

filter: {

create: function(req,res,next){

req.body.user = req.GetMyUser(); // the controller should know how to do this

}

}

}

You know what happens next, right? The field user was never defined as a field on the groups model, because it is not a property of the group. Our POST /groups {name:"My Group"} will return a 400 with the error {user:"unknownfield"}.

We need some way to have the filter add the user so it gets passed to the model filters and post-processors, but not the validators or database saves.

Hence $b.anything.

// controller

module.exports = {

filter: {

create: function(req,res,next){

req.body['$b.user'] = req.GetMyUser(); // the controller should know how to do this

// now it will safely skip the validators, but still get passed to filters and post-processors

Predefined validations are a single string or an array of strings that name validations to which to subject each model before sending persisting them to the database or accepting them from the database.

The following validations exist as of this writing:

notblank: Is not null, undefined or a string made up entirely of whitespace

notpadded: Does not start or end with whitespace

email: Is a valid email pattern. Does not actually check the email address. For example, fooasao12122323_12saos@gmail.com is a valid email pattern, but I am pretty sure that the address is not in use.

integer: must be a valid javascript number. Note: JavaScript does not distinguish between different number types; all numbers are 64-bit floats, so neither do we.

number: same as integer

float: same as integer

double: same as integer

alphanumeric: must be a valid alphanumeric a-zA-Z0-9

string: must be a string

boolean: must be a boolean true or false

array: must be an array

integerArray: must be an array, every element of which must be a valid integer

stringArray; must be an array, every element of which must be a valid alphanumeric

unique: must be an array, no element of which may be repeated more than once

minimum:<n>: must be a string of minimum length n

list:item,item,item,...,item: must be one of the string items in the list

If two or more validations are provided in an array, then all of the validations must pass (logical AND).

name: name of the model class you are validating, e.g. 'user' or 'post', helpful for generic functions.

field: name of the field you are validating, helpful for generic functions.

mode: what we were doing to get this mode for validating, helpful if you need to validate some things on save, but not load. One of: findgetupdatecreate 'patch`` (which are the exact db methods. Smart, eh?)

attrs: the JavaScript object you are validating.

callback: OPTIONAL. Async callback.

And what should the validation function return? It can return one of three things:

true: the validation passed, go ahead and do whatever else you were going to do

false: the validation did not pass, call the callback with an error indicating

object: the validation might or might not have passed, but we want to do some more work:

The returned object should have the following properties:

valid: true or false if the validation passed

value: if this exists, then the value of this key on the object should be changed to the provided value before moving on. See the example below.

message: if the validation failed (valid === false), then this is the message to be passed

Sync: If the arity (number of arguments) of a validation function is four, then it is treated as synchronous, and the validation return is the return value of the function.

Async: If the arity of a validation function is five, then it is treated as asynchronous, and the fifth argument is the callback function. The callback function should have one argument exactly, the true/false/object that would be returned.

Note that sync is likely to be deprecated in future versions.

The classic example for this is a password field. Let's say the user updated their password, we don't just want to validate the password as alphanumeric or existing, we want to do two special things:

We want to validate that the password is at least 8 characters (which john's new password is not)

We want to one-way hash the password before putting it in the database

Our validation function will look like this:

module.exports = {

fields: {

id: {required:true, validation: "integer"},

name: {required:true, validation: "alphanumeric"},

password: {required:true, validation: function(name,field,attrs){

var valid, newpass = attrs[field];

// in create or update, we are OK with no password, but check length and hash it if it exists

if (mode === "create" || mode === "update") {

if (newpass === undefined) {

valid = true;

} else if (newpass.length < 8>) {

valid = {valid:false,message:"password_too_short"};

} else {

valid = {valid:true, value: hashPass(newpass)}; // we want it set to "assde232shwsww1323"

}

} else {

// in get or find mode, we accept whatever the password is

valid = true;

}

return(valid);

}}

}

}

So if the user sends us:

PUT /user/123 {id:"10",name:"john",password:"poorpw"}

then the response from the validation function will be {valid:false,message:"password_too_short"}, the update() will fail, and the error passed back to the calling controller and hence function will be "password_too_short".

On the other hand, if the user sends us a good password:

PUT /user/123 {id:"10",name:"john",password:"longerpwisgood"}

Then the response from the validation function will be {valid:true,value:"assde232shwsww1323"}. The final record stored in the database will be:

In addition to the usual validations - predefined and function - you have the option to create "parent" validations. A field in one item is only allowed a certain value if the field in another related item matches it.

This is best explained with an example.

You have a model called category and another called article. Each article belongs to one, and just one, category (for the database-minded, N:1 relationship). In addition, both articles and categories have a field called status, which can be set to "published" or "draft". Well, then, your models look like this:

// category

{

fields: {

id: {required:true},

name: {required:true, validation:"alphanumeric"},

status: {validation:"list:draft,published"}

}

}

// article

{

fields: {

id: {required:true},

category: {required:true}, // which category this article is part of

name: {required:true, validation:"alphanumeric"},

status: {validation:"list:draft,published"}

}

}

You want to ensure that even though an article could be draft or published, do not let someone set an article to "published" unless its category is also already published. Makes sense right? No one can see the article, unless they already can see the category. In other words, this is a more restrictive validation.

The field status must pass the usual validations - one of "draft" or "published"and it must have its category already published.

You know booster will help you do this, right? Sure you do! Just tell the article it cannot be published if the parent is not also:

// article

{

fields: {

id: {required:true},

category: {required:true}, // which category this article is part of

name: {required:true, validation:"alphanumeric"},

status: {validation:

{valid:"list:draft,published",parent:"category",check:"published"}

}

}

}

The above means, "only accept as valid one of 'draft' or 'published'. Once you passed that check, get the model category (our 'parent'), and, if we are setting our status to 'published', check that the parent is also 'published'".

Note that we have to use the object format for validation here; the simplified format will not work.