nsp-validator

1.0.3 • Public • Published

Neosperience Cloud Objects Validators

Build Status dependencies Codecov

NPM

Neosperience Resource Validator, validation errors and validatable objects. This library provides a base to define custom validators.

Why Neosperience validator?

Neosperience Validator is a set of classes, that can be used to implement custom validators within a project. The main issue this project addresses is related to decoupling validation process from validators. The validation process is achieved applicating a set of validating functions and storing the resulting errors in a stack. The content of a validating function is context-dependent, but the overall process can be abstracted into an utility class. Neosperience Validator addresses exactly this task, providing an helper class, Validator.Validatable that implements a validate() method in a fashion similar to Active Records frameworks such as Rails or Grails.

Heads up: note that NSP Validator does not implement any of the Active Records pattern, but since in these patterns there is a common way of storing validation results and validators within the main class, we choose to adopt the same convention.

Getting started

1. Add library

Include npm module in your project

npm install --save nsp-validator

2. Implement Validator

Extend Validator and implement validatingFunction() to provide a function that check specific logic and if it fails add an Error to the stack, using parent addValidationError(<string|Error>)

// var _ = require("lodash");
// var NSPValidator = require("nsp-validator");
 
var MyValidator = function(){
    _.extend(this, new NSPValidator.Validator());
    this.validationFunction = function(){
        // do something, then call this.addValidationError("error message");    
    }
}

3. Implement Validatable

Extend Validatable in your business object add Validator as validator

// var _ = require("lodash");
// var NSPValidator = require("nsp-validator");
 
var MyBusinessObject = function(someParams){
    _.extend(this, new NSPValidator.Validatable());
    this.addValidator("myValidatorName", MyValidator);
}

4. Validate

Create and use your business object. Sometimes later call validate()

var instance = new MyBusinessObject();
// do something
instance.validate();

API structure

Validatable

Is an abstract class that must be extended by an object that wants to benefit from validation. It presents a set of methods useful for validating child object. The two main methods are validate() and addValidator(validatorName,ValidatorObject) that respectively add a new validator to the common set and run every active validator function.

Methods

.addValidators()

Adds a set of validators to this validatable object. if a validator with the same key exists it is replaced by the new one

Parameter Required Description
validatorsMap Yes Map containing a set of validators functions
.addValidator()

Adds a validator with name key to the set of validators. Same key means validator is replaced.

Parameter Required Description
validatorName Yes Name to be used as key to this validator.
Validator Yes Function to be used as validator.
.validate()

Executes validators to fill errors stack and returns true/false whether any of them has failed or not.

Parameter Required Description
validatorsSubsetMap No Map containing a subset of validators functions to be used instead of current validators.
.removeAllValidators()

Removes every validator associated to this object.

Parameter Required Description
none
.removeValidator()

Removes a specific validator from the set of available validators.

Parameter Required Description
validatorName Yes Name of the validator to be removed.
.clearValidationErrors()

Clears every error that was generated by last validate() call and resets it to the initial state.

Parameter Required Description
none
.errorStack()

Retrieves a string representation of the list of any error that was produced by validate() call.

Parameter Required Description
none

Validator

(TBD)

ValidationError

(TBD)

Common Usage patterns

(TBD)

Future Improvements

(TBD)

Package Sidebar

Install

npm i nsp-validator

Weekly Downloads

0

Version

1.0.3

License

Apache-2.0

Last publish

Collaborators

  • aletheia1982