Fail to validate schema and correctly use additionalProperties

五迷三道 提交于 2019-12-12 13:25:13

问题


I'm trying to validate my JSON schema and use the additionalProperties: false to confirm there is not other properties. My responseBody look like this:

[
  {
    "id": 1234567890987654,
    "email": "eemail@domain.com",
    "civility": 0,
    "firstname": "john",
    "lastname": "do",
    "function": null,
    "phone": null,
    "cellphone": null,
    "role": 1,
    "passwordws": "jdnfjnshn55fff5g8"
  },
  {
...}
]

In the postman tests, I add this

var schema = {
    "type": "array",
    "properties": {
        "id": {"type":"number"},
        "email": {"type":"string"},
        "civility": {"type":"number"},
        "firstname": {"type":"string"},
        "lastname": {"type":"string"},
        "function": {"type":"string"},
        "cellphone": {"type":"string"},
        "role": {"type":"number"},
        "passwordws": {"type":"string"},
    },
    "additionalProperties": false,
    "required": ["id", "email", "civility", "role", "passwordws"]
};

var data = JSON.parse(responseBody);
var result = tv4.validateResult(data, schema);
tests["Valid schema"] = result.valid;

the test should return FAIL because I deleted the "phone" properties from schema, but test still run valid... I tried to change the schema to {type:array, properties: {type: object, properties {list of properties}additionalProperties: false}} but test still return PASS instead of FAIL... any idea ?


回答1:


Your response is an array of object, and i saw:

  • the array's object is not define

  • type id define with "number" instead of "integer"

Try this:

var schema = {
  "type":"array",
  "items": { "$ref": "#/definitions/MyObject" }

  "definitions" : {
        "MyObject" : {
          "type":"object",
            "required" : ["id", "email", "civility", "role", "passwordws"],
            "properties": {
                "id": {"type":"integer"},
                "email": {"type":"string"},
                "civility": {"type":"integer"},
                "firstname": {"type":"string"},
                "lastname": {"type":"string"},
                "function": {"type":"string"},
                "phone": {"type":"string"},
                "cellphone": {"type":"string"},
                "role": {"type":"integer"},
                "passwordws": {"type":"string"}
            },
           "additionalProperties": false,
        },
    },
};



回答2:


After some test, and log the result, the error was due to the null value I received sometimes in objects. I changed the schema you sent me

{
    "type": "array",
    "items": {
        "$ref": "#/definitions/MyObject"
    },

    "definitions": {
        "MyObject": {
            "type": "object",
            "required": ["id", "email", "civility", "role", "passwordws"],
            "properties": {
                "id": {
                    "type": "integer"
                },
                "email": {
                    "type": "string"
                },
                "civility": {
                    "type": "integer"
                },
                "firstname": {
                    "type": ["string", "null"]
                },
                "lastname": {
                    "type": ["string", "null"]
                },
                "function": {
                    "type": ["string", "null"]
                },
                "phone": {
                    "type": ["string", "null"]
                },
                "cellphone": {
                    "type": ["string", "null"]
                },
                "role": {
                    "type": "integer"
                },
                "passwordws": {
                    "type": "string"
                }
            },
            "additionalProperties": false
        }
    }
};

and I'm now able to validate correctly the schema.

Many thanks



来源:https://stackoverflow.com/questions/34286679/fail-to-validate-schema-and-correctly-use-additionalproperties

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!