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

stefie

v2.0.2

Published

Superlight, schema-based, extensible Node.js object validator. Useful for Express req.body/params/query, or MongoDB document before inserting into collection if don't want to use Mongoose.

Downloads

25

Readme

Stefie

Superlight, schema-based, extensible Node.js object validator. Useful for Express req.body/params/query, or MongoDB document before inserting into collection if don't want to use Mongoose.


What's New in 2.0.2

  • Added numberString type to check for number strings

What's New in 2.0.1

  • Added jsonString type to check for JSON strings

What's New in 2.0

  • Extensiblity - Add your own custom validators with the add() method
  • Cleaner namespace management - put all your rules in the _rules object for each property in the schema
  • Code rewritten to be simpler and more elegant
  • Added dateString type to check for valid date strings
  • Renamed ObjectID type to objectId to be consistent with other type names
  • Renamed hexString type to objectIdString to be more intuitive

Installation

$ npm install stefie

Usage

Firstly, model a schema of the object you want to validate. Then, call stefie and see if she returns an error or null.

var stefie = require('stefie');

var movie = {
	title: '300',
	crew: {
		director: 'Zack Snyder',
		writers: ['Frank Miller', 'Zack Snyder']
	}
};

var movieSchema = {
	title: { _rules: { type: 'string' } },
	crew: {
		director: { _rules: { type: 'string' } },
		writers: { _rules: { type: 'array', arrayType: 'string' } }
	}
};

var error = stefie(movie, movieSchema);

if (error != null) {
	console.log(error);
}

The _rules Object

Add all the rules that apply to a property in the _rules object in the schema.

var schema = {
	title: { _rules: { type: 'string' } },
	rating: { _rules: { type: 'number', required: true, null: false, min: 1, max: 10 } }
};

The Return Object

If there are no errors, stefie() returns null.

If there are errors, she returns an object with each property that failed validation and the reason why.

var movie2 = {
	title: 300,
	crew: {
		director: 'Zack Snyder',
		writers: 'Frank Miller, Zack Snyder'
	}
};

var error = stefie(movie2, movieSchema);

if (error != null) {
	console.log(error);
}

Output:

{
	title: 'Invalid type',
	crew: {
		writers: 'Invalid type'
	}
}

Rules

The following are keys you can use in the _rules object in your schema.

Rule precedence: If you have required and/or null rules, required will be evaluated first, then null, then any other rule.

Rule skip: Rules will not be evaluated if the property value is undefined. The exception is the required rule.

required

Data type: boolean

If true, property must be defined. If false, property can be undefined.

var schema = {
	title: { _rules: { required: true } }
};

Note: Setting this rule to false is logically the same as not using it at all.

null

Data type: boolean

If true, property can be null. If false, property cannot be null.

var schema = {
	rating: { _rules: { null: true } }
};

Note: required and null rules can coexist together.

type

Data type: string

Checks if property is of the specified type.

Possible values | Notes --- | --- array |   boolean |   date | Checks if property is a JavaScript Date object dateString | Checks if property can be parsed with Date.parse() jsonString | Checks if property can be parsed with JSON.parse() number |   numberString |   object |   objectId | Checks if property is a MongoDB ObjectID object objectIdString | Checks if property is a valid MongoDB ObjectID string string |  

var schema = {
	rating: { _rules: { type: 'number' } }
};

arrayType

Data type: string

Checks if an array's elements are of the specified type.

See type for possible values.

var schema = {
	writers: { _rules: { type: 'array', arrayType: 'string' } }
};

min

Data type: number

Checks if property is equal to or greater than the specified number.

var schema = {
	rating: { _rules: { type: 'number', min: 1 } }
};

max

Data type: number

Checks if property is equal to or less than the specified number.

var schema = {
	rating: { _rules: { type: 'number', max: 10 } }
};

minLength

Data type: number

Checks if array/string has a length equal to or greater than the specified number.

var schema = {
	name: { _rules: { type: 'string', minLength: 1 } },
	writers: { _rules: { type: 'array', arrayType: 'string', minLength: 1 } }
};

maxLength

Data type: number

Checks if array/string has a length equal to or less than the specified number.

var schema = {
	name: { _rules: { type: 'string', maxLength: 100 } },
	writers: { _rules: { type: 'array', arrayType: 'string', maxLength: 10 } }
};

enum

Data type: array

If property is a single-value, checks if it is one of the enumerated values. If property is an array, checks if each of its elements is one of the enumerated values.

var schema = {
	director: { _rules: { type: 'string', enum: ['Zack Snyder', 'JJ Abrams'] } },
	cast: { _rules: { type: 'array', enum: ['Gerald Butler', 'Lena Headey', 'Michael Fassbender'] } }
};

regex

Data type: regular expression

Checks if property matches the specified regular expression.

var schema = {
	email: { _rules: { regex: /.+@.+/ } }
};

Custom Validators

You can add your own custom validator functions to stefie, and use it in the _rules object in your schema.

stefie.add(rule, fn)

Arguments:

  1. rule (string): Name of the rule
  2. fn (function): Validator function

The validator function must have a signature of function(val, ruleVal) where val is the value of the property of the object we are validating and ruleVal is the value of the rule.

var fn = function(val, ruleVal) {
	return (val !== ruleVal ? null : 'Value is disallowed');
};

stefie.add('disallow', fn);

var person = {
	name: 'Stranger'
};

var error = stefie(person,  {
	name: { _rules: { type: 'string', disallow: 'Stranger' } }
});

console.log(error);

Output:

{
	name: 'Value is disallowed'
}

stefie.remove(rule)

Removes a validator. Cannot remove required and null.

Arguments:

  1. rule (string): Name of the rule
stefie.remove('disallow');

Test

To run the test cases, cd into the stefie directory then do:

npm test

Author: Harry Lee