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

jsontypedef

v2.0.1

Published

Syntactic sugar for creating JSON Type Definition (JTD/RFC8927) schemas, and generating compatible JSON Schema from JTD schemas

Downloads

373

Readme

jsontypedef

Syntactic sugar for creating JSON Type Definition (RFC 8927).

All types from Learn JSON Typedef in 5 Minutes are covered, preserving original naming with a few exceptions:

  • enum is created via values(), because enum is a reserved keyword
  • elements is created via array(), for brevity
  • properties is called object(), for brevity
  • discriminator is called match(), for brevity

Install

npm install jsontypedef

Example

Write:

import { string, number, object } from 'jsontypedef'

console.log(object({
  propertyA: string(),
  propertyB: object({
    innerPropertyC: number(),
  })
}))

Get:

{
  properties: {
    propertyA: { type: 'string' },
    propertyB: {
      properties: {
        innerPropertyC: { type: 'float64' }
      }
    }
  }
}

Saves you a good deal of typing maintaining big type definitions.

See all examples in the test suite.

API: Basic Types

  • empty(metadata)
  • boolean(metadata)
  • string(metadata)
  • timestamp(metadata)
  • float64(metadata) (JavaScript numbers)
  • number(metadata) # alias to float64()
  • integer(metadata) # alias to float64()

API: Specialized Numeric Types

For compatibility with other languages and full JTD support:

  • float32(metadata)
  • int8(metadata)
  • uint8(metadata)
  • int16(metadata)
  • uint16(metadata)
  • int32(metadata)
  • uint32(metadata)

API: Advanced Types

  • values(items, metadata) is an alias to create the enum JTD form.
    • enum is a reserved word in JavaScript.
  • array(type, metadata) creates the elements JTD form
  • object(props, optional, additional) creates the properties JTD form
  • match(field, mapping, metadata) creates the discriminator JTD form

API: Nullable Types

A nullable helper is provided for easily creating nullable rules.

const { nullable } = require('jsontypedef')
  • nullable.empty(metadata)
  • nullable.boolean(metadata)
  • nullable.string(metadata)
  • nullable.timestamp(metadata)
  • nullable.float64(metadata) (JavaScript numbers)
  • nullable.number(metadata) # alias to float64()
  • nullable.integer(metadata) # alias to float64()
  • nullable.float32(metadata)
  • nullable.int8(metadata)
  • nullable.uint8(metadata)
  • nullable.int16(metadata)
  • nullable.uint16(metadata)
  • nullable.int32(metadata)
  • nullable.uint32(metadata)
  • nullable.values(items, metadata) is an alias to create the enum JTD form.
    • enum is a reserved word in JavaScript.
  • nullable.array(type, metadata) creates the elements JTD form
  • nullable.object(props, optional, additional) creates the properties JTD form

JSON Schema compatibility

Functionality-wise, JSON Type Definition is a subset of JSON Schema, with one exception: JSON Schema doesn't support tagged unions (the discriminator JTD form). There are no data constraints, so you can't say a string is supposed to have a length of 5, you can only say a string is a string.

Fastify uses ajv for validation and serialization, but only supports JSON Schema out of the box, although its addSchema() method could be reworked to recognize JTD in the future. Although ajv itself already supports JSON Type Definition, it might take a while for the support to come to Fastify.

That is not a problem because we can easily translate JTD schemas to JSON Schema with the jsonschema submodule provided by this library. which offers all methods from the main API but will output JSON Schema compatible schemas. So you can use it with Fastify:

import Fastify from 'fastify'
import { object, string } from 'jsontypedef/jsonschema'

const schema = {
  headers: object({
    'x-foo': string()
  }),
}

fastify.post('/the/url', { schema }, (req, reply) => {
  reply.send('ok')
})

The jsonschema submodule also includes number() and integer() for convenience.

See the full test suite for more usage examples.