npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

raml-validate

v1.3.0

Published

Strict validation of RAML parameters

Downloads

4,798

Readme

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