Skip to content

Latest commit

 

History

History
73 lines (62 loc) · 1.47 KB

validate.md

File metadata and controls

73 lines (62 loc) · 1.47 KB

← Documentation

Validate

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

Parameters

  • data
  • schema The Schema object, see Schema.
  • options The Options object, see Options.
  • callback The callback gets one argument which is an Error object (see Error. for more information).

Example

/**
 * Schema
 */
var schema = {
  type: 'object',
  properties: {
    user: {
      name: {
        required: true,
        type: 'string',
        minLength: 2,
        maxLength: 45
      },
      surname: {
        required: true,
        type: 'string',
        minLength: 2,
        maxLength: 45
      }
    }
  }
};

/**
 * Data
 */
var data = {
  user: {
    name: 'František',
    surname: 'Hába'
  }
};


var jsonSchemaValidator = amanda('json');

// Stop the validation process after a first error
jsonSchemaValidator.validate(data, schema, function(error) {
  if (error) {
    // Do something...
  } else {
    // Do something else...
  }
});

// Validate the whole schema
jsonSchemaValidator.validate(data, schema, { singleError: false }, function(error) {
  if (error) {
    // Do something...
  } else {
    // Do something else...
  }
});