You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

microschema

Package Overview
Dependencies
Maintainers
5
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

microschema

Helper library to create JSON Schemas in a concise way.

1.2.0
Source
npmnpm
Version published
Maintainers
5
Created
Source

Travis npm latest version semantic-release

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'
    }
  }
}

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:

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

Add dependencies:

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

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'
}

Keywords

JSON Schema

FAQs

Package last updated on 05 Jul 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts