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

smpl-schemas

v0.0.3

Published

Allows creation of JSON Schemas in a cleaner format

Downloads

3

Readme

simple-json-schemas

Writing JSON Schemas can be rather intimidating. This package can ease the pain a bit, by making schema creation quicker and the schema files themselves smaller.

Quick example

Suppose you have the following schema:

{
    "additionalProperties": false,
    "required": [
        "a"
    ],
    "properties": {
        "a": {
            "type": "string",
            "default": "apple"
        },
        "b": {
            "type": "number",
            "default": 127
        }
    }
}

Blech. That's pretty verbose. What if it could be written like this instead?

{
    "properties": {
        "a": "apple",
        "b?": 127
    }
}

Much cleaner. You can infer the default value and the type from the property values, so why not use that? And since we're at it, lets say that properties ending with '?' are optional! Hopefully this makes adopting JSON Schemas a bit more bearable.

This package works by applying certain rules that act as "transformations" to a normal JSON schema. They're based on my opinions on what is convenient to write, but if there's interest then I'm open to adding configurability to this package's behavior. (config for a config package. haha. maybe there'll be a need for a simple-json-schemas-schema? eh... I hope not lol)

The rules

  • infer the default value and type of a property by looking at its direct value
    • "a": "apple" becomes "a": { type: "string", default: "apple" }
    • the type of null is null
    • the type of [...] is array
    • all other types are determined with typeof
  • A schema with properties is given a type of object
    • "a": { properties: {...} } becomes "a": { type: 'object', properties: {...} }
  • additionalProperties are assumed to be false unless otherwise specified
  • required is automatically populated with properties that do not end with '?'. Properties that do have the trailing '?' are renamed to remove the '?'
  • if all properties have a default value set, then those defaults are aggregated up to the parent schema object.
    • "a": { properties: { b: 1, c: 2} } becomes "a": { default: { b: 1, c: 2}, properties: {...} }
  • If specified, original values for type, required, and default are preserved

Usage

  • command line:
    • cat pre.schema.json | npx simple-json-schemas > post.schema.json
  • module:
    const laziness = require('simple-json-schemas');
    const fs = require('fs');
    const srcSchema = fs.readFileSync('srcSchema.json', 'utf-8');
    const schema = laziness(srcSchema);
    
    // then you can do something like:
    const Ajv = require('ajv');
    const ajv = new Ajv({ allErrors: true, useDefaults: true });
    const validator = ajv.compile(schema);
    const config = fs.readFileSync('config.json', 'utf-8');
    if (!validator(config)) {
        for (let error of validator.errors) {
            console.error(error);
        }
    }

Notes

  • Don't expect this to work with schemas that define objects with properties that use the same name as any of the reserved schema words (default, properties, type, required)
  • The module both returns the transformed config object, as well as modifying the original