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

tiny-schema

v1.0.0

Published

A minimalistic JSON validator

Downloads

5

Readme

tiny-schema Build Status

It's like typeof, but it's a function.

const is = require('tiny-schema');

is('number')(5); // => true
is('number')('hello'); // => false
is('string')('hello'); // => true
is('boolean')(false); // => true

But it understands the difference between object and null.

is('object')({}); // => true
is('object')(null); // => false
is('null')(null); // => true
is('null')({}); // => false

And it understands the difference between object and array.

is('array')([]); // => true
is('array')({}); // => false
is('object')([]); // => false

Inspired by JSON Schema, it can recognize integers.

is('integer')(5); // => true
is('integer')(5.5); // => false

And since it knows integers, why not go further?

// Positive
is('+integer')(-5); // => false
is('+integer')(5); // => true
is('+integer')(0); // => true

// Negative
is('-number')(-5.5); // => true
is('-number')(5.5); // => false
is('-number')(0); // => true

And sometimes you might want to exclude those pesky zeros...

// Positive
is('++integer')(5); // => true
is('++integer')(0); // => false

// Negative
is('--integer')(-5); // => true
is('--integer')(0); // => false

Validating deep structures could be useful...

is('[integer]')([-5, 0, 5]); // => true
is('{string}')({ a: 'hello', b: 'world' }); // => true

Nullable types? Might need those too.

is('[string?]?')(['hello']); // => true
is('[string?]?')([null]); // => true
is('[string?]?')(null); // => true

Algebraic data types are so in these days.

is('number|string')('hello'); // => true
is('number|string')(-123); // => true

Nested algebraic data types within a nullable object? Oh I can validate that in one line of code.

is('{integer|boolean}?')({ a: -123, b: true }); // => true
is('{integer|boolean}?')(null); // => true

What if I need to get specific about which strings I accept?

is('/^\\w+$/i')('not a word'); // => false
is('/^\\w+$/i')('word'); // => true

What if I need to get specific about which numbers I accept?

is('100-200')(150); // => true
is('100-200')(200); // => true
is('100-200')(201); // => false
is('100-200')(150.5); // => false (no fractions in an integer range)
is('100.0-200.0')(150.5); // => true (now it accepts fractions)

Enums are nice. Let's use those.

is('"hello"|"world"|500|false')('hello'); // => true
is('"hello"|"world"|500|false')('world'); // => true
is('"hello"|"world"|500|false')(500); // => true
is('"hello"|"world"|500|false')(false); // => true

Can every single feature mentioned thus far be nested and used in every context? Yes? Oh, great.

is('true|{boolean|"hello"|[/^\w+$/i?]}|500.2-600.8|[[--integer]?]');

Jesus, okay I'll stop now.

Escaping string literals

String literals can be created by using "", '', or ``, and each form is practically equivalent. The only difference is that each form's respective quote character must be escaped when referred to literally. Escaping is done by using two quotes in direct succession.

is('"The cow said ""moo""."')('The cow said "moo".'); // => true
is('`The cow said "moo".`')('The cow said "moo".'); // => true

Regular expressions

Regular expressions are passed directly to the RegExp constructor. To include a literal / character in a regular expression, use two forward slashes in direct succession.

is('/^application//json$/i')('application/json'); // => true

Exclusionary ranges

When validating a range of numbers, sometimes it's useful to exclude one or both boundaries. This can be done by using the < or > character on the boundary that should be excluded.

is('0.0->1.0')(0); // => true
is('0.0->1.0')(0.9999); // => true
is('0.0->1.0')(1); // => false

is('0.0<-1.0')(0); // => false
is('0.0<-1.0')(0.9999); // => true
is('0.0<-1.0')(1); // => true

is('0.0<->1.0')(0); // => false
is('0.0<->1.0')(0.9999); // => true
is('0.0<->1.0')(1); // => false

The < and > characters don't correspond to "less than" and "greater than". Rather, they depict an arrow, and should be read as "approaches".

Accepting anything

The any type can be used to accept any value.

is('any')(5); // => true
is('any')({}); // => true
is('any')(null); // => true

Reusing validators

The functions spawned by tiny-schema can be reused without needing to parse the schema string more than once.

const isValid = is('{string}');

isValid({ hello: 'world' }); // => true
isValid({ hello: 5 }); // => false

Limitations

The purpose of tiny-schema is to provide powerful JSON validation with minimal code use. Because of its terse syntax, typical validations can be done in less than one line of code. However, tiny-schema is not a replacement for JSON Schema.

Unlike JSON Schema, tiny-schema cannot...

  • validate each of an object's properties by name
  • validate that an object has certain property names
  • validate that an object doesn't have certain property names
  • validate that an object or array has a minimum or maximum number of properties
  • use JSON Pointers to implement dependent types

tiny-schema should be seen as an easy-to-use JSON type checker on steroids—not an all-purpose, fully-featured JSON typing system.

Installation

npm install --save tiny-schema