1
0
Fork 0

Adjusted Foxx Model docs to use joi instead of legacy schema.

This commit is contained in:
Alan Plum 2014-07-08 14:06:05 +02:00
parent 08d3be148c
commit 2711ee88e4
1 changed files with 15 additions and 13 deletions

View File

@ -16,21 +16,22 @@ exports.model = TodoModel;
A Foxx Model can be initialized with an object of attributes and their values. A Foxx Model can be initialized with an object of attributes and their values.
There's also the possibility of annotation: The second hash you give to the There's also the possibility of annotation: If you extend the model with a
extend method are properties of the prototype. You can define an attributes *schema* property, the model's attributes will be validated against it.
property there:
You can define attributes in the schema using the bundled *joi* library.
For more information on the syntax see [the official joi documentation](https://github.com/spumko/joi).
```js ```js
var Foxx = require("org/arangodb/foxx"); var Foxx = require("org/arangodb/foxx");
var joi = require("joi");
var PersonModel = Foxx.Model.extend({ var PersonModel = Foxx.Model.extend({
// Your instance properties schema: {
}, { name: joi.string().required(),
// Your prototype properties age: joi.number().integer(),
attributes: { active: joi.boolean().default(true)
name: { type: "string", required: true }, }
age: { type: "integer" },
active: { type: "boolean", defaultValue: true }
}); });
exports.model = TodoModel; exports.model = TodoModel;
@ -39,8 +40,8 @@ exports.model = TodoModel;
This has two effects: On the one hand it provides documentation. If you annotated This has two effects: On the one hand it provides documentation. If you annotated
your model, you can use it in the *bodyParam* method for documentation. your model, you can use it in the *bodyParam* method for documentation.
On the other hand it will influence the behavior of the constructor: If you provide On the other hand it will influence the behavior of the constructor: If you provide
an object to the constructor, it will only take those attributes that are listed an object to the constructor, it will validate its attributes and set the special
in the attributes object. This is especially useful if you want to to initialize *errors* property. This is especially useful if you want to to initialize
the Model from user input. On the other hand it will set the default value for all the Model from user input. On the other hand it will set the default value for all
attributes that have not been set by hand. An example: attributes that have not been set by hand. An example:
@ -50,7 +51,8 @@ var person = new PersonModel({
admin: true admin: true
}); });
person.attributes // => { name: "Pete", active: true } person.attributes // => { name: "Pete", admin: true, active: true }
person.errors // {admin: [ValidationError: value is not allowed]}
``` ```
!SUBSECTION Extend !SUBSECTION Extend