Skip to content

Keywords minContains/maxContains #441

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

Closed
epoberezkin opened this issue Oct 14, 2017 · 6 comments
Closed

Keywords minContains/maxContains #441

epoberezkin opened this issue Oct 14, 2017 · 6 comments

Comments

@epoberezkin
Copy link
Member

These keywords will complement the existing keyword contains to define minimum and/or maximum number of items that are valid against the schema in contains keyword (in the same schema object).

Example:

{
  "$comment": "validate that the array contains >= 2 and <= 4 boolean true values",
  "type": "array",
  "items": {"type": "boolean"},
  "contains": {"const": true},
  "minContains": 2,
  "maxContains": 4
}

Meta-schema:

{
  "properties": {
    "minContains": {"type": "number", "minimum": 0, "default": 1},
    "maxContains": {"type": "number"}
  },
  "dependencies": {
    "minContains": ["contains"],
    "maxContains": ["contains"]
  }
}

"minContains": 0 would allow limiting the maximum number of matching elements without requiring one matching element.

@handrews handrews added this to the draft-future milestone Oct 14, 2017
@handrews handrews changed the title [proposal] Keywords minContains/maxContains Keywords minContains/maxContains Oct 18, 2017
@handrews
Copy link
Contributor

Removed the [proposal] prefix as I've been removing all of them since we have control over the labels now. Proposals are labeled with "Enhancement".

@handrews
Copy link
Contributor

@handrews
Copy link
Contributor

This proposal seems pretty reasonable to me. Any objections? I'm going to tentatively put it in the draft-08 milestone. Anyone who feels like submitting a PR is welcome to, otherwise I'll get around to it eventually.

@mchastain-turner
Copy link

It seems that minContains and maxContains could allow for schema definitions that would have a count validator. Otherwise you would have to do an allOf which seems like it would be unwieldy.

{
  "$comment": "validate that the array has at least one headline and multiple paragraphs",
  "type": "array",
  "contains": {"const": true},
  "minContains": [{"const": "headline", "count":1}, {"const":"paragraph", "count":1}],
  "maxContains": [{"const": "headline", "count":1}]
}

@Relequestual
Copy link
Member

I feel allOf would be a more natural way to express the constraint you describe.
Given you can already do this, and it isn't exactly a "shorthand", I don't see any reason to change what's already there to what you describe.

@mchastain-turner
Copy link

It does seem odd though that minContains and maxContains are applied as constraints to contains. It seems like you could define the constraints directly in the contains schemas:

{
  "type": "array",
  "contains": [{"const": "onlyOne", "minCount": 1, "maxCount":1}, {"const": "atLeastOne", "minCount":1}]
}

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

No branches or pull requests

4 participants