WordPress.org

Make WordPress Core

Opened 5 weeks ago

Last modified 11 days ago

#51025 new enhancement

Add support for the anyOf and oneOf JSON Schema keywords.

Reported by: TimothyBlynJacobs Owned by:
Milestone: 5.6 Priority: normal
Severity: normal Version:
Component: REST API Keywords: needs-patch needs-unit-tests json-schema
Focuses: Cc:

Description

These would be particularly helpful for the Image Editor Batch API: https://github.com/WordPress/gutenberg/pull/23369

Reference: https://tools.ietf.org/html/draft-fge-json-schema-validation-00#section-5.5.4

One of the tricky things is how to handle error messages. For an example schema:

{
  "type": "array",
  "items": {
    "oneOf": [
      {
        "type": "object",
        "properties": {
          "operation": {
            "type": "string",
            "enum": [
              "crop"
            ]
          },
          "x": {
            "type": "integer"
          },
          "y": {
            "type": "integer"
          }
        }
      },
      {
        "type": "object",
        "properties": {
          "operation": {
            "type": "string",
            "enum": [
              "rotate"
            ]
          },
          "degrees": {
            "type": "integer",
            "minimum": 0,
            "maximum": 360
          }
        }
      }
    ]
  }
}

If the user submitted this data:

[
{
  "operation": "rotate",
  "degrees": 450
}
]

You want to be able to give an error like "degrees must be greater than 0 and less than 360". But the most simple validation approach would give an error like "did not match against exactly one schema".

Change History (2)

#1 @TimothyBlynJacobs
5 weeks ago

For schemas like the example that have a property in common that is used to disambiguate, we could solve this by introducing a keyword that would signify that property. But that would really only work for these switch type schemas.

A more generic approach might be to do something like this:

  1. Discard schemas that don't match the type.
  2. For objects, find the schema that has the most properties in common.
  3. Find the schema that has the most keywords that validate.

We can also mandate that each schema in the oneOf array has a title set. That way we can at least give an error message like "x is not a valid Rotation or Crop".

This ticket was mentioned in Slack in #core-restapi by mdmag. View the logs.


11 days ago

Note: See TracTickets for help on using tickets.