AVJ not validating enum types

Viewed 3841

Apologies if this has already been ask, but I wasn't able able to find an answer that works

I am having trouble validating a JSON Schema using an enum type with AVJ

I would expect the below code to return false, since the given value does not appear in the enum type

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

var schema = {
  gender: {
    enum: [
      'male',
      'female',
      'other'
    ]
  }
};
ajv.validate(schema, { gender: 'test' });
// returns true

Are you able to let me know how to fix this please

1 Answers

In JSON Schema, all properties in the schema are directives called keywords. Unknown keywords are ignored.

In your schema, "gender" isn't a known JSON Schema keyword, so it's going to be ignored. You're probably looking for the "properties" keyword:

{
  properties: {
    "gender": {
      enum: ["male", "female", "other"]
    }
  }
}
Related