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

json-from-schema

v1.11.0

Published

Generates random JSON using a JSON schema

Downloads

336

Readme

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