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

@telegraph-engineering/node-validator

v0.0.4

Published

A simple package for validating parameters

Downloads

4

Readme

TMG Node Validator

Build Status

build status

Install

    npm install @telegraph-engineering/node-validator --save

Description

TMG node validator is a library for validating that any passed object conforms to the spec passed to it.

Getting started

Once you have installed the pacakge, it can be required, which will return a function that accepts two paramters. The first being the object you wish to test, and the second is the config that the object should be validated against.

Example Checking that a parameter is defined and has type string.

const Validator = require('@telegraph-engineering/node-validator');
const spec = {
    name: {
	    required: true,
	    type: 'string'
	}
};
const param = {} //your object to test;

const errors = Validator(param, spec);
if (errors && errors.length > 0) {
	console.error("Validation failed");
	console.log(errors.join(' & '));
	return;
}

//else do your other logic

Spec options

| Name | Type | Default | Description | |--|--|--|--| | children | {object} | | all specs for children should be passed in the child param | | required | {boolean} | false | wether or not the paramter must be present| | type| {string} | | The data type that the field must be, equivelant of typeof param | requiredIf| {string} | | This field is used to specify that this field is required if the passed key is undefined| | validation| {function}| | A custom validator function, which will be called with the value of the parameter under test, if the function returns anything other than undefined it will be treated as a failure| | validValues| {array}|| An array of possible values for this parameter|

children

If a passed object has children that you wish to validate, you can pass the specs for the children as children parameter

const spec = {
    param1: {
	    children: {
		    param2: {
			    required: true
		    }
	    }
    }
};
Validate({param1: {param2: undefined}}) //fail
Validate({param1: {param2: true}}) //pass

required

This field defines if a field is required

const spec = {
    param1: {
	    required: true
    }
};
Validate({param1: undefined}) //fail
Validate({param1: true}) //pass

type

This field defines what type of fata the field must contain, if it is defined

const spec = {
    param1: {
	    type: 'string'
    }
};
Validate({param1: 1234}) //fail
Validate({param1: 'String'}) //pass

requiredIf

This field allows you to specify that this field is required, if another field is undefined

const spec = {
    param1: {
	    requiredIf: 'param2'
    },
    param2: {
	    type: 'string'
    }
};
Validate({param1: undefined, param2: undefined}) //fail
Validate({param1: undefined, param2: 'String 2'}) //pass
Validate({param1: 1234}) //pass
Validate({param1: 'String', param2: 'String 2'}) //pass

validation

Allows the passing of a custom function to validate values

const spec = {
    param1: {
	    validation: (val) => {
		    if (val.indexOf('invalid') < -1) {
			    return "Should not contain invalid";
		    }
	    }
    }
};
Validate({param1: 'this string is invalid'}) //fail
Validate({param1: 'this string is valid'}) //pass

validValues

Allows the passing of a list of values that the paramter must match one of

const spec = {
    param1: {
	    validValues: ['valid', 27]
    }
};
Validate({param1: 'this string is invalid'}) //fail
Validate({param1: 27}) //pass
Validate({param1: 'valid'}) //pass

Limitations

  • The tool cannot currently validate the values of arrays

The future

  • Support for arrays
  • Support for callback/ promise for custom validation