GithubHelp home page GithubHelp logo

json-from-schema's Introduction

json-from-schema

json-from-schema generates random JSON based on JSON Schema draft v4 schemas.

Usage

var jfs = require('json-from-schema');
var schema1 = {
  id: 'http://www.example.com/herp#'
  , type: "object"
  , properties: {
    someString: {type: 'string', pattern: 'bl(a){1,10}h'}
    , someInt: {type: 'integer', minimum: 23, maximum: 42}
    , someEnum: {$ref: '#/definitions/blaEnum'}
    , someEnumArray: {type: 'array', items: {$ref: '#/definitions/blaEnum'}, minItems: 5, maxItems: 8}
    , someObject: {
      type: 'object'
      , properties: {
        derp: {type: 'string', minLength:1, maxLength:5}
        , herp: {type: 'string', minLength:5, maxLength:10}
      }

      , patternProperties: {
        'pat-\\d+': {type: 'string', pattern: 'patStr-\\w{1,20}'}
      }

      , additionalProperties: true
      , required: ['derp']
    }
  }

  , additionalProperties: false
  , required: ['someString', 'someObject']
  , definitions: {
    blaEnum: {enum: ['bla', 'dohoi', 666]}
  }
};

var schema2 = {
  id: 'http://www.example.com/derp#'
  , type: "object"
  , properties: {
    herps: {type: "array", items: {$ref: 'http://www.example.com/herp'}}
  }
};

var gen = new jfs.JsonFromSchema([schema1, schema2])
var sampleDerp = gen.generate('http://www.example.com/derp'); // note: no hash at the end

var sampleHerp = gen.generate('http://www.example.com/herp');

generate() options

generate() takes an options object as its second parameter. The following options are supported:

  • minCharCode and maxCharCode (integers): random strings are generated so that the character codes are between these two values
  • charSet (array): generate random strings using this character set. Each element of the array should be a single character
  • minRandomKeys and maxRandomKeys (integers): the minimum and maximum number of randomly generated keys an object can have when additionalProperties is true
  • minPatternProperties and maxPatternProperties (integers): minimum and maximum number of pattern properties to randomly generate
  • overrideMinItems and overrideMaxItems (integers): override array minItems and maxItems for all arrays when generating array contents. Useful for generating a certain minimum amount of test data, for example
  • requireAll (boolean): behave like all properties of an object were required
  • additionalProperties (boolean): overrides any/all additionalProperties keywords across the entire schema
  • useZulu (boolean): always use the Z "time zone" (e.g. 1965-12-13T11:34:13.713Z) when generating strings using the date-time format

Supported

  • $ref (JSON pointers and schema URIs)
  • string
    • pattern
    • format
      • ipv4
      • ipv6
      • date-time
  • array
    • maxItems
    • minItems
    • items (single schema)
  • number
    • minimum
    • maximum
  • integer
    • minimum
    • maximum
    • exclusiveMinimum
    • exclusiveMaximum
  • boolean
  • enum
  • null
  • object
    • properties
      • default values for properties
    • patternProperties
    • required
    • additionalProperties (boolean)
  • oneOf
  • anyOf
  • allOf
  • type keyword with an array (type: ['string', 'integer'])

TODO

  • number
    • exclusiveMinimum / exclusiveMaximum
  • number / integer
    • multipleOf
  • array
    • uniqueItems
    • additionalItems
    • items (array of schemas)
  • object
    • dependencies
    • maxProperties
    • minProperties
    • additionalProperties (schema)
  • not
  • string
    • more formats

json-from-schema's People

Contributors

abalmos avatar nikmartin avatar orbat avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.