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

legitimize

v0.0.3

Published

A simple and flexible object validator.

Downloads

5

Readme

Legitimize

Legitimize is a simple and flexible object validator. It can be useful for validating JSON objects or to simply ensure an options parameter contains valid values.

Install

npm install legitimize

Quick Start

It is relatively simple to use legitimize. Simply require the module, create a scheme and it's returned value will be the validation function. You may also pass a second parameter that is an object of default property rules.

var Legitimize = require("legitimize");

var defaults = {
    type: "string"
};

var validate = new Legitimize({
    name: {
        required: true,
        pattern: /[a-z]+/i
    },
    town: {
        required: true
    },
    age: {
        type: "integer",
        error: "'age' must be an integer that is between 0 and 120",
        use: function(value) { return (value >= 0 && value <= 120); }
    },
    color: {
        within: ["red", "green", "blue"]
    }
}, defaults);

This example object will return an error on many accounts but will return the first error it has encounted. 'town' should be a string, 'age' should be an integer that's between 0 and 120, and lastly color is a string (from the default rules object) but is not either 'red', 'green', or 'blue'.

var invalid = validate({
    name: "Lewis Barnes",
    town: 5,
    age: "four hundred",
    color: "myColour"
});

// returns 'town' must have a data type of string
console.log(invalid);

This object is a revised copy of the previous example with changes made to the town, age, and color properties. Since there are no errors, it returns null.

var invalid = validate({
    name: "Lewis Barnes",
    town: "Manchester",
    age: 20,
    color: "blue"
});

// returns null
console.log(invalid); 

Supported Property Rules

  • required - If true, the property must be present within the object.
  • type - Defines the data type of the property. For information see 'Supported Data Types'.
  • pattern - Tests the value against a regular expression pattern.
  • within - Searches an array of items for the property value.
  • use - Allows custom validation via a function. This function is passed a single parameter that is the value of the property. The returned value of the function will dictate if the value is valid.

Supported Data Types

  • string
  • number
  • object
  • array
  • boolean
  • function
  • regex
  • path
  • decimal
  • integer

Future Improvements

  • Add comments to the code.
  • Make use of a testing framework.
  • Revise README 'Supported Data Types' to demonstrate examples of the data type.

Changelog

Licence

Copyright (c) 2014 Lewis Barnes. See LICENSE for details.