Giter Club home page Giter Club logo

json-schema-generator's Introduction

json-schema-generator

JSON schema generated based on draft-v4 of the specification. Note that the full spec if not yet supported. The compiler will be enhanced to support as much as possible. More specifically, there's no support for $ref nodes or special nodes like location (lat, long), etc. These features will be added in future releases or you can always fork and make it better :-)

Install (GIT)

git clone https://github.com/krg7880/json-schema-generator
cd json-schema-generator
npm install .

Install as cli (NPM)

Run on the command line:

npm install -g json-schema-generator

Then use (for example):

#### JSON PATH
json-schema-generator path/to/input.json -o path/to/output.json
#### JSON URL
json-schema-generator https://sample.com/path/to/input.json --jsondir ./source/backup -o ./path/to/dir/
#### JSON STDIN | STDOUT
cat input.json | json-schema-generator > output.json

Install as lib (NPM)

Run on the command line:

npm install json-schema-generator --save-dev

Then, in your project:

var jsonSchemaGenerator = require('json-schema-generator'),
    obj = { some: { object: true } },
    schemaObj;

schemaObj = jsonSchemaGenerator(json);

Cli usage

Usage: json-schema-generator [<target>|--url <url>|--file <file>|--stdin]

If <target> is specified, it is interpreted as follows: a protocol (like http://) 
means url; anything else is treated as path to a local file. 
If no input file is specified and stdin is provided, stdin is used.

Options:
  --stdin          Use stdin as input.                                              
  --url            Remote json document to use as input.                            
  --file           Local json document to use as input.                             
  --schemadir, -o  Directory (or file, if ending with .json) where the schema will
                   be stored.                                                       
  --jsondir        Directory (or file, if ending with .json) where the source
                   document is copied to. Useful with --url.                        
  --pretty         Whether to use pretty json format. Use --no-pretty for false.
                                                                     [default: true]
  --force, -f      If a destination file already exists, overwrite it.              
  --help, -h       Show this help text.                                             

Example JSON

{
    "title": "fresh fruit schema v1",
    "type": "object",
    "required": ["skin", "colors", "taste"],
    "properties": {
        "colors": {
            "type": "array",
            "minItems": 1,
            "uniqueItems": true,
            "items": {
                "type": "string"
            }
        },
        "skin": {
            "type": "string"
        },
        "taste": {
            "type": "number",
            "minimum": 5
        }
    }
}

Example Output

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "description": "",
  "type": "object",
  "properties": {
    "title": {
      "type": "string",
      "minLength": 1
    },
    "type": {
      "type": "string",
      "minLength": 1
    },
    "required": {
      "type": "array",
      "items": {
        "required": [
          
        ],
        "properties": {
          
        }
      }
    },
    "properties": {
      "type": "object",
      "properties": {
        "colors": {
          "type": "object",
          "properties": {
            "type": {
              "type": "string",
              "minLength": 1
            },
            "minItems": {
              "type": "number"
            },
            "uniqueItems": {
              "type": "boolean"
            },
            "items": {
              "type": "object",
              "properties": {
                "type": {
                  "type": "string",
                  "minLength": 1
                }
              },
              "required": [
                "type"
              ]
            }
          },
          "required": [
            "type",
            "minItems",
            "uniqueItems",
            "items"
          ]
        },
        "skin": {
          "type": "object",
          "properties": {
            "type": {
              "type": "string",
              "minLength": 1
            }
          },
          "required": [
            "type"
          ]
        },
        "taste": {
          "type": "object",
          "properties": {
            "type": {
              "type": "string",
              "minLength": 1
            },
            "minimum": {
              "type": "number"
            }
          },
          "required": [
            "type",
            "minimum"
          ]
        }
      },
      "required": [
        "colors",
        "skin",
        "taste"
      ]
    }
  },
  "required": [
    "title",
    "type",
    "required",
    "properties"
  ]
}

Background

I created this schema generator to validate JSON responses from APIs. As the JSON API is enhanced and nodes are added or removed from the response, the schema is regenerated and validated against the newly deployed API.

Tests

To run tests, including fetching documents via HTTP, we've added node-stubby-server-cli to help with serving mock data. The ports for the stub server is defined under test/helpers/stubby-cli, in the event the default port is in use, you can change them there.

npm install -g stubby

Install mocha globally (as cli) and run

npm test

Validating Documents

JSON documents can be validated against schemas using chai-json-schema. See the tests under test for example usage.

Contributors

Thanks to those who have contributed. These kind folks are listed below:

json-schema-generator's People

Contributors

edsilv avatar esco avatar kirk7880 avatar krg7880 avatar msftenhanceprovenance avatar nickyout avatar rbren avatar schadha-ibm avatar tschef avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

json-schema-generator's Issues

Missing field for nested json

example json

{"web-app": {
"servlet": [
{
"a": "atest",
"b": "btest",
"init-param": {
"orange": 4,
"watermelon": true}},
{
"a": "ctest",
"b": "dtest",
"init-param": {
"apple": 4,
"banana": true}}]
}}

generated schema

{
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "",
"type": "object",
"properties": {
"web-app": {
"type": "object",
"properties": {
"servlet": {
"type": "array",
"uniqueItems": true,
"minItems": 1,
"items": {
"required": [
"a",
"b"
],
"properties": {
"a": {
"type": "string",
"minLength": 1
},
"b": {
"type": "string",
"minLength": 1
},
"init-param": {
"type": "object",
"properties": {
"orange": {
"type": "number"
},
"watermelon": {
"type": "boolean"

}
},
"required": [
"orange",
"watermelon"
]
}
}
}
}
},
"required": [
"servlet"
]
}
},
"required": [
"web-app"
]
}

apple and banana are missing....

Using the library in the browser?

Hi,

Is it possible to use the library in the browser? If yes, how do I package all the js file into a single for the purpose? Thanks for your help.

Only evaluates first parent keys

The test JSON example does not create the schema for child keys as it says in the documentation:

var jsonSchemaGenerator = require('json-schema-generator')

const json_raw = {
  "title": "fresh fruit schema v1",
  "type": "object",
  "required": ["skin", "colors", "taste"],
  "properties": {
      "colors": {
          "type": "array",
          "minItems": 1,
          "uniqueItems": true,
          "items": {
              "type": "string"
          }
      },
      "skin": {
          "type": "string"
      },
      "taste": {
          "type": "number",
          "minimum": 5
      }
  }
}

var jsonSchema = jsonSchemaGenerator(json_raw)
console.log(jsonSchema);

Outputs:

{
  '$schema': 'http://json-schema.org/draft-04/schema#',
  description: '',
  type: 'object',
  properties: {
    extra: { type: 'object', properties: [Object], required: [Array] },
    data: { type: 'object', properties: [Object], required: [Array] },
    key: { type: 'object', properties: [Object], required: [Array] },
    metadata: { type: 'object', properties: [Object], required: [Array] }
  },
  required: [ 'extra', 'data', 'key', 'metadata' ]
}

"Maximum call stack size exceeded" when type is object

When validating the following schema:

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "type": "object",
    "properties": {
        "event_type": {
            "type": "string"
        },
        "dependencies_type": {
            "type": "string"
        },
        "agent_type": {
            "type": "string"
        },
        "key": {
            "type": "string"
        },
        "node": {
            "type": "object",
            "properties": {
                "parent_id": {
                    "type": "string"
                },
                "value": {
                    "type": "object"
                },
                "id": {
                    "type": "string"
                }
            },
            "required": [
                "parent_id",
                "value",
                "id"
            ]
        }
    },
    "required": [
        "event_type",
        "node",
        "key",
        "dependencies_type",
        "agent_type"
    ]
}

The error below occurs. This is due to the type of the "value" field being "object".

RangeError: Maximum call stack size exceeded
    at String.replace (native)
    at new URLUtils (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/node_modules/json-schema-faker/node_modules/deref/lib/util/helpers.js:9:23)
    at new URLUtils (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/node_modules/json-schema-faker/node_modules/deref/lib/util/helpers.js:24:16)
    at parseURI (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/node_modules/json-schema-faker/node_modules/deref/lib/util/helpers.js:77:10)
    at Object.resolveURL (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/node_modules/json-schema-faker/node_modules/deref/lib/util/helpers.js:83:10)
    at Object.module.exports [as normalizeSchema] (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/node_modules/json-schema-faker/node_modules/deref/lib/util/normalize-schema.js:56:12)
    at /home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/node_modules/json-schema-faker/node_modules/deref/lib/index.js:59:18
    at Array.forEach (native)
    at $ref (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/node_modules/json-schema-faker/node_modules/deref/lib/index.js:58:35)
    at generate (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/node_modules/json-schema-faker/lib/jsf.js:19:21)
    at generateForProp (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:41:11)
    at generateNegativeType (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:81:11)
    at generateForTypes (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:389:44)
    at generate (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:452:42)
    at generateNegativesForObject (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:320:22)
    at generateNegativeDetailsForType (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:371:44)
    at generateForTypes (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:390:44)
    at generate (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:452:42)
    at generateNegativesForObject (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:320:22)
    at generateNegativeDetailsForType (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:371:44)
    at generateForTypes (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:390:44)
    at generate (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:452:42)
    at generateNegativesForObject (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:320:22)
    at generateNegativeDetailsForType (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:371:44)
    at generateForTypes (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:390:44)
    at generate (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:452:42)
    at generateNegativesForObject (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:320:22)
    at generateNegativeDetailsForType (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:371:44)
    at generateForTypes (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:390:44)
    at generate (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:452:42)
    at generateNegativesForObject (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:320:22)
    at generateNegativeDetailsForType (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:371:44)
    at generateForTypes (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:390:44)
    at generate (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:452:42)
    at generateNegativesForObject (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:320:22)
    at generateNegativeDetailsForType (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:371:44)
    at generateForTypes (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:390:44)
    at generate (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:452:42)
    at generateNegativesForObject (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:320:22)
    at generateNegativeDetailsForType (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:371:44)
    at generateForTypes (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:390:44)
    at generate (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:452:42)
    at generateNegativesForObject (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:320:22)
    at generateNegativeDetailsForType (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:371:44)
    at generateForTypes (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:390:44)
    at generate (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:452:42)
    at generateNegativesForObject (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:320:22)
    at generateNegativeDetailsForType (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:371:44)
    at generateForTypes (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:390:44)
    at generate (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:452:42)
    at generateNegativesForObject (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:320:22)
    at generateNegativeDetailsForType (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:371:44)
    at generateForTypes (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:390:44)
    at generate (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:452:42)
    at generateNegativesForObject (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:320:22)
    at generateNegativeDetailsForType (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:371:44)
    at generateForTypes (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:390:44)
    at generate (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:452:42)
    at generateNegativesForObject (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:320:22)
    at generateNegativeDetailsForType (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:371:44)
    at generateForTypes (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:390:44)
    at generate (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:452:42)
    at generateNegativesForObject (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:320:22)
    at generateNegativeDetailsForType (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:371:44)
    at generateForTypes (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:390:44)
    at generate (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:452:42)
    at generateNegativesForObject (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:320:22)
    at generateNegativeDetailsForType (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:371:44)
    at generateForTypes (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:390:44)
    at generate (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:452:42)
    at generateNegativesForObject (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:320:22)
    at generateNegativeDetailsForType (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:371:44)
    at generateForTypes (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:390:44)
    at generate (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:452:42)
    at generateNegativesForObject (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:320:22)
    at generateNegativeDetailsForType (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:371:44)
    at generateForTypes (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:390:44)
    at generate (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:452:42)
    at generateNegativesForObject (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:320:22)
    at generateNegativeDetailsForType (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:371:44)
    at generateForTypes (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:390:44)
    at generate (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:452:42)
    at generateNegativesForObject (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:320:22)
    at generateNegativeDetailsForType (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:371:44)
    at generateForTypes (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:390:44)
    at generate (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:452:42)
    at generateNegativesForObject (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:320:22)
    at generateNegativeDetailsForType (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:371:44)
    at generateForTypes (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:390:44)
    at generate (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:452:42)
    at generateNegativesForObject (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:320:22)
    at generateNegativeDetailsForType (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:371:44)
    at generateForTypes (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:390:44)
    at generate (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:452:42)
    at generateNegativesForObject (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:320:22)
    at generateNegativeDetailsForType (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:371:44)
    at generateForTypes (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:390:44)
    at generate (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:452:42)
    at generateNegativesForObject (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:320:22)
    at generateNegativeDetailsForType (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:371:44)
    at generateForTypes (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:390:44)
    at generate (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:452:42)
    at generateNegativesForObject (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:320:22)
    at generateNegativeDetailsForType (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:371:44)
    at generateForTypes (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:390:44)
    at generate (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:452:42)
    at generateNegativesForObject (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:320:22)
    at generateNegativeDetailsForType (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:371:44)
    at generateForTypes (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:390:44)
    at generate (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:452:42)
    at generateNegativesForObject (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:320:22)
    at generateNegativeDetailsForType (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:371:44)
    at generateForTypes (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:390:44)
    at generate (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:452:42)
    at generateNegativesForObject (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:320:22)
    at generateNegativeDetailsForType (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:371:44)
    at generateForTypes (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:390:44)
    at generate (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:452:42)
    at generateNegativesForObject (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:320:22)
    at generateNegativeDetailsForType (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:371:44)
    at generateForTypes (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:390:44)
    at generate (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:452:42)
    at generateNegativesForObject (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:320:22)
    at generateNegativeDetailsForType (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:371:44)
    at generateForTypes (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:390:44)
    at generate (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:452:42)
    at generateNegativesForObject (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:320:22)
    at generateNegativeDetailsForType (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:371:44)
    at generateForTypes (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:390:44)
    at generate (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:452:42)
    at generateNegativesForObject (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:320:22)
    at generateNegativeDetailsForType (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:371:44)
    at generateForTypes (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:390:44)
    at generate (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:452:42)
    at generateNegativesForObject (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:320:22)
    at generateNegativeDetailsForType (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:371:44)
    at generateForTypes (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:390:44)
    at generate (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:452:42)
    at generateNegativesForObject (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:320:22)
    at generateNegativeDetailsForType (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:371:44)
    at generateForTypes (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:390:44)
    at generate (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:452:42)
    at generateNegativesForObject (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:320:22)
    at generateNegativeDetailsForType (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:371:44)
    at generateForTypes (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:390:44)
    at generate (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:452:42)
    at generateNegativesForObject (/home/miaou/projects/events/couchapp-dependencies/node_modules/json-schema-test-data-generator/dist/index.js:320:22)npm ERR! Test failed.  See above for more details.

json-schema-generator doesn't handle arrays of primitives correctly

When given an object like:

{myArray: ['one', 'two']}

The generator creates a schema like the following:

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "description": "",
    "properties": {
        "myArray": {
            "type": "array",
            "items": {
                "required": [],
                "properties": {}
            }
        }
    },
    "required": ["myArray"],
    "type": "object"
}

The "items" should instead be something like:

"items": {
    "type": "string",
    "minLength": 1
}

The currently-generated schema breaks validators like ajv.

Mocha/Chai deep.equal fails when validating

I am using your json schema to validate two endpoints/responses, and I am having trouble using a working validator. The only one that works well, and with complex JSON is the deep.equal in Mocha/Chai.

The issue occurs when the required field appears with an array and not consistently in the same order, which deep.equal flags as a fail. Could you possible sort the required array, or at least produce an array that's consistently in the same order? That would really help me out.

Required does not properly work for arrays

In an array of items in the array, required is set even if it is not true.

> const jsonSchemaGenerator = require('json-schema-generator')
undefined
> jsonSchemaGenerator([{a: 1, b: 2}, {a: 2, b: 3}, {b: 5}])
{
  type: 'array',
  '$schema': 'http://json-schema.org/draft-04/schema#',
  description: '',
  minItems: 1,
  uniqueItems: true,
  items: {
    type: 'object',
    required: [ 'a', 'b' ],
    properties: { a: [Object], b: [Object] }
  }
}
>

In the previous example a cannot be required since it does not exist in the third element

Update shrinkwrap and request dependency due to tough-cookie

Update tough-cookie version to at least 2.3.0 https://github.com/krg7880/json-schema-generator/blob/master/npm-shrinkwrap.json#L150

Update request to v2.81.1
Reason being is that the dependency tough-cookie is v2.3.0 on that version while on request v2.47.x and v2.51.x uses tough-cookie v0.12.0 which has vulnerability issues

npm WARN deprecated [email protected]: ReDoS vulnerability parsing Set-Cookie https://nodesecurity.io/advisories/130

The latest request module has [email protected] which fixes this issue

schema generated for an empty array is invalid

const gen = require('json-schema-generator')
const testObj = {test: []}
const schema = gen(testObj)
console.log(schema)

This outputs:

{ '$schema': 'http://json-schema.org/draft-04/schema#',
  description: '',
  type: 'object',
  properties: 
   { test: 
      { type: 'array',
        uniqueItems: undefined,
        minItems: undefined,
        items: [Object] } },
  required: [ 'test' ] }

This is problematic as the value of minItems inside schema.properties.test is undefined, so when the original object is tested against the generated schema, we have a validation error.

var Validator = require('jsonschema').Validator
const validator = new Validator()
const result = validator.validate(testObj, schema, {throwError: false})

result.errors contains:

[ ValidationError {
    property: 'instance.test',
    message: 'does not meet minimum length of undefined',
    schema: 
     { type: 'array',
       uniqueItems: undefined,
       minItems: undefined,
       items: [Object] },
    instance: [],
    name: 'minItems',
    argument: undefined,
    stack: 'instance.test does not meet minimum length of undefined' } ]

Update request -> extend

Prototype Pollution security issue

  • We are using [email protected] which is the latest version and found this security vulnerability:
High severity vulnerability found in extend
Description: Prototype Pollution
Introduced through: [email protected]
From: [email protected] > [email protected] > [email protected]

Solution is to use the right version of request which does not use [email protected]; if latest version of request still has that issue, then request needs to fix that by using the latest version of extend where they fix this issue.

Can it be used in nodejs program

I did npm install json-schema-generator, then added this line of code.
schemaGen = require 'json-schema-generator';

I am getting the Error: Cannot find module 'pretty-data'.

It seems to be pretty-data needs to be moved from dev-dependencies to dependencies in package.json.

Thanks,
Raghu

JSON schema generation on object with an array.

The json schema generator does not generate schema the way I want it to.
For e.g. When I create the schema on the object : {c: ["a", "b", "c"]}

The schema created is as below:

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "description": "",
  "type": "object",
  "properties": {
    "c": {
      "type": "array",
      "items": {
        "required": [
          
        ],
        "properties": {
          
        }
      },
      "value": [
        "a",
        "b",
        "c"
      ]
    }
  },
  "required": [
    "c"
  ]
}

In the above schema - the array c, has a default value of [“a”, “b”, “c”]

Instead my requirement is that, we should create the schema as below.

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "description": "",
  "type": "object",
  "properties": {
    "c": {
      "type": "array",
      "items": {
        "type": "string",
        "enum": [
          "a",
          "b",
          "c"
        ]
      }
    }
  },
  "required": [
    "c"
  ]
}

Need a newer version of `extend` dependency

Patch available for [email protected]
  ✗ Prototype Pollution [High Severity][https://snyk.io/vuln/npm:extend:20180424] in [email protected]
    introduced by [email protected] > [email protected] > [email protected] and 2 other path(s)

This might be a duplicate of #27 , but it seems the latest [email protected] has old request which contain [email protected].

Need a new version of it to pick up the [email protected] based on https://snyk.io/vuln/npm:extend:20180424 .

Add support additionalProperties

Hello there and thank you for this lib!
Please add support for additionalProperties, they are true by default causing obsolete code in typescript.

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.