0

I am using is-my-json-valid npm module to validate incoming http request. I defined schema to validate array of objects . This npm module failed to validate objects inside array correctly.

I have defined the schema as mentioned below:

var validator = require('is-my-json-valid')


var validate = validator({
    required: true,
    type: 'object',
    properties: {
        name: {
            required: true,
            type: 'string'
        },
        author: {
            required: true,
            type: 'string'
        },
        libraries: {
            required: true,
            type: 'array',
            items: {
                type: 'object',
                properties: {
                    id: {
                        required: true,
                        type: 'number'
                    }
                },
                additionalProperties: false
            }
        }
    },
    additionalProperties: false
});

const obj = {
    name: 'myn4m3',
    author: 'mys3lf',
    libraries: []
};
console.log('should be valid', validate(obj));
// console.log('should not be valid', validate({}))
console.log(validate.errors) 

Actual: should be valid true null

Expected: since, libraries array had mandatory "id" property as i'm not providing it it should throw validation error but it gives true.

can someone help on this ?

unnamed-bull
  • 361
  • 4
  • 15

1 Answers1

0

You need to add Objects in array

change this

const obj = {
name: 'myn4m3',
author: 'mys3lf',
libraries: []

};

to this

const obj = {
name: 'myn4m3',
author: 'mys3lf',
libraries: [{}]

};

Irakli
  • 58
  • 2
  • 9