o-validator
v1.0.1
Published
Flexible and lightweight object validation library.
Downloads
1,371
Maintainers
Readme
o-validator
Flexible and lightweight object validation library.
Build validators from a generic low-level API, or use helpful pre-built validation functions. This library makes no assumptions about the structure of the provided data, and instead lets the consumer define how their data should be inspected by validating properties with common predicate functions. Based off of functional principles, no special syntax required.
Install
$ npm install o-validator --save
Usage
var V = require('o-validator');
var R = require('ramda');
var hasLengthGreaterThan = R.curry(function(n, x) {
return R.propSatisfies(R.lt(n), 'length', x);
});
var schema = {
title : V.required(R.is(String)),
description : R.allPass([R.is(String), hasLengthGreaterThan(5)]),
isActive : R.is(Boolean),
tags : R.is(Array)
};
V.validate(schema, {
title : 'Hi There',
description : 'This is a great post.',
isActive : true
// tags are not defined - but that is OK, validator treats them as optional
});
// => true
The validator runs each argument against the defined validation schema, asserting a true outcome for each. Properties defined in the validation schema are assumed to be optional unless declared otherwise using Validator.required
.
Note: this module is best used with a functional library to provide predicates (isString, isNull, etc.), such as ramda.
Functions are curried by default
All methods in this library are curried, which means that if a method is called without all the arguments it requires, the return value will be a function that takes the remaining arguments. This is very helpful in general, and in the case of this library it makes it possible to create validator functions that can be saved and used at a later time.
For example, one could write the previous validation like this:
var V = require('o-validator');
// construct validation function and save it for later
var validateArgs = V.validate(<schema>);
// use (and re-use) previously constructed validation function
validadeArgs(<object>); // => Boolean
Types
Type definitions used in this module:
-- Function that takes a value and produces a boolean
Predicate (a -> Boolean)
-- An object with Predicates as values. These are provided to validation
-- functions, and are used to define how input data will be validated
Schema {k: Predicate}
-- An object that contains information about a validation failure
ErrorObject {property: String, errorCode: String}
-- An ErrorObject that also includes a message property
ErrorObjectWithMessage {property: String, errorCode: String, message: String}
Docs
As noted previously, all provided methods in this library are curried. The type signatures are written to reflect that.
Validator.validate
Schema -> Object -> Boolean
Validates a data object against the provided schema, returning a boolean value to indicate if the supplied data object is valid.
Note: when partially applied with a schema, this function produces a Predicate
. As such, it can be used to recursively validate objects with nested properties.
Validator.validate(<schema>, <object>) -> Boolean
Validator.getErrors
Schema -> Object -> [ErrorObjectWithMessage]
Returns a list of validation errors produced from validating the data object against the provided schema. Error objects contain information about the validation error, including the offending property, and what sort of validation error occurred (see Validator.errorCodes
). If no errors are found, the method returns an empty array.
Note: this method attaches default error messages to the error objects.
Validator.getErrors(<schema>, <object>) -> [ErrorObjectWithMessage]
Validator.validateOrThrow
Schema -> Object -> Error or Object
Throws an error containing information about any validation errors, if found. Otherwise returns the original input arguments.
Note: the final error message is built using the default error messages.
// Invalid args
Validator.validateOrThrow(<schema>, <object>) -> Error
// Valid args
Validator.validateOrThrow(<schema>, <object>) -> <object>
Validator.validateWithErrorHandler
([ErrorObject] -> a) -> Schema -> Object -> a or Object
Low level function for creating a validation with a custom error handler. Invokes the supplied error handler if any validation errors are found, otherwise returns the original arguments. Error handling function will be passed an array of ErrorObject
s.
Note: error handler should be a function that takes an array of errors, and does something with them [ErrorObject] -> a
.
// Invalid args
Validator.validateWithErrorHandler(<error-handler>, <schema>, <object>) -> <error-handler-result>
// Valid args
Validator.validateWithErrorHandler(<error-handler>, <schema>, <object>) -> <object>
Validator.addDefaultErrorMessages
[ErrorObject] -> [ErrorObjectWithMessage]
Utility function that adds default error messages to a list of errors. Useful when building a custom validation using validateWithErrorHandler
.
Validator.errorCodes
{k: String}
Error codes that define the type of validation error that was found. Used to populate ErrorObject.errorCode
. Useful when building a custom validation using validateWithErrorHandler
.
{
REQUIRED : 'REQUIRED',
UNSUPPORTED : 'UNSUPPORTED',
VALUE : 'VALUE',
UNKNOWN : 'UNKNOWN'
}
Validator.required
(type not exposed, implementation details are internal to this library)
Specifies that a property in the schema is required. Note: by default properties as assumed to be optional.
var validateArgs = Validator.validate({
title : Validator.required(isString)
description : isString
});
// when the validator is invoked, a title property must be supplied,
// while the description property is optional
In depth example
As noted above, this library aims to provide a generic low-level API so that almost any use case can be accommodated. For example, we could easily build a validation function that throws errors using the default error message library, but also includes information about the original top level object in its error response.
// custom-validator.js
var V = require('o-validator');
var R = require('ramda');
var getDefaultErrorMessages = R.compose(R.pluck('message'), V.addDefaultErrorMessages);
// custom validation error handler
// prefixes error messages with a provided top level identifier
var customErrorHandler = R.curry(function(identifier, errors) {
var errorMessages = getDefaultErrorMessages(errors);
var message = 'Could not validate "' + identifier + '" - ' + R.join('; ', errorMessages);
throw new Error(message);
});
// custom validation function
var validateWithIdentifier = R.curry(function(identifier, schema, props) {
return V.validateWithErrorHandler(customErrorHandler(identifier), schema, props);
});
module.exports = validateWithIdentifier;
By simply glueing together a few functions, we now have a customized validation function ready to be re-used in our project.
// user-profile.js
var R = require('ramda');
var validateWithIdentifier = require('./custom-validator');
var validateUserProfile = validateWithIdentifier('userProfile', {
name : R.is(String),
age : R.is(Number)
});
validateUserProfile({name: 'Tyler', age: 'seventy-million'});
// => Error: Could not validate "userProfile" - Illegal value for parameter "age"
Contributing
- Install dependencies
$ npm install
- Run the specs
$ npm test
- Make a fix.
- Submit a PR.