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

strongjson

v0.0.6

Published

Library for creating documented, strongly-typed, & validated JSON.

Downloads

70

Readme

StrongJSON

Library for creating strongly-typed, validated, & documentable JSON.

npm install --save strongjson

Why StrongJSON

Keeping track of JSON object properties, especially when they are nested and complex, can be hard. This library helps you define a JSON schema that gives you validation, options, and documentation of large complex JSON objects or JS objects.

Getting Started:


var StrongJSON = require('strongjson');
var Types = require('strongjson').Types;

var HouseType = StrongJSON.create(Types.Object(
  Types.Key("address", Types.String(), { description: "full address of house." }),
  Types.Key("rooms", Types.Array({ description: "All the rooms in the house." },
    Types.Object(
      Types.Key("name", Types.String({ validate: { options: ["bedroom", "bathroom", "kitchen"] }})),
      Types.Key("area", Types.Number(), { description: "Area in square feet." })
    )
  ))
))

const {valid, errors} = HouseType.validate({
  "address": "123, South Road", // full address of house.
    "rooms": [                    // All the rooms in the house.
      {
        "name": "bedroom",        // Can be "bedroom", "bathroom", or "kitchen".
        "area": 300               // Area in square feet
      }
    ]
})

console.log(valid)  // => True
console.log(errors) // => []

// Trigger an error:
house.rooms[0].name = "Not a room option";

const {valid, errors} = HouseType.validate(house)

console.log(valid) // => false
console.log(errors)  // =>
// errors: [
//   {
//     errorKey: 'INVALID_OPTION',
//     message: 'Invalid value 'unknown room\'. Expected one of the following: \'bedroom\', \'bathroom\', \'kitchen\'',
//     schemaId: '[Object][Key rooms][Array][option 0][Object][Key name][string]'
//   }
// ]

Types

var Types = require('strongjson').Types

Types.Object();
Types.Array();
Types.Key();
Types.String();
Types.Number();
Types.Null();
Types.Boolean();

Type arguments can be in any order. For example, these are all the same:

Types.Key("myKey", {description: "My wonderful key"}, Types.String);
Types.Key(Types.String, "myKey", {description: "My wonderful key"});
Types.Key({description: "My wonderful key"}, Types.String, "myKey");

Types.Object() and Types.Array() can accept arrays of Types as type "options":

Types.Key("myKey", [Types.String(), Types.Number()]);
Types.Array([Types.String(), Types.Number()]);

Requiring Existence of Object

You can require the existence of Object.Key.

var HouseType = StrongJSON.create(Types.Object(
  Types.Key("address", Types.String(), { required: true }),
))

HouseType.validate({}).valid // false

Object.Key required is done "shallow", meaning if parent keys aren't required, then the validation will pass. For example:

const MyType = StrongJSON.create(
  Types.Object(
    Types.Key("outerKey",           // add {required: true} here if you want this to validate existence of the innerKey
      Types.Object(
        Types.Key("innerKey", {required: true}, Types.String())
      )
    )
  )
);

HouseType.validate({}).valid // true

Custom Validation

In upcoming versions, custom validation will be allowed via passing curry'd functions into the options `validate' key:


const minLength = (minLength) => (array) => array.length > minLength;

const MyType = StrongJSON.create(
  Types.Array({ validate: [ minLength(3) ] }, Types.Number())
);

HouseType.validate([1, 2]).valid   // false because it must be 3

Documenting & Generating Examples:

In upcoming versions, you'll be able to set example values and generate example json.

HouseType.getExample();

Serialization:

const HouseTypeJson = HouseType.serialize();   // Serializes to a string for storage in a data store.
const NewHouseType = StrongJSON.deserialize(HouseTypeJson)