Skip to content

Commit

Permalink
Update the documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
František Hába committed Mar 15, 2012
1 parent cfa1a5e commit e428d78
Show file tree
Hide file tree
Showing 7 changed files with 276 additions and 233 deletions.
27 changes: 22 additions & 5 deletions docs/README.md
@@ -1,16 +1,33 @@
<a name="documentation"></a>
# Documentation

### Engines

* [JSON Schema](#jsonSchema)

<a name="jsonSchema"></a>
## JSON Schema

diff.md

### Example

```
// Initialize a JSON Schema validator
var jsonSchemaValidator = amanda('json');
```

<a name="methods"></a>
## Methods
### Methods

* [validate](#validate)
* [addValidator](#addValidator)
* [validate](https://github.com/Baggz/Amanda/tree/master/docs/json/methods/validate.md#validate)
* [addAttribute](https://github.com/Baggz/Amanda/tree/master/docs/json/methods/validate.md#addAttribute)
* [addAttributeConstructor](https://github.com/Baggz/Amanda/tree/master/docs/json/methods/validate.md#addAttributeConstructor)

<a name="objects"></a>
## Objects
### Objects

* [Schema]((https://github.com/Baggz/Amanda/tree/master/docs/objects/schema.md#schema)
* [Schema](https://github.com/Baggz/Amanda/tree/master/docs/objects/schema.md#schema)
* [required](https://github.com/Baggz/Amanda/tree/master/docs/objects/schema.md#required)
* [minLength](https://github.com/Baggz/Amanda/tree/master/docs/objects/schema.md#minLength)
* [maxLength](https://github.com/Baggz/Amanda/tree/master/docs/objects/schema.md#maxLength)
Expand Down
65 changes: 65 additions & 0 deletions docs/json/methods/addAttribute.md
@@ -0,0 +1,65 @@
[← Documentation](https://github.com/Baggz/Amanda/tree/master/docs/README.md)

<a name="addAttribute"></a>
# addAttribute

```javascript
addAttribute(attributeName, attributeFn)
```

This method allows you to add a custom attribute.

**Example**

```javascript
/**
* EvenAttribute
*
* @param {string} property
* @param {any} propertyValue
* @param {any} attributeValue
* @param {object} propertyAttributes
* @param {function} callback
*/
var evenAttribute = function(property, propertyValue, attributeValue, propertyAttributes, callback) {

// If ‘even: true’
if (attributeValue) {
if (typeof propertyValue === 'number' && (propertyValue % 2) !== 0) {
this.addError();
}
}

// Continue...
return callback();

};

// Add a new validator
var jsonSchemaValidator = amanda('json');
jsonSchemaValidator.addAttribute('even', evenAttribute);

/**
* Schema
*/
var schema = {
type: 'object',
properties: {
items: {
type: 'number',
even: true // <= That's your attribute
}
}
};
```

## Attribute API

* `this.addError`
* `this.attributes`
* `this.instance`
* `this.schema`
* `this.joinPath`
* ...

> In progress...
7 changes: 5 additions & 2 deletions docs/methods/validate.md → docs/json/methods/validate.md
Expand Up @@ -50,8 +50,11 @@ var data = {
}
};


var jsonSchemaValidator = amanda('json');

// Stop the validation process after a first error
amanda.validate(data, schema, function(error) {
jsonSchemaValidator.validate(data, schema, function(error) {
if (error) {
// Do something...
} else {
Expand All @@ -60,7 +63,7 @@ amanda.validate(data, schema, function(error) {
});

// Validate the whole schema
amanda.validate(data, schema, { singleError: false }, function(error) {
jsonSchemaValidator.validate(data, schema, { singleError: false }, function(error) {
if (error) {
// Do something...
} else {
Expand Down
20 changes: 9 additions & 11 deletions docs/objects/error.md → docs/json/objects/error.md
Expand Up @@ -3,28 +3,26 @@
<a name="error"></a>
# Error

### Methods

* [getProperties](#getProperties)
* [getMessages](#getMessages)

**Example**

```javascript
[
{
property: 'users[0].username'
propertyValue: 123
validator: 'type'
validatorValue: 'string',
message: 'Only string is allowed'
attributeName: 'type'
attributeValue: 'string',
message: 'Lorem ipsum dolor isamet pide quidu delime.'
},
{
// ...
}
...
]
```

### Methods

* [getProperties](#getProperties)
* [getMessages](#getMessages)

<a name="getProperties"></a>
## getProperties [#](#getProperties)

Expand Down
21 changes: 9 additions & 12 deletions docs/objects/options.md → docs/json/objects/options.md
Expand Up @@ -11,7 +11,7 @@
<a name="singleError"></a>
## singleError [#](#singleError)

If you set `singleError` to `false`, validation continue after first error occurred. By default `singleError` is set to `true`.
If you set the `singleError` flag to `false`, validation continues even after a first error occurred. By default the `singleError` flag is set to `true`.

**Example**

Expand All @@ -32,7 +32,7 @@ This property allows you to set custom error messages. If you want to use more a

* `{{property}}`
* `{{propertyValue}}`
* `{{validator}}`
* `{{attributeValue}}`

**Example**

Expand All @@ -41,21 +41,18 @@ This property allows you to set custom error messages. If you want to use more a
* Options
*/
var options = {

messages: {

// Custom error message as a string (with placeholders)
format: 'Uh oh! Param ‘{{property}}’ must be an {{propertyValue}}.' // Uh oh! Param ‘email’ must be an email.
// A custom error message as a string (with placeholders)
format: 'Uh oh! The property ‘{{property}}’ must be an {{attributeValue}}. Well, ‘{{propertyValue}}’ doesn't look like an email. ' // Uh oh! Param ‘email’ must be an email.
// Custom error message as a function
enum: function(property, propertyValue, validator) {
return 'Value of the ‘' + property + '’ property must be ' + validator.join(' or ') + '.'; // Value of the ‘sex’ property must be male or female.
// A custom error message as a function
enum: function(property, propertyValue, attributeValue) {
return 'Value of the ‘' + property + '’ property must be ' + attributeValue.join(' or ') + '.'; // A value of the ‘sex’ property must be male or female.
}
}
};
// Validate the data agains the schema and use custom error messages
amanda.validate(data, schema, options, function(error) {
// Do something...
});
};
```

0 comments on commit e428d78

Please sign in to comment.