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

nsure

v0.2.27

Published

nsure is a JS class that allows deep testing/sanitation, modification chains and model generation of objects.

Downloads

4

Readme

build status

nsure

[nsure] is a nodejs class that allows deep testing, modification chains and model generation of objects

The goal here was to have a simple, yet highly flexible cheking system for variables

This project is still in very early development, hence anything is subject to change!

  • installing
    npm install nsure
  • a simple example

this is a series of checks and modifications to make sure (nsure ;) ) that a string is a valid email

		var nsure = require('nsure');
    var nsureRules_user_email = {
			defaultTo: function() {
				return '[email protected]';
			},
			checks: [ 'type', 'stringMaxLength', 'replace_trim', 'email' ],
			type: {
				expected: 'string',
				onFail: [ 'returnError' ],
				error: {
					code: 'email.format',
					msg: '[email] needs to be valid, >6 chars and <80 chars.'
				}
			},
			stringMaxLength: {
				max: 80,
				onFail: [ 'returnError' ],
				error: {
					code: 'email.format',
					msg: '[email] needs to be valid, >6 chars and <80 chars.'
				}
			},
			replace_trim: {
				query: /^[\s]+|[\s]+$/g,
				replacement: ''
			},
			email: {
				onFail: [ 'returnError' ],
				error: {
					code: 'email.format',
					msg: '[email] needs to be valid, >6 chars and <80 chars.'
				}
			}
		};
		var emailCheck = new nsure(nsureRules_user_email);
		emailCheck('bla @blub');
  • helpers for shortening your checks
	var helpDemoRuleSet = {
		'attributesToCheck': {
			'gender': Nsure.helpers.inListNsure(['m', 'f']),
			'height': Nsure.helpers.numberNsure(100, 290, 180),
			'tld': Nsure.helpers.stringNsure(4, '.com'),
			'url': Nsure.helpers.urlNsure(400, 'http://google.com'),
			'email': Nsure.helpers.emailNsure(),
			'protocols': Nsure.helpers.arrayOfNsure(['string', ['http', 'ftp', 'https' ], ['http'])
		},
		onUnruledAttributes: [ 'deleteAttribute' ],
		onError: 'returnErrorMsg'
	};
	var testObject = {
	};
	var testEnsure = new Nsure(helpDemoRuleSet);
	console.log(testObject);
	console.log('single attribute check ---');
	var result = testEnsure.check(testObject, [ 'gender' ]);
	console.log(result);
	console.log('full attribute check ---');
	var result = testEnsure.check(testObject);
	console.log(result);
	console.log('---MODEL---');
	console.log(testEnsure.model);
	console.log('---=====---'); 
		
  • getting a model from a more complex check

Here a complex object is defined, then a ruleset is defined and a nsure generated for it. the object is passed through and the result is modified appropriately. Also, whenever a nsure is instanced using a ruleset, it will generate a model based on that set of rules, which is available via .model

		var rules = {
			attributesToCheck: {
				level: {
						checks: ['type', 'numberRange'],
						type: {
							expected: 'number',
							onFail: [ 'toInt' ],
							fallback: function(input, options, fullInput, fullRules) { console.log('Error detected in "level"! "level" must be a number!'); return fullInput.level; }
						},
						numberRange: {
							min: 1,
							max: 13,
							onFail: [ 'toBorder' ]
						}
					},
				a: {
					checks: [ 'type', 'numberRange' ],
					type: {
						expected: 'number',
						onFail: [ 'toInt' ],
						fallback: 0
					},
					numberRange: { 
						min: 2,
						max: 1000,
						onFail: [ 'toBorder' ]
					}
				},
				b: {
					checks: [ 'type', 'stringMaxLength' ],
					type: {
						expected: 'string',
						onFail: [ 'toFallback' ],
						fallback: ''
					},
					stringMaxLength: {
						max: 8,
						onFail: [ 'cut' ]
					}
				},
				d: {
					checks: [ 'mongoId' ],
					mongoId: {
						onFail: [ 'toFallback' ],
						fallback: new ObjectID()
					}
				},
				f: {
					checks: [ 'regEx' ],
					regEx: {
						method: 'test',
						//method: 'execute',
						//executeReturn: 0,
						expression: '^[0-9a-fA-F]{24}$',
						flags: 'gi',
						onFail: [ 'returnError' ],
						error: {
							code: 'test.f',
							msg: '[f] should be a String of 24 chars.'
						}
					}
				},
				timestamp: {
					checks: [ 'type' ],
					type: {
						expected: 'number',
						onFail: [ 'toInt' ],
						toInt: {
							onFail: [ 'returnError' ],
							error: {
								code: 'timestamp',
								msg: 'timestamp must be a number.'
							}
						},
						fallback: function(val) {
							var error = new aError('timestamp', 'timestamp must be a number.', val);
							return error;
						}
					}
				}, 
				o: {
					checks: [ 'type', 'subNsure' ],
					type: {
						expected: 'object',
						onFail: [ 'returnError' ],
						error: {
							code: 'noObject',
							msg: 'o must be a object.'
						}
					},
					subNsure: {
						attributesToCheck: {
							o_a: {
								checks: [ 'type' ],
								type: {
									expected: 'number',
									onFail: [ 'returnError' ],
									error: {
										code: 'noNumber',
										msg: 'o.o_a must be a number.'
									}
								}
							},
							o_c: {
								checks: [ 'type', 'subNsure' ],
								type: {
									expected: 'object',
									onFail: [ 'returnError' ],
									error: {
										code: 'noObject',
										msg: 'o.o_c must be a object.'
									}
								},
								subNsure: {
									attributesToCheck: {
										o_c_1: {
											checks: [ 'type' ],
											type: {
												expected: 'number',
												onFail: [ 'returnError' ],
												error: {
													code: 'noNumber',
													msg: 'o.o_c.o_c_1 must be a number.'
												}
											}
										}
									}
								}
							}
						},
						onUnruledAttributes: [ 'deleteAttribute' ],
						onError: 'returnErrorMsg'
					}
				}
			},
			onUnruledAttributes: [ 'deleteAttribute' ],
			onError: 'returnErrorMsg'
		};
		
		var testEnsure = new nsureField(rules);
		console.log(testObject);
		var result = testEnsure.check(testObject);
		console.log(result);
		console.log('---MODEL---');
		console.log(testEnsure.model);
		console.log('---=====---'); */
		
- 
		var a = 1;
		
- get everything
		var a = 1;
		

VERSION

v 0.2.27

author

Toni Wagner

Licence

free