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 ?

4

1 回答 1

0

您需要在数组中添加对象

改变这个

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

};

对此

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

};

于 2019-05-02T11:01:08.300 回答