jsonschema icon indicating copy to clipboard operation
jsonschema copied to clipboard

in some cases, the verification result is wrong

Open nic-chen opened this issue 5 years ago • 1 comments

wrong case:

local schema = {
    type = "object",
    oneOf = {        
        {
            title = "work with consumer object",
            properties = {
                access_key = {type = "string", minLength = 1, maxLength = 256},
                secret_key = {type = "string", minLength = 1, maxLength = 256},
                algorithm = {
                    type = "string",
                    enum = {"hmac-sha1", "hmac-sha256", "hmac-sha512"},
                    default = "hmac-sha256"
                },
                clock_skew = {
                    type = "integer",
                    default = 300
                },
                signed_headers = {
                    type = "array",
                    items = {
                        type = "string",
                        minLength = 1,
                        maxLength = 20,
                    }
                },
            },
            required = {"access_key", "secret_key"},
            additionalProperties = false,
        },
    },
        {
            title = "work with route or service object",
            properties = {},
            additionalProperties = false,
        }
}


local validator = jsonschema.generate_validator(schema)

local conf = {}
local ok = validator(conf)

if not ok then
  ngx.say("value checking failure")
  return
end

If I change the order of items of the schema object , it will be ok:

local schema = {
    type = "object",
    oneOf = {
        {
            title = "work with route or service object",
            properties = {},
            additionalProperties = false,
        },    
        {
            title = "work with consumer object",
            properties = {
                access_key = {type = "string", minLength = 1, maxLength = 256},
                secret_key = {type = "string", minLength = 1, maxLength = 256},
                algorithm = {
                    type = "string",
                    enum = {"hmac-sha1", "hmac-sha256", "hmac-sha512"},
                    default = "hmac-sha256"
                },
                clock_skew = {
                    type = "integer",
                    default = 300
                },
                signed_headers = {
                    type = "array",
                    items = {
                        type = "string",
                        minLength = 1,
                        maxLength = 20,
                    }
                },
            },
            required = {"access_key", "secret_key"},
            additionalProperties = false,
        },
    },
}


local validator = jsonschema.generate_validator(schema)

local conf = {}
local ok = validator(conf)

if not ok then
  ngx.say("value checking failure")
  return
end

In the wrong case, conf had been change to {"clock_skew": 300}.

I think this is the reason.

nic-chen avatar Sep 17 '20 00:09 nic-chen

it seems to be a bug, let we fix it

membphis avatar Sep 17 '20 06:09 membphis