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

jsontype

v2.1.0

Published

A small library that takes JSON structure descriptions and creates a function that validates if a javascript value is in exactly this structure.

Readme

Overview

JSONtype is a markup language that describes JSON data layout and allows you to validate random JavaScript values. This prevents you from writing lots of try-catch blocks or to get undefined results when processing malformed user input.

The JSONtype library is a schema validator library for JavaScript (mainly Node.js). It allows you to define your data structure in a compact way and create a JavaScript function from it. The validation is then just a function call.

Syntax

* - accepts everything but undefined values string - only accepts string mail - only accepts strings that are formed like an email "abc" - only accepts string with exactly this content number - only accepts numbers boolean - only accepts true or false function - only accepts a function undefined - only accepts undefined (a object entry must not be defined)

[*] - accepts any array [type] - accepts only arrays of that specified subtype

{name: type, name: type} - accepts only objects that have these properties with exactly these types {name: type, * : undefined} - accepts only objects that have no other properties than the named ones {*: type} - accepts only objects where all properties have the specified subtype {name?: type} - accepts only objects that dont have this named property or the property has the specified type {"name": type} - accepts only objects which have this named property with exactly this type (be careful, the parser is not perfect)

Caution: Do not accept validation schemas from the user because they could harm you.

Installing

npm:

npm install jsontype

You can use jsontype for JavaScript via var makeValidator = require('jsontype').makeValidator or use it for PHP projects by calling node node_modules/jsontype/jsontype2php ... with your validators

Using jsontype in JavaScript and Node.js

	var jsontype = require('jsontype');
	var validator = jsontype.makeValidator('{username: string, password: string}');
	if(validator({username: 'peter', password: '123'})) {
		// do something
	}

You can also define your own types as regexp or function like

	var jsontype = require('jsontype');
	function validatePassword(value) {
		// Password criteria
		return typeof(value) === 'string' && value.length >= 8;
	}
	// we define two additional validators
	// identifier forces strings via regexp to begin with a alphanumerical character
	// pwstring forces strings via function to be at least 8 characters strong
	var validator = jsontype.makeValidator('{username: identifier, password: pwstring}', {
		identifier: /^[a-zA-Z_][a-zA-Z_0-9]*$/,
		pwstring: validatePassword
	});
	if(!validator({username: 'peter', password: '123'})) {
		console.log('Check if your password fits the criteria');
	}

This especially makes sense if you want to compose validators to more complex situations.

makeValidator creates a Function which you can call with one parameter - the value to validate. The function returns true if the value validates successfully. It is good practise to create all validators at startup and then just call the generated functions. If you want to have an exception thrown instead of a boolean return value, use makeThrowValidator. It will create functions that exit with undefined or throw an exception.

Using jsontype in PHP

To use jsontype in PHP, e.g. to build JSON APIs, use jsontype2php.js

Usage:

  • node jsontype2php.js '{username: string, password: string}' for creating a function validate($x)
  • node jsontype2php.js '$foo' '[mail]' for creating a inline PHP statement that checks if $foo is a list of mail addresses

Some remarks:

  • you might need single quotes on command line for escaping the string and double quotes for string literals inside the schema
  • using custom types like validate_my_regex lead to calls to a function with the same name returning a boolean

Once you created a PHP snippet, copy+paste it into your code. This guarantees high performance and effective JSON validation.

More Examples

Look into runtests.js which provides a lot of test cases that cover all features of jsontype.