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

False positive requireValidation check with minProperties #149

Open
andylizi opened this issue Oct 14, 2021 · 3 comments
Open

False positive requireValidation check with minProperties #149

andylizi opened this issue Oct 14, 2021 · 3 comments

Comments

@andylizi
Copy link

Description

{
    "type": "object",
    "minProperties": 1,
    "properties": {
        "phoneNumber": { "type": "string" },
        "emailAddress": { "type": "string" }
    },
    "additionalProperties": false
}

This schema will fail with [requireValidation] if properties is used, required should be specified.

The requirement in this case is not necessary because minProperties is specified and additionalProperties is set to false, so at least one of phoneNumber and emailAddress is already required without explicitly using the required keyword.

Related code

if (node.properties && !node.required) enforceValidation('if properties is used, required')

@ChALkeR
Copy link
Contributor

ChALkeR commented Oct 17, 2021

The purpose of that check is to ensure that the schema author does not forget to specify required when it's needed, which is a common mistake, e.g.

{
  "type": "object",
  "properties": {
      "x": { "type": "number" },
      "y": { "type": "number" }
  }
}

If the intent is to not have any properties required, explicitly stating required: [] in the schema will silence this check.

@ChALkeR
Copy link
Contributor

ChALkeR commented Oct 17, 2021

In the additionalProperties: false + properties + minProperties case, this behavior does seem to be intentional (for the schema) though, so perhaps it's ok to skip required check.

I wonder if this could introduce schema errors though vs the benefits of not writing require: [] in such cases.

I'll look closer into that.

@andylizi
Copy link
Author

Personally I can't think of any situations where minProperties + additionalProperties: false behavior can be unintentional. The alternative way of doing this would be to use anyOf + required, which is much more verbose, has oblique error messages and presumably compiles to longer code. Numbers larger than one can't be expressed in this way either.

The workaround of require: [] is also not immediately obvious and confusing to future readers. My thoughts after submitting this issue were, "Oh, I guess I just have to disable this check in the mean time then". And since requireValidation can't be trivially overrided in strong mode, I'm sure some people will be lazy enough to disable stronge mode entirely (!).

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