Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do I use not keyword in Lua? #79

Open
howiezhao opened this issue Jun 7, 2022 · 1 comment
Open

How do I use not keyword in Lua? #79

howiezhao opened this issue Jun 7, 2022 · 1 comment

Comments

@howiezhao
Copy link

In jsonschema we have not keyword, but how can I use it in Lua?

For example, We have jsonschema:

"dependencies": {
  "field_2": { "not": { "required": ["field_3"] } },
  "field_3": { "not": { "required": ["field_2"] } }
}

How do we represent it in Lua, I try like below but it doesn't work:

dependencies = {
    field_2 = {
        ["not"] = {
            required = {
                "field_3"
            }
        }
    },
    field_3 = {
        ["not"] = {
            required = {
                "field_2"
            }
        }
    },
}

Can someone help me?

@spacewander
Copy link
Contributor

It works for me:

#!/usr/bin/env resty


local schema = {
    type = "object",
    properties = {
        field_2 = {type = "integer", exclusiveMinimum = 0},
        field_3 = {type = "integer",  exclusiveMinimum = 0},
    },
    dependencies = {
        field_2 = {
            ["not"] = {
                required = {
                    "field_3"
                }
            }
        },
        field_3 = {
            ["not"] = {
                required = {
                    "field_2"
                }
            }
        },
    }
}
local jsonschema = require 'jsonschema'
local myvalidator = jsonschema.generate_validator(schema)
print(myvalidator({field_2 = 1, field_3 = 1}))
print(myvalidator({field_2 = 1}))

Run:

falsefailed to validate dependent schema for "field_2": value wasn't supposed to match schema
true

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants