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

simple-typechecker

v1.0.1

Published

Check a JavaScript object against a JSON schema, with a single function call

Downloads

3

Readme

simple-typechecker

Usage

const check = require('simple-typechecker');

var UserListSchema = {
  "users": [
    {
      "firstName": "string",
      "lastName": "string",
      "age": "number",
      "hobbies": [ "string" ],
      "favoriteColor": [ "string", null ]
    }
  ]
}

var myUserDataFromServer = {
  users: [
    {
      firstName: "Bob",
      lastName: "Belcher",
      hobbies: [ "cooking burgers", "being a dad", ],
      favoriteColor: "red"
    },
    {
      firstName: "Linda",
      lastName: "Belcher",
      age: 41,
      hobbies: [ "singing", 24, "dancing"]
    },
    {
      firstName: "Tina",
      lastName: "Belcher",
      age: 14,
      hobbies: [ "boys" ]
    }
  ]
}

check(myUserDataFromServer, UserListSchema, 'TheStuff')

Output:

> Assertion failed: TheStuff.users[0].age is not a number: undefined
> Assertion failed: TheStuff.users[1].hobbies[1] is not a string: 24

What does it do?

Takes two things as inputs: a JS object containing data, and a JS object (can be fully represented as JSON) representing a schema. It compares the two, and prints informative errors to the console for every discrepancy.

Who is it for?

In most projects, the server-side language and/or the DB have strict types already. In those cases, you probably don't need this.

This utility is for those of us who deal with a loosely-typed server language (in my case, Common Lisp) and a loosely-structured data source (in my case... also Lisp. yeah.) It will spot data problems the moment they hit the browser, and can save hours of debugging.

How to use it

Each value in the schema, be it the root value (usually an object, but not necessarily!) or a composite value, takes one of the following forms:

  • A type name which can be gleaned from typeof
  • null, which matches both null and undefined
  • An array with a single value in it, which represents an N-length array of values of that type
  • An array with more than one value in it, which represents a single value of one of those types (for example, [ "string", null ] means "a string or nothing"; [ "string", [], null ] means "a string or an array of any type items or nothing").
  • An object, which represents an object. Its keys specify the schemas for their values, but no complaints will be made about values whose names are not included in the schema. Which means, for example, that { } represents "an object with any or no properties".

You may have realized at this point that every value in a schema is itself a valid schema, which makes schemas composable. So the example at the top:

var UserListSchema = {
  "users": [
    {
      "firstName": "string",
      "lastName": "string",
      "age": "number",
      "hobbies": [ "string" ],
      "favoriteColor": [ "string", null ]
    }
  ]
}

could have also been:

var UserSchema = {
  "firstName": "string",
  "lastName": "string",
  "age": "number",
  "hobbies": [ "string" ],
  "favoriteColor": [ "string", null ]
}
var UserListSchema = {
  "users": [ UserSchema ] // an array of objects matching UserSchema
}

Tips

  • The schemas can be fully represented as JSON, which means they could be stored in their own JSON files or even in a Mongo database or be sent over HTTP.
  • Schemas can be arbitrarily deep and complex. There's no reason that
"favoriteColor": [ "string", null ]

couldn't have been

  "favoriteColor": [
    {
      "name": "string",
      "rgb": {
        "r": "number",
        "g": "number",
        "b": "number"
      }
    },
    null
  ]
  • Use these schemas to define a contract between your server- and client-side code, in the absence of a true type system. You can put in writing, "this is what my code works with", and if the server deviates from that, you'll immediately know that that was the reason things broke.