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

json-schema-shorthand

v3.1.0

Published

Shortcuts for json schema definitions

Downloads

1,378

Readme

json-schema-shorthand

JSON Schema is a useful beast, but its schema definition can be a little bit more long-winded than necessary. This module allows to use a few shortcuts that will be expanded into their canonical form.

To install

npm install json-schema-shorthand

Typical use

import * as j from 'json-schema-shorthand';

let schema = j.object({
    foo: 'number',
    bar: array('string'),
});

// schema === {
//    type: 'object',
//    properties: {
//        foo: { type: 'number' },
//        bar: { type: 'array', items: { type: 'string' } }
//    }
// }

Compatibility with json-schema-to-ts

json-schema-shorthand can be used in conjecture with json-schema-to-ts. Just remember to as const your schemas to get the most precise types out of FromSchema<>.

const res = j.shorthand({
    object: {
        foo: "number!",
    },
    } as const
);

expectTypeOf(s).toMatchTypeOf<{ foo: number; }>();

Functions

j.shorthand( schema )

Takes in a data structure and expands any shorthand (see next section) found in it. Note that because json-schema-shorthand is using @yanick/updeep-remeda internally, the returned schema is frozen.

Also the default export of json-schema-shorthand.

let schema = j.shorthand( { object: { foo: 'number' } });
// => { type: 'object', properties: { foo: { type: 'number' } } }

j.number( description?, schema? )

let schema = j.number( 'number of thingies', { maximum: 5 });
// => { type: 'number', description: 'number of thingies', maximum: 5 }

Expands into a number type.

j.integer( description?, schema? )

let schema = j.integer({ maximum: 5 });
// => { type: 'integer', maximum: 5 }

Expands into an integer type.

j.string( description?, schema? )

let schema = j.string({ maxLength: 5 });
// => { type: 'string', maxLength: 5 }

Expands into a string type.

j.array( itemsSchema, schema? )

let schema = j.array('number', { maxItems: 5 });
// => { type: 'array', items: { type: 'number' }, maxItems: 5 }

Expands into an array type.

j.object( description?, properties, schema? )

let schema = j.object({ foo: 'string!' }, { description: "yadah" });
// => { type: 'object',
//      properties: { foo: { type: 'string' } },
//      required: [ 'foo' ],
//      description: "yadah" }

Expands into an object type.

j.allOf(schemas,extra), j.oneOf(schemas,extra), j.anyOf(schemas, extra)

let schema = j.allOf(j.array(), { items: 'number' });
// => { allOf: [
//      { type: 'array' },
//      { items: { type: number } }
//    ] }

Same for oneOf and anyOf.

j.not(description?, schema)

let schema = j.not(array());
// => { not: { type: 'array' } }

Shorthands

Types as string

If a string type is encountered where a property definition is expected, the string is expanded to the object { "type": type }.

{
    "foo": "number",
    "bar": "string"
}

expands to

{
    "foo": { "type": "number" },
    "bar": { "type": "string" }
}

If the string begins with a #, the type is assumed to be a local reference and #type is expanded to { "$ref": type }.

{ "foo": "#/definitions/bar" }

becomes

{ "foo": { "$ref": "#/definitions/bar" } }

If the string begins with a $, the type is assumed to be a general reference and $type is expanded to { "$ref": type }.

{ "foo": "$http://foo.com/bar" }

becomes

{ "foo": { "$ref": "http://foo.com/bar" } }

object property

{ object: properties } expands to { type: "object", properties }.

shorthand                              expanded
------------------------               ---------------------------
foo: {                                  foo: {
    object: {                               type: "object",
        bar: { }                            properties: {
    }                                           bar: { }
}                                           }
                                        }

array property

{ array: items } expands to { type: "array", items }.

shorthand                              expanded
------------------------               ---------------------------
foo: {                                  foo: {
    array: 'number'                         type: "array",
}                                           items: {
                                                type: 'number'
                                            }
                                        }

required property

If the required attribute is set to true for a property, it is bubbled up to the required attribute of its parent object.

shorthand                              expanded
------------------------               ---------------------------

foo: {                                  foo: {
    properties: {                           required: [ 'bar' ],
      bar: { required: true },              properties: {
      baz: { }                                bar: {},
    }                                         baz: {}
}                                       }

The type or $ref of a field can also be appended a ! to mark it as required.

shorthand                              expanded
------------------------               ---------------------------

foo: {                                  foo: {
    properties: {                           required: [ 'bar', 'baz' ],
      bar: 'number!'                        properties: {
      baz: '#baz_type!'                       bar: {   type: 'number' },
    }                                         baz: { '$ref': '#baz_type' }
}                                       }