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

microschema

v1.6.0

Published

Helper library to create JSON Schemas in a concise way.

Downloads

5,398

Readme

microschema

Small library without dependencies to create JSON Schemas in a concise way.

Example:

const ms = require('microschema')

ms.strictObj({
  identityId: 'string:required',
  clientId: 'number',
  redirectUri: 'string:uri',
  scope: 'string',
  ipAddress: ms.string({pattern: ''}),
  children: ms.arrayOf(ms.strictObj({
    scope: 'string'
  }))
})

Strings

Using the ms.string() method:

ms.string()
output = {type: 'string'})
ms.string({pattern: '[a-z]+'})

// Passing a javascript RegExp is equivalent to the above
ms.string({pattern: /[a-z]+/})

output = {
  type: 'string',
  pattern: '[a-z]+'
}

Specifying a format:

ms.string({format: 'email'})

output = {
  type: 'string',
  format: 'email'
}

Note: Check which formats are available with your JSON Schema implementation before using this.

Specifying min and max length:

ms.string({minLength: 3, maxLength: 50})

output = {
  type: 'string',
  minLength: 3,
  maxLength: 50
}

Setting the required flag (only possible within an object):

ms.obj({
  foo: ms.required.string()
})

output = {
  type: 'object',
  required: ['foo'],
  properties: {
    foo: {
      type: 'string'
    }
  }
}

Simplified usage within objects:

ms.obj({
  foo: 'string'
})

output = {
  type: 'object',
  properties: {
    foo: {
      type: 'string'
    }
  }
}
ms.obj({
  foo: 'string:required'
})

output = {
  type: 'object',
  required: ['foo'],
  properties: {
    foo: {
      type: 'string'
    }
  }
}

Numbers and Integers

Simplified usage within objects:

ms.obj({
  foo: 'string'
})

Using the ms.number() method:

ms.number()
output = {type: 'number'}
ms.number({min: 0, max: 10})

output = {
  type: 'number',
  minimum: 0,
  maximum: 10
}

Using the ms.integer() method:

ms.integer()
output = {type: 'integer'}

The integer() methods also accepts min and max params the same as number() does.

Booleans

ms.boolean()
output = {type: 'boolean'})

Simplified usage within objects:

ms.obj({
  foo: 'boolean:required'
})

output = {
  type: 'object',
  required: ['foo'],
  properties: {
    foo: {
      type: 'boolean'
    }
  }
}

Null

ms.null()
output = {type: 'null'})

Objects

ms.obj()
output = {type: 'object'}

Don't allow additional properties with strictObj():

ms.strictObj({
  count: ms.integer()
})

output = {
  type: 'object',
  additionalProperties: false,
  properties: {
    count: {type: 'integer'}
  }
}

Add title and description annotations to the schema:

ms.obj({
  displayName: 'string',
}, {title: 'Title', description: 'Desc.'})

output = {
  type: 'object',
  title: 'Title',
  description: 'Desc.',
  properties: {
    displayName: {type: 'string'}
  }
}

Add dependencies:

ms.obj({
  creditCard: 'string',
  address: 'string'
}, {dependencies: {creditCard: 'address'}})

output = {
  type: 'object',
  properties: {
    creditCard: {type: 'string'},
    address: {type: 'string'}
  },
  dependencies: {
    creditCard: ['address']
  }
}

Set a default value in case the property is absent:

ms.obj({
  creditCard: 'string',
  address: 'string'
}, {default: {}})

output = {
  type: 'object',
  default: {},
  properties: {
    count: {type: 'integer'}
  }
}

Arrays

ms.arrayOf(ms.string())

output = {
  type: 'array',
  items: {type: 'string'}
}

You can use these additional modifiers:

ms.arrayOf(ms.string(), {minItems: 1, maxItems: 3, uniqueItems: true})

output = {
  type: 'array',
  items: {type: 'string'},
  minItems: 1,
  maxItems: 3,
  uniqueItems: true
}

Enumerations

// All values in an enumeration must be of the same type.
ms.enum('foo', 'bar')

output = {
  type: 'string',
  enum: ['foo', 'bar']
}

Constant Value

ms.const('foo')

// The output is the same as ms.enum('foo') as there is no equivalent
// to value in JSON schema.
output = {
  type: 'string',
  const: 'foo'
}

Combining Types

ms.types('string', 'number')
output = {
  type: ['string', 'number']
}

ms.types(ms.string({format: 'uri'}), ms.number({min: 0}))
output = {
  type: ['string', 'number'],
  format: 'uri',
  minimum: 0
}

anyOf / oneOf / allOf

ms.anyOf('number', ms.obj({foo: 'string'}))

output = {
  anyOf: [
    {type: 'number'},
    {
      type: 'object',
      properties: {
        foo: {type: 'string'}
      }
    }
  ]
}

Note: you can also pass an array as the first argument

$id / $ref

ms.$id('#user').obj({
  name: 'string',
  friend: ms.$ref('#user')
})

output = {
  $id: '#user',
  type: 'object',
  properties: {
    name: {type: 'string'}
    friend: {$ref: '#user'}
  }
}

definitions

ms.definitions({
  user: ms.obj({name: 'string'})
}).obj({
  name: 'string',
  friend: ms.$ref('#/definitions/user')
})

output = {
  definitions: {
    user: {
      type: 'object',
      properties: {
        name: {type: 'string'}
      }
    }
  }
  type: 'object',
  properties: {
    name: {type: 'string'}
    friend: {$ref: '#/definitions/user'}
  }
}