ComponentMetadataRequirementsChecker::check() should validate that the example(s) actually match the JSON schema

Created on 19 February 2025, about 2 months ago

Overview

Quoting @lauriii from #3501902-65: Adding the Image component results in a state considered invalid :

I'm getting this error when I try to place "Container" element from demo design system. It has an optional image without an example.
AssertionError: assert(\is_array($this->value)) in assert() (line 78 of /var/www/html/web/modules/contrib/experience_builder/src/PropSource/FallbackPropSource.php).

Quoting @longwave from #3501902-66: Adding the Image component results in a state considered invalid :

the problem with the demo Container is that it specifies an image without an example:

    # Don't put examples as this is used with and without images.
    image:
      $ref: json-schema-definitions://experience_builder.module/image
      type: object
      title: Image

but Experience Builder's image schema says the src property is required:

    "image": {
      "title": "image",
      "type":  "object",
      "required": ["src"],
      "properties": {
        "src": {
          "title": "Image URL",
          "$ref": "json-schema-definitions://experience_builder.module/image-uri"
        },

This causes a validation error because when you first add the component the image doesn't have a src. If an example was specified (as in the XB image component) then we use that as the fallback, but there is nothing to fall back to here either.

👆 The root cause:

  1. \Drupal\experience_builder\ComponentMetadataRequirementsChecker::check() only checks for the presence of an examples[0], but it does not verify that it actually complies with the JSON Schema!
  2. … because you'd reasonably expect that JSON Schema validates its examples, but … nope:

    This keyword can be used to provide sample JSON values associated with a particular schema, for the purpose of illustrating usage. It is RECOMMENDED that these values be valid against the associated schema.

    https://json-schema.org/draft/2020-12/draft-bhutton-json-schema-validati... 🧟‍♀️😭

  3. So it's up to either Drupal core's SDC subsystem to strictly validate this (it doesn't), or up to XB.

Conclusion: XB must do this, since for XB it is critical.

(For those of you wondering: — ignoring the fact that the SDC subsystem doesn't use this at all, the same "recommended to be valid" statement is present there: https://json-schema.org/draft/2020-12/draft-bhutton-json-schema-validati... 😬)

Proposed resolution

  1. Prevent Component config entities from being created for SDCs whose examples for SDC props do not validate against the prop's JSON Schema.
  2. Do this by making \Drupal\experience_builder\ComponentMetadataRequirementsChecker::check() more strict.
  3. Add explicit test coverage that detects this.

User interface changes

None.

📌 Task
Status

Active

Version

0.0

Component

Config management

Created by

🇧🇪Belgium wim leers Ghent 🇧🇪🇪🇺

Live updates comments and jobs are added and updated live.
  • Usability

    Makes Drupal easier to use. Preferred over UX, D7UX, etc.

Sign in to follow issues

Merge Requests

Comments & Activities

Production build 0.71.5 2024