Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

schema generated for an empty array is invalid #20

Open
brendonjohn opened this issue Jan 13, 2017 · 0 comments
Open

schema generated for an empty array is invalid #20

brendonjohn opened this issue Jan 13, 2017 · 0 comments

Comments

@brendonjohn
Copy link

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' } ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant