Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including kontur with all npm packages installed. Try it out:

var kontur = require("kontur")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

kontur v0.2.0

json-schema made simple

kontur

NPM Version Build Status Node.js Version

json-schema made simple

overview

import { compile, bool, int, str } from 'kontur'

compile({
  gender: str,
  age: int,
  nickname: str,
  verified: bool
})
compile({
  gender: str.in('male', 'female').optional,
  age: int.between(0, 200),
  nickname: str.minlen(3).match(/^[a-zA-Z]/),
  verified: bool.optional.default(false)
})

nested schema

compile({
  assignment: {
    assignees: array.len(3).uniq.items(str.len(16)),
    assigner: object.strict.properties({
      id: str.len(16)
    }),
    assigned_at: str.datetime
  }
})

types

object

strict, no extra properties should be included

size(num), maxsize(num), minsize(num), limit the number of properties

properties(schema), specify schema of children

array

strict, no extra items should be included

len(num), minlen(num), maxlen(num), limit the length of the array

items(schema), all element should match

uniq

contains(schema), at least one element should match

string

match(regexp), match a regular expression

email, ipv4, ipv6, uri, datetime, built-in formats

number/int

min(num), max(num), between(num, num),

min(num).exclusive, max(num).exclusive

null

nil just null

boolean

bool

tuple(direived from array)

use plain array

[str, int.between(1,5)]

enum

str.in('created', 'suspended', 'deleted')
any('male', 'female')

miscs

optional used in context of object, by default all keys are required

depends(keys) used in context of object

default(value) add default value

desc(text) add description

title(text) add title

combining schemas

all

all(int.min(0), int.max(1))

any

any(int.min(1).exclusive, int.max(0).exclusive)

one

one(int.min(0), int.max(1))

not

not.nil
not.object.array
RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free