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

custom vocabulary: keywords useful for arrays of objects #28

Open
karenetheridge opened this issue Jun 3, 2020 · 3 comments
Open

custom vocabulary: keywords useful for arrays of objects #28

karenetheridge opened this issue Jun 3, 2020 · 3 comments

Comments

@karenetheridge
Copy link
Owner

see json-schema-org/json-schema-vocabularies#22 -- when the instance data is an array of objects, it would be useful to have the keywords isUniqueByProperty: <propertyname> and isSortedByProperty: <propertyname>.

@karenetheridge
Copy link
Owner Author

karenetheridge commented Jun 3, 2020

Semantics:

  • these keywords are ignored (treated as if they are not present) unless the instance data validates against {"type":"array", "items": {"type": "object" } }.
  • isUniqueByProperty MUST have a value of string. It evaluates to true if the property named by the keyword either does not exist in each item member's object, or appears exactly once across all items. If any property value of the indicated name appears more than once across all the items, evaluation is false.
  • isSortedByProperty MUST have a value of string. It evaluates to true if the property named by the keyword either does not exist in each item member's object, or appears in each item in sorted order. ("in order" is considered to be normal string sorting (insert spec?) if the properties are of string type, or numeric if the properties are of number type. Behaviour is undefined if the named property is not a consistent type across all items, or is any type other than "string" or "number". If the indicated property is seen more than once across the items, the sort order between those items is indeterminate (so any ordering would be considered to be valid), so it may be desirable to combine with "isUniqueByProperty".

Since both keywords can act on object items even when the property is not present, and the outcome is indeterminant if the property types vary, it is recommended to include extra validation with the "required", "properties" and "type" keywords for the property of interest.

Example:

{
  "type": "array",
  "items": {
    "type": "object",
    "required": [ "name" ],
    "properties": {
      "name": { "type": "string" },
      ... other properties...
    },
  },
  "isUniqueByProperty": "name",
  "isSortedByProperty": "name",
}

@karenetheridge
Copy link
Owner Author

TBD: maybe both these keywords could also take a list of properties?

  • isUniqueByProperty: the tuple obtained by all the properties is used as the uniqueness criteria
  • isSortedByProperty: the items must be sorted by considering the first property first, then the second, and so on.

@iilei
Copy link

iilei commented Nov 16, 2020

For better flexibility I would consider using jsonpath expressions.

The Example above then would become something like

{
  ...
  "uniquenessProvidedBy": ".name",
  "sortingProvidedBy": ".name",
}

whilst allowing for conjunction keys etc.

TBD: maybe both these keywords could also take a list of properties?

That would also allow for lists:

{
  ...
  "uniquenessProvidedBy": ".[name,key,id]",
  "sortingProvidedBy": ".[name,key,id]",
}

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

2 participants