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

nsp-validator

v1.0.3

Published

Neosperience Resource Validator, validation errors and validatable objects

Downloads

10

Readme

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)