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 🙏

© 2026 – Pkg Stats / Ryan Hefner

js-svu

v0.0.4

Published

A simple object validation utility for javascript

Readme

js-svu

A simple object validation utility for javascript inspired by Fluent Validation and Mocha. Combine with your favorite assertion library (I like Chai).

Warning: this library is still experimental and the API unstable.

var validation = require("js-svu");
var expect = require('chai').expect;

var nameValidator = validation.create({
    firstName: function(it) {
        expect(it).to.be.a('string');
        expect(it).to.not.be.empty;
        expect(it).to.have.length.below(30);
    },
    lastName: function(it) {
        expect(it).to.be.a('string');
        expect(it).to.not.be.empty;
        expect(it).to.have.length.below(40);
    },
});

var name = {
    firstName: 1,
    lastName: "Doe",
};

var result = nameValidator(name);

Installation

Using npm:

npm install js-svu

Note: js-svu currently depends on lodash. And is best used with an asertion library of some sort.

Quick Start

First, define a validator.

var validation = require("js-svu");
var expect = require('chai').expect;

var nameValidator = validation.create({
    firstName: function(it) {
        expect(it).to.be.a('string');
        expect(it).to.not.be.empty;
        expect(it).to.have.length.below(30);
    },
    lastName: function(it) {
        expect(it).to.be.a('string');
        expect(it).to.not.be.empty;
        expect(it).to.have.length.below(40);
    }
});

The above validator ensures that every object it validates has a property named "firstName" that's a non-empty string whose length is less than 30, and a property named "lastName" that's a non-empty string whose length is less than 40. The above example validator makes use of Chai's expect API which throws exceptions (that js-svu handles), but one could also simply return a string (an error message) to indicate failure.

var validation = require("js-svu");

var nameValidator = validation.create({
    firstName: function(it) {
        if(typeof it !== 'string') {
        	return "firstName must be a string.";
    	}

        if(typeof it.length === 0) {
        	return "firstName may not be empty.";
    	}

        if(typeof it.length >= 30) {
        	return "firstName must contain fewer than 30 characters.";
    	}
    },
    lastName: function(it) {
        if(typeof it !== 'string') {
        	return "lastName must be a string.";
    	}

        if(typeof it.length === 0) {
        	return "lastName may not be empty.";
    	}

        if(typeof it.length >= 40) {
        	return "lastName must contain fewer than 40 characters.";
    	}
    }
});

Next, use the validator to validate an object.

var name = {
    firstName: 1,
    lastName: "Doe",
};

var result = nameValidator(name);
var isValid = validation.isValid(result)

Examine the result to determine if the object passed validation, and if not why ( the object above should fail validation since firstName is not a string). For each property in the object being validated a property with the same name is added to the result object, but instead of containing a copy of the value it contains an error message.

result.firstName === "Some error message about the type being wrong.";

The presense of a property in the result object indicates a problem with the associated property in the validated object. If a result object contains no properties, all properties are valid (a valid object).

You can also flatten the result object into a simple collection of error messages.

var errorCollection = validation.flatten(result);

Which will be empty if no of the validations failed. And for more complex objects with properties whose values are objects themselves, validation can be delegated to other validators.

var nameValidator = validation.create({
    firstName: function(it) {
        expect(it).to.be.a('string');
        expect(it).to.not.be.empty;
        expect(it).to.have.length.below(30);
    },
    lastName: function(it) {
        expect(it).to.be.a('string');
        expect(it).to.not.be.empty;
        expect(it).to.have.length.below(40);
    },
});

var studentValidator = validation.create({
    name: function(it) {return nameValidator(it);},
    id: function(it) {
        expect(it).to.be.a('number');
        expect(it).to.be.above(0);
    },
    gpa: function(it) {
        expect(it).to.be.a('number');
        expect(it).to.be.within(0,4);
    },
});

var student = {
    name: {
        firstName: 1,
        lastName: "Doe",
    },
    id: "invalidId",
    gpa: 3.4
};

var result = studentValidator(student);
result.name.firstName === "Some error message about the type being wrong.";
result.id === "Some error message about the type being wrong.";

Philosophy

The basic philosophy behind the utility is that validators should be written in terms closly related to the objects being validated, and that the results should also be reported in terms closely related to those objects. In pratice this means that each property in an object should have an associated property in the validator that contains the function that validates the value, and an associated property in the result (if it fails validation) that contains the error message.

Examples

Not written yet.