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

json-valid

v0.0.4

Published

A json model validate kit for NodeJS

Downloads

6

Readme

json-validator

This is a simple to use json data model validate kit for NodeJS.

In every NodeJS App, we have to receive data from Clients, which is represented in JSON format. Unlike a Relational Database, JSON doesn't have a schema, and the data from client can be arbitrary, so we have to validate use input on the server.

There are some alternatives to this kind of scenario:

but not as easy as I expected, so here comes this Repo.

Install

npm install json-valid

Usage

var Validator = require('json-valid');

Define a model

var model = {
   username: 'string',
   avatar: 'url',
   age: 'number',
   is_vip: 'bool',
   created_at: 'date'
}

var validator = new Validator(model);
validator.validate(data);	// return a Array of error

More Options

var model = {
  username: 'string',
  slogan: 'string?',		// ? means optional
  age: 'number:[12,18)',	// 12 <= age < 18
  email: 'email',
  phone: 'string:1\\d{10}',	// use your own pattern, Chinese's cell phone number starts with 1, and other 10 digit
  description: 'string?:0,200' // optional, if exists 0 <= description.length <= 200
}

Nested

Validator.define('User', {
  username: 'string',
  avatar: 'url',
  is_vip: 'bool'
});

Validator.define('Comment', {
  comment_id: 'number',
  content: 'string',
  author: 'User'
});

var model = {
  artical_id: 'number',
  content: 'string',
  creator: 'User',
  comments: 'array:Comment'
}

var validator = new Validator(model);
var errors = validator.validate({
  artical_id: 1234,
  content: "This is a article",
  creator: {
    username: 'Ricky',
    avatar: 'http://www.google.com/doodle.png',
    is_vip: 1		// This is a error!
  },
  comments: [{
	comment_id: '2344',		// This is a error!
  	author: {
	  username: 'Hi',
	}
  }]
});

console.log(errors);

And here is the output from above:

[ { [Error: Data field creator.is_vip type error] code: 1000, dataField: 'creator.is_vip', value: 1 },
  { [Error: Data field comments.0.comment_id type error] code: 1000, dataField: 'comments.0.comment_id', value: '2344' },
  { [Error: Data field comments.0.content doesn't exist] code: 1002, dataField: 'comments.0.content' },
  { [Error: Data field comments.0.author.avatar doesn't exist] code: 1002, dataField: 'comments.0.author.avatar' },
  { [Error: Data field comments.0.author.is_vip doesn't exist] code: 1002, dataField: 'comments.0.author.is_vip' } ]

Supported Types

  • string: you can limit the length with 'field': 'string:6,20' (A common password length limit), or provide your own RegExp with 'field': 'string:a\\d+' (Need escape! This is equal to /a\d+/g)
  • number: you can limit the min and max value with 'field': 'number:[10,20)' (A square bracket means include, and parentheses mean exclude, just like what you do in Math)
  • bool
  • url
  • date: Any valid date string is supported
  • email
  • array: you can sepecify the element type of array with 'array-field': 'array:YourType', and define your type with Validator.define('YourType', model), can be nested! If element type not provided, any array will pass the test.

Add your own type

Fork this Repo, and add your type into validators directory.

License

MIT