Ajv: Isn't "type" an allowed property name ?

Created on 3 Mar 2016  路  13Comments  路  Source: ajv-validator/ajv

In this schema:

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "title": "effects",
  "type": "array",
  "uniqueItems": true,
  "items" : {
    "title": "effect",
    "type": "object",
    "properties": {
      "id": {
        "description": "The unique identifier for an effect",
        "type": "integer",
        "minimum": 0
      },
      "displayName": {
        "description": "The display name of an effect",
        "type": "string"
      },
      "name": {
        "description": "The name of an effect",
        "type": "string",
        "pattern": "\\S+"
      },
      "type": {
        "description": "Whether an effect is positive or negative",
        "type": { "enum": [ "good","bad" ] }
      }
    },
    "required": ["id", "displayName", "type", "name"],
    "additionalProperties":false
  }
}

I'm getting

Error: schema is invalid:data.items.properties['type'].type should be equal to one of the allowed values, data.items.properties['type'].type should be array, data.items.properties['type'].type should match some schema in anyOf, data.items should be array, data.items should match some schema in anyOf

I don't understand why.

Is it really forbidden to have object with a property named "type" ?

(I'm not getting any error for this with the jsonschema package)

usage

Most helpful comment

this:

"type": {
   "description": "Whether an effect is positive or negative",
   "type": { "enum": [ "good","bad" ] }
}

is invalid.

object is not a correct value for type keyword, should be either string or array of strings.

this:

"type": {
   "description": "Whether an effect is positive or negative",
   "enum": [ "good","bad" ]
}

is valid

All 13 comments

this:

"type": {
   "description": "Whether an effect is positive or negative",
   "type": { "enum": [ "good","bad" ] }
}

is invalid.

object is not a correct value for type keyword, should be either string or array of strings.

this:

"type": {
   "description": "Whether an effect is positive or negative",
   "enum": [ "good","bad" ]
}

is valid

Indeed. Thanks !

Just moved all my json schema tests from jsonschema to ajv. 43x speed up. Thanks for creating this package !

Thank you :)
43x sounds about right :)
benchmark is 100x + but it depends on the schema of course.

Does type as a top level field work? I'm having trouble with both jsonschema and ajv. Maybe I'm missing something?

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

var schema = {
    "type": {
       "description": "Whether an effect is positive or negative",
       "type": { "enum": [ "good","bad" ] }
    }
};

var validate = ajv.compile(schema);

test({
  "type": "good"
});

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

"Error: schema is invalid: data.type should be equal to one of the allowed values, data.type should be array, data.type should match some schema in anyOf"

Tested in https://npm.runkit.com/ajv

@tejasmanohar The way to define top level property "type" is the same as for any other protperty name - use "properties" keyword with the map of property schemas inside:

{
  "type": "object",
  "properties": {
    "type": {
      "enum": [ "good","bad" ]
    }
  }
}

please see JSON schema dosc and some tutorial.

Oops my bad. Sorry for the silly question @epoberezkin

I had a doubt while keeping a type property for validation.
I have a type property in body, which is of type string.

{
    "type": "object",
    "required": ["customer"],
    "properties": {
        "customer": {"type": "string"},
        "type" : {"type": "string"}
    }
}

I am getting the error :
Error: schema is invalid: data.type should be equal to one of the allowed values, data.type should be array, data.type should match some schema in anyOf
my request has

{
    "customer":"abcdefgh1234",
    "type":"booking",
}

can you help me out debug this issue ?
@epoberezkin
TIA
Rohit

Most likely you are passing data in place of schema - difficult to test without seeing the code

@epoberezkin I have the same issue here.

Doing :

schema.json

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "id": "#/s3Document",
  "type": "object",
  "additionalProperties": true,
  "properties": {
    "url": {
      "type": "string",
      "format": "uri"
    },
    "type": {
      "type": "string"
    }
  }
}

validate.js

const Ajv = require('ajv');

const ajv = new Ajv({
  allErrors: true,
  coerceTypes: true
});
const schema = require('./schema.json');
const schemaValidator = ajv.compile(schema);

module.exports = (data) => { 
  return schemaValidator(data);
});

index.js

const validate = require('./validate');

console.log('Is valid:', validate({
  "url": "http://www.google.com",
  "type": "site"
}));

Running:

$ node index.js

I get this error:

Error: schema is invalid: data.type should be equal to one of the allowed values, data.type should be array, data.type should match some schema in anyOf

I don't see how else I could express that type should be a string...
Could you help ?

Changing the $schema to "http://json-schema.org/draft-07/schema#" and removing the id solved the issue...
Sorry for the bothering.

we are having the same problem

// data sample
data = {
    from: "[email protected]"
    presenceChangeType: "sync"
    status: "online"
    subStatus: ""
    timestamp: 1571059446068
    type: "presence"
}
// schema

var dataSchema = {
  "from": { type: "string" },
  "status": [
    {
      "properties": {
        "presence": { "type": "string" },
      },
    },
  ],
  "subStatus": { "type": "string" },
  "presenceChangeType": { "type": "string" },
  "timestamp": { "type": "integer" },
  "type": {
    "const": "presence" ,
  }
};

we get the following error:

Error: schema is invalid: data.type should be equal to one of the allowed values, data.type should be array, data.type should match some schema in anyOf

as I wrote, you probably use data instead of schema. submit the whole code sample to runkit.com to look further

Was this page helpful?
0 / 5 - 0 ratings