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

fans-warden

v1.1.0

Published

A data warden designed to validate an 'object' based on a ruled blueprint. Can be used for the browser's 'localStorage' or API call requests/responses. Inspired by React's PropTypes.

Downloads

14

Readme

fans-warden

A promise based 'React PropType' inspired JavaScript object validator.

Getting started

npm install --save fans-warden

Usage

Here is a cookie cutter example on how to use fans-warden:

import Warden, { Types } from 'fans-warden';

const rules = {
  prop1: Types.string,
  prop2: Types.bool.isRequired,
  prop3: Types.shapeOf({
    prop4: Types.array,
    prop5: Types.number,
  }),
  prop6: Types.arrayOf(Types.number).isRequired,
  prop7: Types.arrayOf(Types.shapeOf({
    prop8: Types.string.isRequired,
    prop9: Types.number.isRequired,
  })),
  prop10: Types.shape,
  prop11: Types.func,
  prop12: Types.func.isRequired,
};

const data = {...}; // the data you want to validate based on your rules

Warden(rules, data).then((r) => {
  console.log(`The ${r} object is valid :)`);
}).catch((e) => {
  console.log(`The validation failed for this reason: ${e}`);
});

Types

The different Types available for use are the following:

  • Types.string
  • Types.number
  • Types.bool
  • Types.array
  • Types.shape
  • Types.func
  • Types.arrayOf()
  • Types.shapeOf()

By adding .isRequired at the end of the rule (ex: Types.string.isRequired), the validation will now fail if the property is not present in the object to validate.

Types.arrayOf()

Types.arrayOf() takes an argument, which needs to be any possible Type (even Types.arrayOf()). The validation will fail if the array contains an element having a different type than the provided Type.

see example above

Types.shapeOf()

Types.shapeOf() takes an argument, which needs to be an object ({}). This object can implement keys with additional rules that you want to apply, making a deep validation.

see example above

values()

By adding the values(array) method, the data now has to be present inside the array passed to values(), or else the validation fails. You can also combinate values(array) to isRequired.

Here are the different Types that support values(array):

  • Types.string
  • Types.number
  • Types.bool
  • Types.array
  • Types.arrayOf()

example

import Warden, { Types } from 'fans-warden';

const rules = {
  prop1: Types.string.values(['hello', 'hey']).isRequired,
  prop2: Types.number.isRequired.values([15, 20]),
  prop3: Types.array.values([true, 15, 'hey']),
  prop4: Types.arrayOf(Types.string).values(['hey', 'hello']),
  prop5: Types.arrayOf(Types.arrayOf(Types.number).values([15, 16])),
  prop6: Types.arrayOf(Types.shapeOf({
    prop7: Types.number.values([15, 16]),
    prop8: Types.string.values(['hey', 'hello']),
  })),
};

const data = {...}; // the data you want to validate based on your rules

Warden(rules, data).then((r) => {
  console.log(`The ${r} object is valid :)`);
}).catch((e) => {
  console.log(`The validation failed for this reason: ${e}`);
});

min(), max(), range()

By adding a range method on a Types.number such as min(x), max(y) or range(x, y), you define maximum and minimum values for the number. The validation will now fail if the value is smaller or greater than the provided ranges.

You can also combinate the range methods to values(array) and isRequired.

example

import Warden, { Types } from 'fans-warden';

const rules = {
  prop1: Types.number.min(5),
  prop2: Types.number.max(6),
  prop3: Types.number.range(5, 7),
  prop4: Types.number.values([12, 13]).range(10, 15),
  prop5: Types.arrayOf(Types.number.max(4)),
  prop6: Types.arrayOf(Types.number.max(4)).values([2, 3]),
  prop7: Types.arrayOf(Types.arrayOf(Types.number.range(1, 5))),
  prop8: Types.shapeOf({
    prop9: Types.number.max(1, 100).isRequired,
  }),
};

const data = {...}; // the data you want to validate based on your rules

Warden(rules, data).then((r) => {
  console.log(`The ${r} object is valid :)`);
}).catch((e) => {
  console.log(`The validation failed for this reason: ${e}`);
});