├── .gitignore ├── LICENSE ├── README.md ├── cli.js ├── index.js ├── index_.coffee └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | /node_modules/* 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # JSON Schema random 2 | 3 | Given a JSON Schema (draft 04), provide a random valid instance. 4 | 5 | # Disclaimer 6 | 7 | This is an choppy piece of software, that is in need of some care and affection. 8 | 9 | # TODO 10 | 11 | * format 12 | * uniqueItems 13 | * $ref 14 | * patternProperties 15 | * dependencies 16 | * allOf 17 | * anyOf 18 | * oneOf 19 | * not 20 | 21 | # Install 22 | 23 | ```bash 24 | npm install json-schema-random 25 | ``` 26 | 27 | # Usage 28 | 29 | ```bash 30 | # Return a generated JSON instance to stdout of the JSON_SCHEMA_FILE 31 | json-schema-random JSON_SCHEMA_FILE 32 | ``` 33 | 34 | ```coffee 35 | # Or, in your source code 36 | generate = require 'json-schema-random' 37 | generate {type: 'number'} 38 | ``` 39 | 40 | ## Command-line options 41 | 42 | `--schemaid` - set `$schema` field to `id` found in the schema document 43 | 44 | `--no-additional` - don't generate fields for `additionalProperties` 45 | 46 | `--no-random` - return blank values instead of random 47 | 48 | # License 49 | 50 | Apache 2.0 51 | -------------------------------------------------------------------------------- /cli.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | /*jshint node:true*/ 3 | var fs = require('fs'), 4 | generate = require('./index.js'), 5 | minimist = require('minimist'), 6 | schema, 7 | instance; 8 | 9 | var args = minimist(process.argv.slice(2), { 10 | default: { 11 | method: 'all', 12 | additional: true, 13 | random: true 14 | } 15 | }); 16 | 17 | schema = JSON.parse(fs.readFileSync(args._[0], 'utf8')); 18 | instance = generate(schema, args); 19 | console.log(JSON.stringify(instance, null, 4)); 20 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | /*jshint node:true*/ 2 | require('coffee-script'); 3 | module.exports = require('./index_'); 4 | -------------------------------------------------------------------------------- /index_.coffee: -------------------------------------------------------------------------------- 1 | random = require 'otw/like/_/random' 2 | type = require 'otw/like/_/type' 3 | randexp = do () -> 4 | _randexp = require 'randexp' 5 | (pattern) -> new _randexp(pattern).gen() 6 | 7 | 8 | module.exports = exports = (schema, options = {}) -> 9 | options.root_schema = schema 10 | 11 | generator = new exports.Generator options 12 | return generator.generate() 13 | 14 | class exports.Generator 15 | 16 | constructor: (options) -> 17 | @options = options 18 | 19 | generate: (schema = null, depth = 0) -> 20 | 21 | schema = @options.root_schema unless schema 22 | 23 | return schema unless type(schema) is 'object' 24 | 25 | return @enum schema, depth if schema.enum 26 | 27 | if schema.$ref 28 | return @ref schema.$ref, depth 29 | 30 | switch schema.type 31 | when 'string', 'number', 'integer', 'boolean', 'array', 'object', 'null' 32 | return @[schema.type] schema, depth 33 | else 34 | if type(schema.type) is 'array' 35 | return @oneOf schema, depth 36 | # FIXME what if type(schema.type) = 'object' and _.size(schema.type) > 0 ? 37 | if schema.type is 'any' or type(schema.type) is 'undefined' or (type(schema.type) is 'object' and _.size(schema.type) is 0) 38 | return @any schema, depth 39 | # FIXME what type is this? 40 | console.log schema.type 41 | throw "what type is this ?" 42 | 43 | 44 | # enum 45 | 'enum': (schema, depth = 0) -> 46 | randomOptions = 47 | minimum: 0 48 | maximum: schema.enum.length - 1 49 | randomIndex = @random 'integer', randomOptions 50 | enumValue = schema.enum[randomIndex] 51 | @generate enumValue, depth + 1 52 | 53 | 54 | # type = null 55 | 'null': () -> 56 | null 57 | 58 | 59 | # type = boolean 60 | boolean: (schema, depth = 0) -> 61 | @random 'boolean' 62 | 63 | 64 | # type = number / integer 65 | number: (schema, depth = 0) -> 66 | @integer schema, depth 67 | 68 | integer: (schema, depth = 0) -> 69 | minimum = schema.exclusiveMinimum + 1 if schema.exclusiveMinimum 70 | minimum ?= schema.minimum 71 | maximum = schema.exclusiveMaximum - 1 if schema.exclusiveMaximum 72 | maximum ?= schema.maximum 73 | decimals = 0 74 | decimals = @random 'number', {minimum: 0, maximum: 3} if schema.type is 'number' 75 | 76 | randomOptions = 77 | minimum: minimum 78 | maximum: maximum 79 | divisibleBy: schema.divisibleBy 80 | decimals: decimals 81 | @random 'number', randomOptions 82 | 83 | 84 | # type = string 85 | string: (schema, depth = 0) -> 86 | return randexp schema.pattern if schema.pattern 87 | # FIXME format should not be ignored 88 | randomOptions = 89 | minLength: schema.minLength 90 | maxLength: schema.maxLength 91 | @random 'string', randomOptions 92 | 93 | 94 | # type = array 95 | array: (schema, depth = 0) -> 96 | o = [] 97 | 98 | if type(schema.items) is 'object' and schema.items.$ref 99 | result = @ref schema.items.$ref, depth 100 | return [result] 101 | 102 | if type(schema.items) is 'array' and schema.items.length 103 | for itemSchema in schema.items 104 | o.push @generate itemSchema, depth + 1 105 | 106 | if schema.additionalItems 107 | if type(schema.minItems) isnt 'undefined' 108 | minimum = schema.minItems - o.length 109 | else 110 | minimum = 1 111 | minimum = 1 if minimum < 0 112 | # minimum set to 1, not 0, on purpose 113 | if type(schema.maxItems) isnt 'undefined' 114 | maximum = schema.maxItems - o.length 115 | else 116 | maximum = o.length + @random 'integer', {minimum: 0, maximum: 2} 117 | randomOptions = 118 | minimum: minimum 119 | maximum: maximum 120 | howManyMoreItems = @random 'integer', randomOptions 121 | while howManyMoreItems 122 | howManyMoreItemsLeft = howManyMoreItems - o.length 123 | if howManyMoreItemsLeft 124 | for i in [1..howManyMoreItemsLeft] 125 | o.push @generate schema.additionalItems, depth + 1 126 | o = _.deepUnique o if schema.uniqueItems 127 | howManyMoreItemsLeft = howManyMoreItems - o.length 128 | break unless howManyMoreItemsLeft 129 | o 130 | 131 | 132 | # type = object 133 | object: (schema, depth = 0) -> 134 | if schema.oneOf 135 | oneof_schema = type: schema.oneOf 136 | return @oneOf oneof_schema, depth 137 | 138 | o = {} 139 | for key, prop of schema.properties 140 | continue unless @options.method is 'all' or (type(schema.required) is 'array' and key in schema.required) 141 | # FIXME 142 | # continue if random 'boolean' 143 | o[key] = @generate prop, depth + 1 144 | 145 | if type(schema.additionalProperties) is 'object' 146 | # break if schema.additionalProperties.$ref?[0] is '#' 147 | randomOptions = 148 | minimum: 1 149 | maximum: 3 150 | # minimum set to 1, not 0, on purpose 151 | howManyMoreProperties = @random 'integer', randomOptions 152 | for i in [0..howManyMoreProperties] 153 | o[@random 'string', {minimum: 0, maximum: 10}] = @generate schema.additionalProperties, depth+1 154 | 155 | else if @options.additional and schema.additionalProperties isnt false 156 | o[@random 'string', {minimum: 0, maximum: 10}] = @generate {type: 'any'}, depth+1 157 | 158 | if @options.schemaid and schema.id 159 | o.$schema = schema.id 160 | 161 | return o 162 | 163 | 164 | # type = any 165 | any: (schema, depth = 0) -> 166 | types = ['string', 'number', 'integer', 'boolean', 'object', 'null'] 167 | randomOptions = 168 | minimum: 0 169 | maximum: types.length-1 170 | typeIndex = @random 'integer', randomOptions 171 | @generate {type: types[typeIndex]}, depth + 1 172 | 173 | 174 | # type = [] 175 | oneOf: (schema, depth = 0) -> 176 | return 0 unless schema.type.length # treat as 'any' 177 | randomOptions = 178 | minimum: 0 179 | maximum: schema.type.length-1 180 | typeIndex = @random 'integer', randomOptions 181 | @generate schema.type[typeIndex], depth + 1 182 | 183 | 184 | ref: (ref, depth = 0) -> 185 | path = ref.split('/') 186 | if path[0] != '#' 187 | throw "ref does not start with #: " + ref 188 | 189 | schema = @options.root_schema 190 | for name in path.slice(1) 191 | schema = schema[name] 192 | if not schema 193 | throw "$ref traversal failed: " + ref 194 | 195 | return @generate schema, depth 196 | 197 | random: (type, options) -> 198 | if @options.random 199 | return random.apply random, arguments 200 | 201 | switch type 202 | when 'number', 'integer', 'int' 203 | 0 204 | when 'boolean', 'bool' 205 | false 206 | when 'string' 207 | "" 208 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "json-schema-random", 3 | "description": "Given a JSON Schema, provide a random valid instance", 4 | "version": "0.0.4", 5 | "author": "Andrei Neculau <@andreineculau>", 6 | "users": { 7 | "andreineculau": true 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git://github.com/andreineculau/json-schema-random.git" 12 | }, 13 | "keywords": [ 14 | "json", 15 | "schema", 16 | "random" 17 | ], 18 | "license": "Apache 2", 19 | "engines": { 20 | "node": ">=0.8.21" 21 | }, 22 | "dependencies": { 23 | "coffee-script": "~1.6.0", 24 | "randexp": "~0.3.1", 25 | "otw": "git://github.com/andreineculau/otw.git#v0.0.2", 26 | "minimist": "~0.2.0" 27 | }, 28 | "bin": { 29 | "json-schema-random": "cli.js" 30 | } 31 | } 32 | --------------------------------------------------------------------------------