This package has been deprecated

Author message:

Please note that this package is no longer maintained. You may use webapi-parser or amf-client-js instead.

raml-validate

1.3.0 • Public • Published

RAML Validate

NPM version Build status Test coverage Greenkeeper badge

Strict and pluginable validation of RAML 0.8 named parameters and RAML 1.0 built-in types.

Installation

npm install raml-validate --save

Usage

You must require the module and call it as a function to get a validation instance back.

var validate = require('raml-validate')();
 
// RAML version to use, either 'RAML10' or 'RAML08' (default)
var RAMLVersion = 'RAML10'
 
// Create a user model schema.
var user = validate({
  username: {
    type: 'string',
    minLength: 5,
    maxLength: 50,
    required: true
  },
  password: {
    type: 'string',
    minLength: 5,
    maxLength: 50,
    required: true
  }
}, RAMLVersion);
 
// Validate a user model.
user({
  username: 'blakeembrey',
  password: 'super secret password'
}); //=> { valid: true, errors: [] }

Module does not currently support wild-card parameters (RAML 0.8) and regular expression patterns in property declaration (RAML 1.0)

Getting validation errors

All validation errors can be retrieved from the errors property on the returned object. If valid === false, the errors will be set to an array. This can be useful for generating error messages for the client.

[
  {
    valid: false,
    key: 'password',
    value: 'test',
    rule: 'minLength',
    attr: 5
  }
]

Required validation

If the validation does not set required to be true, a null or undefined value will be valid.

Union types (RAML 1.0 only)

The module supports the 'union' type as defined in datatype-expansion's algorithm.

validate({
  userId: {
    type: 'union',
    anyOf: [
      { type: 'string' },
      {
        type: 'integer',
        maximum: 100
      }
    ]
  }
});

Repeated validation (RAML 0.8 only)

The module has core support for repeated properties in the form of an array. If the validation is set to repeat, but does not receive an array - validation will fail with a repeat error.

Multiple types (RAML 0.8 only)

The module supports multiple types according to the RAML spec (see RAML 0.8 named parameters with multiple types. When multiple types are specified, it'll run the validation against the matching type.

validate({
  file: [{
    type: 'string'
  }, {
    type: 'file'
  }]
});

If any of the types are set to repeat, it'll only run that validation object when every value in the array is of the correct type - otherwise you will receive a type error.

Adding new types (RAML 0.8 only)

New type validations can be added by setting the corresponding property on the validate.TYPES object. For example, adding file validation to support buffers can be added by doing:

validate.TYPES.file = function (value) {
  return Buffer.isBuffer(value);
};

The function must accept the value as the first parameter and return a boolean depending on success or failure.

Adding new rules (RAML 0.8 only)

New rules can be added by setting the corresponding property on the validate.RULES object. For example, to add file size support we can do the following:

validate.RULES.minFileSize = function (size) {
  return function (value) {
    return value.length <= size;
  };
};

The function must accept the validation value as its only parameter and is expected to return another function that implements the validation logic. The returned function must accept the value as the first argument, and can optionally accept the key and model as the second and third arguments. This is useful for implementing a rule such as requires, where both parameters may be optional; however, when set, depend on eachother being set.

validate.RULES.requires = function (property) {
  return function (value, key, object) {
    return value != null && object[property] != null;
  };
};

License

Apache 2.0

Readme

Keywords

Package Sidebar

Install

npm i raml-validate

Weekly Downloads

981

Version

1.3.0

License

Apache-2.0

Unpacked Size

44.8 kB

Total Files

7

Last publish

Collaborators

  • cesaraugustogarcia
  • jstoiko
  • mulesoft-npm