json-from-schema generates random JSON based on JSON Schema draft v4 schemas.
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()
takes an options object as its second parameter. The following options are supported:
minCharCode
andmaxCharCode
(integers): random strings are generated so that the character codes are between these two valuescharSet
(array): generate random strings using this character set. Each element of the array should be a single characterminRandomKeys
andmaxRandomKeys
(integers): the minimum and maximum number of randomly generated keys an object can have when additionalProperties is trueminPatternProperties
andmaxPatternProperties
(integers): minimum and maximum number of pattern properties to randomly generateoverrideMinItems
andoverrideMaxItems
(integers): override array minItems and maxItems for all arrays when generating array contents. Useful for generating a certain minimum amount of test data, for examplerequireAll
(boolean): behave like all properties of an object were requiredadditionalProperties
(boolean): overrides any/all additionalProperties keywords across the entire schemauseZulu
(boolean): always use the Z "time zone" (e.g.1965-12-13T11:34:13.713Z
) when generating strings using thedate-time
format
- $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)
- properties
- oneOf
- anyOf
- allOf
- type keyword with an array (
type: ['string', 'integer']
)
- number
- exclusiveMinimum / exclusiveMaximum
- number / integer
- multipleOf
- array
- uniqueItems
- additionalItems
- items (array of schemas)
- object
- dependencies
- maxProperties
- minProperties
- additionalProperties (schema)
- not
- string
- more formats