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.1
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: '[email protected]',
username: 'kenneth'
};
amanda.validate(data, schema, function(error) {
// Do something...
});
To install Amanda, use NPM.
$ npm install amanda
Releases are available for download from GitHub.
Version | Description | Size | Action |
---|---|---|---|
amanda.js |
uncompressed, with comments | 13.96 KB (3.22 KB gzipped) | Download |
amanda.min.js |
compressed, without comments | 5.82 KB (1.96 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
var schema = {
type: 'object',
properties: {
username: {
required: true
}
}
};
Example
var schema = {
type: 'object',
properties: {
username: {
type: 'string',
length: [2, 45]
}
}
};
object
array
string
number
function
boolean
alpha
alphanumeric
ipv4
ipv6
ip
email
url
date
decimal
int
percentage
port
regexp
unsignedInt
Example
var schema = {
type: 'object',
properties: {
sex: {
type: 'string',
values: ['female', 'male']
}
}
};
Example
var schema = {
type: 'object',
properties: {
username: {
type: 'string',
except: ['admin', 'administrator']
}
}
};
Example
var schema = {
type: 'number',
min: 100
};
Example
var schema = {
type: 'number',
max: 100
};
Example
var schema = {
type: 'string',
pattern: /^[a]{2,4}$/
};
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)
(The MIT License)
Copyright (c) 2011 František Hába <[email protected]>
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.