Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AVJ not validating enum types

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

like image 489
RubberDuck Avatar asked May 13 '20 19:05

RubberDuck


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"]
    }
  }
}
like image 92
awwright Avatar answered Sep 28 '22 08:09

awwright