Ajv: additionalProperties: false has no effect

Created on 28 Mar 2020  路  7Comments  路  Source: ajv-validator/ajv

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

"version": "6.12.0"

Ajv options object

const options = {
  v5: true,
  allErrors: true,
  additionalProperties: false,
}

JSON Schema

const schema =
{
    "type": "object",
    "required": [
      "givenName",
      "familyName"
    ],
    "properties": {
      "givenName": {
        "type": "string"
      },
      "familyName": {
        "type": "string"
      }
    }
  }

Sample data

const test_user = {
  givenName: "Bla",
  familyName: "Bla",
  thisShouldFail: "but it will not"
}

Your code

var Ajv = require('ajv');
var ajv = new Ajv(options);

var validate = ajv.compile(schema);
var valid = validate(test_user);
console.log(valid);

Validation result, data AFTER validation, error messages


What results did you expect?


Are you going to resolve the issue?

Hopefully I am just doing something wrong.

usage

Most helpful comment

Try items, not item, within your array definition. If you turn on the strictKeywords option, it should catch mistakes like this.

All 7 comments

I believe the problem is that additionalProperties is not an Ajv option. It is a keyword on the object schema. Try adding it to your example schema at the top level, like this:

const schema =
{
    "type": "object",
    "additionalProperties": false,
    "required": [
      "givenName",
      "familyName"
    ],
    "properties": {
      "givenName": {
        "type": "string"
      },
      "familyName": {
        "type": "string"
      }
    }
}

@ryanmeador that is correct

It works! Thank you! :)

Hi guys, sorry to bother you again, but I've got a related issue and I am not abble to google the solution. My understanding is that additionalProperties applies to nested objects too. But I have this schema:

{
    "type": "object",
    "required": [
      "givenName",
      "familyName"
    ],
    "properties": {
      "familyMembers": {
        "type": "array",
        "item": {
          "type": "object",
          "additionalProperties": false,
          "properties": {
            "givenName": {
              "type": "string"
            },
            "familyName": {
              "type": "string"
            }
          }
        }
      },
      "email": {
        "type": "string"
      },
      "givenName": {
        "type": "string"
      },
      "familyName": {
        "type": "string"
      }
    }
  }

And this object:

const test_user3 = {
    email: "[email protected]",
    givenName: "John",
    familyName: "Smith",
    familyMembers: [
        { givenName: "John", gender: "Male", birthDate: "2020-02-99" },
        { givenName: "Jane", gender: "Female" },
        { givenName: "Jason", gender: "Neuter" }
    ]
}

And it validates. Any ideas please?

Try items, not item, within your array definition. If you turn on the strictKeywords option, it should catch mistakes like this.

That was it, unbelievable... thanks a lot @ryanmeador !

@ryanmeador thank you, @simundev good it's solved

Was this page helpful?
0 / 5 - 0 ratings