ajv-validator / ajv

The fastest JSON schema Validator. Supports JSON Schema draft-04/06/07/2019-09/2020-12 and JSON Type Definition (RFC8927)
https://ajv.js.org
MIT License
13.72k stars 872 forks source link

Cannot verify the required object properties in an array. #746

Closed shuowpro closed 6 years ago

shuowpro commented 6 years ago

What version of Ajv are you using? Does the issue happen if you use the latest version?

6.3.0.

Yes.

Ajv options object


{allErrors: true}

JSON Schema


{
    "title": "ARRs",
    "type": "object",
    "properties": {
        "ARRs": {
            "type": "array",
            "items": {
                "type": "object",
                "properties": {
                    "name": {
                        "type": "string",
                        "title": "Name"
                    }
                },
                "required": ["name"]
            }
        }
    }
}

Sample data


vsds: [{}]}

Your code


var Ajv = require('ajv');
var ajv = new Ajv({allErrors: true});

var schema = {
    "title": "ARRs",
    "type": "object",
    "properties": {
        "ARRs": {
            "type": "array",
            "items": {
                "type": "object",
                "properties": {
                    "name": {
                        "type": "string",
                        "title": "Name"
                    }
                },
                "required": ["name"]
            }
        }
    }
}

var validate = ajv.compile(schema);

test({vsds: [{}]});

function test(data) {
  var valid = validate(data);
  if (valid) console.log('Valid!');
  else console.log('Invalid: ' + ajv.errorsText(validate.errors));
}

Validation result, data AFTER validation, error messages

Valid!

What results did you expect?

Error! The name is required.

epoberezkin commented 6 years ago

"ARRs" != "vsds"