Skip to content

AoJ/Amanda

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

64 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Amanda

Amanda validates data against JSON Schema.

Features

  • 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

Example

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

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 13.96 KB (3.22 KB gzipped) Download
amanda.min.js compressed, without comments 5.82 KB (1.96 KB gzipped) Download

Documentation

Methods

Objects

Validate

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

Parameters

  • data
  • schema The Schema object, see Schema below.
  • options If you set options.singleError to false, validation continue after first error occurred. By default options.singleError is set to true.
  • 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

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

GetVersion

getVersion()

Example

amanda.getVersion(); // => '0.2.0'

GetValidators

getValidators()

Example

amanda.getValidators(); // => { type: function() {}, ... }

Schema

Validators

Required

var schema = {
  type: 'object',
  properties: {
    username: {
      required: true
    }
  }
};

Length

Example

var schema = {
  type: 'object',
  properties: {
    username: {
      type: 'string',
      length: [2, 45]
    }
  }
};

Type

  • object
  • array
  • string
  • number
  • function
  • boolean

Format

  • alpha
  • alphanumeric
  • ipv4
  • ipv6
  • ip
  • email
  • url
  • date
  • decimal
  • int
  • percentage
  • port
  • regexp
  • unsignedInt

Enum

Example

var schema = {
  type: 'object',
  properties: {
    sex: {
      type: 'string',
      values: ['female', 'male']
    }
  }
};

Except

Example

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

Min

Example

var schema = {
  type: 'number',
  min: 100
};

Max

Example

var schema = {
  type: 'number',
  max: 100
};

Pattern

Example

var schema = {
  type: 'string',
  pattern: /^[a]{2,4}$/
};

Error

Methods

Example

{
  '0': {
    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']

Compatibility

Node.js

From version 0.4.11.

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

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