Amanda validates data against JSON Schema.
- Extendable, you can create your own validators
- Fully asynchronous
- Can be used with Node.js and in the browser
- Amanda has no dependencies
- AMD compatible, you can load it via RequireJS
- Lightweight
- Fully documented
- Tested
Version
0.2.2
var schema = {
type: 'object',
properties: {
name: {
required: true,
type: 'string',
length: [2, 45]
},
email: {
required: true,
type: 'string',
format: 'email'
},
username: {
required: true,
type: 'string',
format: 'alphanumeric'
}
}
};
var data = {
name: 'Kenneth',
email: 'kenneth@gmail.com',
username: 'kenneth'
};
amanda.validate(data, schema, function(error) {
// Do something...
});
You can find more examples in the /examples/ folder.
To install Amanda, use NPM.
$ npm install amanda
Releases are available for download from GitHub.
Version | Description | Size | Action |
---|---|---|---|
amanda.js |
uncompressed, with comments | 15.06 KB (3.42 KB gzipped) | Download |
amanda.min.js |
compressed, without comments | 6.2 KB (2.09 KB gzipped) | Download |
Methods
Objects
Parameters
data
schema
The Schema object, see Schema below.options
If you setoptions.singleError
tofalse
, validation continue after first error occurred. By defaultoptions.singleError
is set totrue
.callback
Thecallback
gets one argument which is an Error object (see Error below for more information).
Example
/**
* Schema
*/
var schema = {
type: 'object',
properties: {
user: {
name: {
required: true,
type: 'string',
length: [2, 45]
},
surname: {
required: true,
type: 'string',
length: [2, 45]
}
}
}
};
/**
* Data
*/
var data = {
user: {
name: 'František',
surname: 'Hába'
}
};
// Stop validation after first error
amanda.validate(data, schema, function(error) {
if (error) {
// Do something...
} else {
// Do something else...
}
});
// Validate whole schema
amanda.validate(data, schema, { singleError: false }, function(error) {
if (error) {
// Do something...
} else {
// Do something else...
}
});
This method allows you to add a custom validator.
Example
var evenValidator = function(property, propertyValue, validator, propertyValidators, callback) {
// If ‘even: true’
if (validator) {
if (typeof propertyValue === 'number' && (propertyValue % 2) === 0) {
// No problem, the number is event
callback();
} else {
// The number is not even
callback('Not even.');
}
// Skip
} else {
return callback();
}
};
// Add a new validator
amanda.addValidator('even', evenValidator);
var schema = {
type: 'object',
properties: {
name: {
type: 'string',
length: [2, 45],
even: true // <= That's your validator
}
}
}
Example
amanda.getVersion(); // => '0.2.0'
Example
amanda.getValidators(); // => { type: function() {}, ... }
Validators
- required
- length
- format
- type
- enum
- except
- minimum
- maximum
- pattern
- maxItems
- minItems
- exclusiveMinimum
- exclusiveMaximum
- divisibleBy
- uniqueItems
This attribute indicates if the instance must have a value, and not be undefined. This is
false
by default, making the instance optional.
Examples
var schema = {
type: 'object',
properties: {
username: {
required: true
}
}
};
When the instance value is a string, this defines the length of the string.
Examples
var schema = {
type: 'object',
properties: {
username: {
type: 'string',
length: [2, 45]
}
}
};
var schema = {
type: 'string',
length: 25
};
This attribute defines what the primitive type or the schema of the instance must be in order to validate. A string indicating a primitive or simple type. The following are acceptable string values:
object
Value must be an object.array
Value must be an array.string
Value must be a string.number
Value must be a number, floating point numbers are allowed.function
Value must be a function.boolean
Value must be a boolean.
Examples
var schema = {
type: 'object',
properties: {
username: {
// ...
}
}
};
var schema = {
type: 'array',
items: {
// ...
}
};
var schema = {
type: 'string'
};
This property defines the type of data, content type, or microformat to be expected in the instance property values. The following formats are predefined:
alpha
alphanumeric
ipv4
ipv6
ip
email
url
date
decimal
int
percentage
port
regexp
unsignedInt
Examples
var schema = {
type: 'string',
format: 'email'
};
This provides an enumeration of all possible values that are valid for the instance property. This must be an array, and each item in the array represents a possible value for the instance value.
Examples
var schema = {
type: 'object',
properties: {
sex: {
type: 'string',
enum: ['female', 'male']
}
}
};
Examples
var schema = {
type: 'object',
properties: {
username: {
type: 'string',
except: ['admin', 'administrator']
}
}
};
This attribute defines the minimum value of the instance property when the type of the instance value is a number.
Examples
var schema = {
type: 'number',
minimum: 100
};
This attribute defines the maximum value of the instance property when the type of the instance value is a number.
Examples
var schema = {
type: 'number',
maximum: 100
};
When the instance value is a string, this provides a regular expression that a string instance must match in order to be valid.
Examples
var schema = {
type: 'string',
pattern: /^[a]{2,4}$/
};
This attribute defines the maximum number of values in an array when the array is the instance value.
Examples
var schema = {
type: 'array',
maxItems: 10
};
This attribute defines the minimum number of values in an array when the array is the instance value.
Examples
var schema = {
type: 'array',
minItems: 10
};
This attribute indicates if the value of the instance (if the instance is a number) can not equal the number defined by the maximum attribute. This is false by default, meaning the instance value can be less then or equal to the maximum value.
Examples
var schema = {
type: 'number',
maximum: 100,
exclusiveMaximum: true
};
This attribute indicates if the value of the instance (if the instance is a number) can not equal the number defined by the minimum attribute. This is false by default, meaning the instance value can be greater then or equal to the minimum value.
Examples
var schema = {
type: 'number',
minimum: 100,
exclusiveMinimum: true
};
This attribute indicates that all items in an array instance must be unique (contains no two identical values).
Examples
var schema = {
type: 'array',
uniqueItems: true
};
This attribute defines what value the number instance must be divisible by with no remainder (the result of the division must be an integer).
Examples
var schema = {
type: 'number',
divisibleBy: 2
};
Examples
var schema = {
type: 'number',
maximum: 100,
exclusiveMaximum: true
};
Methods
Example
{
'0': {
property: 'users[0].username'
propertyValue: 123
validator: 'type'
validatorValue: 'string',
message: 'Only string is allowed'
}
}
Example
error.getProperties(); // => ['users[0].username']
Example
error.getMessages(); // => ['Only string is allowed']
From version 0.4.11.
Desktop
Browser | Supported | Version |
---|---|---|
Google Chrome | ✔ | 12+ |
Safari | n/a | Not tested |
Firefox | n/a | Not tested |
Opera | n/a | Not tested |
Internet Explorer | ✕ | Not tested |
Testing in progress...
$ npm test
The following are the major contributors of Amanda (in alphabetical order).
- František Hába (@Baggz) hello@frantisekhaba.com
(The MIT License)
Copyright (c) 2011 František Hába <hello@frantisekhaba.com>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.