RAML Validate
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 = ; // RAML version to use, either 'RAML10' or 'RAML08' (default)var RAMLVersion = 'RAML10' // Create a user model schema.var user = ; // Validate a user model.; //=> { 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.
;
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.
;
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:
validateTYPES { return Buffer;};
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:
validateRULES { return { return valuelength <= 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.
validateRULES { return { return value != null && objectproperty != null; };};
License
Apache 2.0