SDC Component validation should not throw a fatal error if enforce_prop_schemas is false

Created on 4 April 2024, 9 months ago
Updated 18 June 2024, 6 months ago

Problem/Motivation

The fact that a component schema failure throws a fatal error makes theme development more challenging, since the page will not render. This does not happen for module schema. We should be logging errors instead. At the least, this should be configurable.

Steps to reproduce

Install a theme that uses SDC > The Governor is one. https://www.drupal.org/project/governor β†’

It declares that the breadcrumb component uses string titles, which they usually are. But Route titles are TranslatableMarkup. Installing a module that declares such a route then throws a fatal error.

To see the reported issue, you must install a module that exposes a path that produces a breadcrumb trail at least two deep. Installing acquia_dam and visiting https://(yoursite).ddev.site/user/1/acquia-dam is one way to do that.

If you want to see the values being passed to the template for debugging, you need to disable validation.

Do so by returning β€œTRUE” from docroot/core/modules/sdc/src/Twig/TwigExtension::doValidateProps. With that check disabled, you can dump the twig variable and see the data structure.

Proposed resolution

Log an error message instead of throwing a fatal exception.

-or-

Allow developers to configure the above options.

πŸ› Bug report
Status

Closed: works as designed

Version

10.3 ✨

Component
single-directory componentsΒ  β†’

Last updated 2 days ago

Created by

πŸ‡ΊπŸ‡ΈUnited States sarahwylie

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @sarahwylie
  • πŸ‡ΊπŸ‡ΈUnited States agentrickard Georgia (US)
  • πŸ‡ΊπŸ‡ΈUnited States agentrickard Georgia (US)
  • πŸ‡ΊπŸ‡ΈUnited States agentrickard Georgia (US)
  • πŸ‡ΊπŸ‡ΈUnited States agentrickard Georgia (US)

    The problem here, I suspect, goes much deeper. The current implementation of SDC doesn't do any testing against core render components, which can be wildly inconsistent depending on how they are passed to the template.

    So the component assumes that breadcrumb.[0].title is a string, but that is not a safe assumption. How do we make it so?

    Or is the intent of SDC not to render core templates but only custom data that the theme/module completely controls?

  • Status changed to Needs work 9 months ago
  • πŸ‡ΊπŸ‡ΈUnited States agentrickard Georgia (US)

    This may have been corrected when SDC merged to core, since this seems to be new:

        // Dismiss type errors if the prop received a render array.
        $errors = array_filter(
          $validator->getErrors(),
          function (array $error) use ($context): bool {
            if (($error['constraint'] ?? '') !== 'type') {
              return TRUE;
            }
            return !Element::isRenderArray($context[$error['property']] ?? NULL);
          }
        );
        if (empty($errors)) {
          return TRUE;
        }
    
  • πŸ‡ΊπŸ‡ΈUnited States agentrickard Georgia (US)

    Updated title.

    This issue has been fixed -- sort of -- in 10.3. However, the enforce_prop_schemas value in a theme.info.yml file only applies if NO schema is present for the component.

    Individual validation errors are still thrown if one part of the schema is incorrect.

    
        $schema = $component->metadata->schema;
        if (!$schema) {
          if ($component->metadata->mandatorySchemas) {
            throw new InvalidComponentException(sprintf('The component "%s" does not provide schema information. Schema definitions are mandatory for components declared in modules. For components declared in themes, schema definitions are only mandatory if the "enforce_prop_schemas" key is set to "true" in the theme info file.', $component_id));
          }
          return TRUE;
        }
    

    As a developer, I would expect the final exception to be wrapped in a similar statement.

          if ($component->metadata->mandatorySchemas) {
            throw new InvalidComponentException($message);
          }
    
  • Status changed to Active 9 months ago
  • πŸ‡ΊπŸ‡ΈUnited States agentrickard Georgia (US)
  • e0ipso Can Picafort

    enforce_prop_schemas is only used to make sure components in the theme contain a valid schema. It is not meant as an on/off switch for schema validation.

    As I see it, if you don't want schema validation, there are two solutions:

    1. Delete the prop schemas, and set enforce_prop_schemas: false
    2. Keep the invalid schemas, and turn off PHP assertions.

    @agentrickard any chance you can write a note in the SDC documentation on this topic β†’ to avoid other tripping over this? Right now docs are a bit bare bones and could use some love. See screenshot below.

  • πŸ‡ΊπŸ‡ΈUnited States agentrickard Georgia (US)

    Well, except we are trying to use a theme that does have schema validation. The theory makes sense but I'd still love to see a setting that would not trigger a fatal render error.

    I see those as breaking changes (deprecations) that have no advance warning, since they can come from the fact that current theming doesn't enforce strict typing and SDC does.

    It may be that the plan is to correct all that for Drupal 11, but as it stands, SDC may break randomly when modules are installed.

    Note: I will update the docs, too.

  • Status changed to Closed: works as designed 9 months ago
  • πŸ‡ΊπŸ‡ΈUnited States agentrickard Georgia (US)

    We're going to close this as part of the development plan to "catch errors early". See 🌱 [Policy] SDC backwards compatibility policy Active . specifically the note "To determine if this policy is feasible, it would be worth SDC-ifiying some core components that would benefit from better default styles."

    New ticket for the breadcrumb issue: πŸ“Œ SDC: Breadcrumb handling consistency Active

  • πŸ‡ΊπŸ‡ΈUnited States xjm
Production build 0.71.5 2024