Skip to content

zzen/Amanda

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Amanda

Build Status

Contents

Amanda validates data against JSON Schema.

Features

  • Extendable, you can create your own validators
  • 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

Example

/**
 * Schema
 */
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'
    }
  }
};

/**
 * Data
 */
var data = {
  name: 'Kenneth',
  email: '[email protected]',
  username: 'kenneth'
};

// Validate the data against the schema
amanda.validate(data, schema, function(error) {
  // Do something...
});

You can find more examples in the /examples/ folder.

Download

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

Usage

Browser

<script src="amanda.js"></script>

Node.js, RingoJS, Narwhal

var amanda = require('amanda');

RequireJS

// Configuration options, the path should not include the .js extension
require.config({
  paths: {
    "robb": "path/to/amanda"
  }
});

// Load Amanda
require(['amanda'], function(amanda) {

  // Do something...

});

Documentation

Methods

Objects

Validate

validate(data, schema[, options], callback)

Parameters

  • data
  • schema The Schema object, see Schema below.
  • options The Options object, see Options below.
  • callback The callback 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...
  }
});

AddValidator

addValidator(name, fn)

This method allows you to add a custom validator.

Example

/**
 * EventValidator
 *
 * @param {string} property
 * @param {object|number|boolean|string} propertyValue
 * @param {object} validator
 * @param {object} propertyValidators
 * @param {function} callback
 */
var evenValidator = function(property, propertyValue, validator, propertyValidators, callback) {
  
  // If ‘even: true’
  if (validator) {
    
    if (typeof propertyValue === 'number' && (propertyValue % 2) === 0) {
      // The number is even
      callback();
    } else {
      // The number is not even
      callback('Not even.');
    }

  // Skip
  } else {
    return callback();
  }

};

// Add a new validator
amanda.addValidator('even', evenValidator);

/**
 * Schema
 */
var schema = {
  type: 'object',
  properties: {
    name: {
      type: 'string',
      length: [2, 45],
      even: true // <= That's your validator
    }
  }
}

Schema

Validators


Required

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
    }
  }
};

MinLength

When the instance value is a string, this defines the minimum length of the string.

Example

{
  type: 'string',
  minLength: 10
}

MaxLength

When the instance value is a string, this defines the maximum length of the string.

Example

{
  type: 'string',
  maxLength: 45
}

Length

When the instance value is a string, this defines the length of the string.

Example

{
  type: 'string',
  length: 5
}

Type

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:

  • string Value must be a string.
  • number Value must be a number, floating point numbers are allowed.
  • integer Value MUST be an integer, no floating point numbers are allowed. This is a subset of the number type.
  • boolean Value must be a boolean.
  • object Value must be an object.
  • array Value must be an array.
  • function Value must be a function.

Examples

var schema = {
  type: 'object',
  properties: {
    username: {
      // ...
    }
  }
};
var schema = {
  type: 'array',
  items: {
    // ...
  }
};
var schema = {
  type: 'string'
};

Format

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'
};

Enum

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']
    }
  }
};

Except

Examples

var schema = {
  type: 'object',
  properties: {
    username: {
      type: 'string',
      except: ['admin', 'administrator']
    }
  }
};

Minimum

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
};

Maximum

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
};

Pattern

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}$/
};

MaxItems

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
};

MinItems

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
};

ExclusiveMaximum

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
};

ExclusiveMinimum

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
};

UniqueItems

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
};

DivisibleBy

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
};

Error

Methods

Example

[
  {
    property: 'users[0].username'
    propertyValue: 123
    validator: 'type'
    validatorValue: 'string',
    message: 'Only string is allowed'
  },
  {
    // ...
  }
]

getProperties

Example

error.getProperties(); // => ['users[0].username']

getMessages

Example

error.getMessages(); // => ['Only string is allowed']

Options

Properties

singleError

If you set singleError to false, validation continue after first error occurred. By default singleError is set to true.

Example

amanda.validate(data, schema, { singleError: false }, function(error) {

  // Do something...

});

messages

This property allows you to set custom error messages. If you want to use more ambitious messages, you can pass a function.

Placeholders

  • {{property}}
  • {{propertyValue}}
  • {{validator}}

Example

/**
 * 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.

    // 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.
    }

  }
};

// Validate the data agains the schema and use custom error messages
amanda.validate(data, schema, options, function(error) {
  // Do something...
});

Compatibility

Node.js

From version 0.6.0.

Browsers

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...

Tests

$ npm test

Contributors

The following are the major contributors of Amanda (in alphabetical order).

License

(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.

Packages

No packages published

Languages

  • JavaScript 100.0%