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

patternGroups and patternRequired #16

Open
epoberezkin opened this issue Oct 28, 2016 · 1 comment
Open

patternGroups and patternRequired #16

epoberezkin opened this issue Oct 28, 2016 · 1 comment

Comments

@epoberezkin
Copy link
Member

Originally added as wiki: https://github.com/json-schema/json-schema/wiki/patternGroups-and-patternRequired-(v5-proposal)

Proposed keywords

This proposal would introduce two new keywords:

  • patternGroups and patternRequired

these keywords would complement the existing keyword:

  • patternProperties

Purpose

Currently, schemas can specify a minimum/maximum number of object properties, but they cannot place such constraints on particular groups of properties.

This proposal basically allows us to specify the number of properties that must match a particular pattern.

Values

patternGroups

The value of patternGroups would be an object. The keys of the object would be regular expressions (exactly like the existing patternProperties).

The values inside patternGroups would be objects, containing zero or more of the following properties:

  • minimum - the minimum number of properties in the data that MUST match the corresponding pattern
  • maximum - the maximum number of properties in the data that MUST match the corresponding pattern
  • schema - the schema that matching properties must follow.

minimum/maximum would be non-negative integers, and schema would be a schema.

patternRequired

The value of this keyword should be the array of patterns (to require at least one property matching the pattern)

Behaviour

patternGroups

If the instance is an object, then for every entry in patternGroups:

  1. the set of properties matching that pattern is collected
  2. if minimum or maximum are specified in the patternGroups entry, then the size of this property set must be between these values (inclusive)
  3. if schema is specified in the patternGroups entry, then for every property in the property set, the corresponding object member in the instance must follow that schema.

patternRequired

If the instance is an object, the data to be valid should have at least one property matching each pattern (the same property can match multiple patterns).

Example patternGroups

{
    "type": "object",
    "patternGroups": {
        "^[a-z]+$": {
            "minimum": 1,
            "schema": {"type": "string"}
        },
        "^[0-9]+$": {
            "minimum": 1,
            "schema": {"type": "integer"}
        }
    }
}

Example with patternRequired

{
    "type": "object",
    "patternProperties": {
        "^[a-z]+$": {"type": "string"},
        "^[0-9]+$": {"type": "integer"}
    },
    "patternRequired": ["^[a-z]+$", "^[0-9]+$"]
}

Both these schemas expresses the constraint that instance objects must have at least one alphabetic key, and at least one numeric-key. (This constraint is currently not possible to express).

Additionally, the alphabetic keys must hold strings, and the numeric keys must hold integers.

Valid: {"abc": "foo", "123": 456}
Invalid: {"abc": "foo", "def": "bar"}

For the simple case the syntax of patternRequired is the simpler and consistent with properties/required.

@handrews handrews changed the title v6 validation: patternGroups and patternRequired validation: patternGroups and patternRequired Dec 27, 2016
@handrews handrews changed the title validation: patternGroups and patternRequired patternGroups and patternRequired Sep 28, 2017
@handrews
Copy link
Contributor

Moving keyword proposals that don't have clear momentum to the vocabularies repo.

@handrews handrews transferred this issue from json-schema-org/json-schema-spec Feb 28, 2020
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