[SDC] Improve error handling during prop validation errors

Created on 7 June 2023, over 1 year ago
Updated 17 July 2024, 5 months ago

Problem/Motivation

The JSON serialization process is unable to know when to serialize PHP's empty arrays as {} or as [] in JSON. This is a known gotcha when serializing to JSON. This can cause validation errors during SDC's prop schema validation. In particular, if a prop expects an object (associative array in PHP) it will receive an empty JSON array, and complain about the mismatched types.

This produces the following exception (abbreviated for legibility):

The website encountered an unexpected error. Please try again later.

Drupal\sdc\Exception\InvalidComponentException: [ellipses] Array value found, but an object is required in Drupal\sdc\Component\ComponentValidator->validateProps() (line 190 of core/modules/sdc/src/Component/ComponentValidator.php).
Drupal\sdc\Twig\TwigExtension->doValidateProps(Array, 'olivero:pager') (Line: 112)
Drupal\sdc\Twig\TwigExtension->validateProps(Array, 'olivero:pager') (Line: 42)
__TwigTemplate_1dbebacf17837daaff99a96584e759fe->doDisplay(Array, Array) (Line: 394)
Twig\Template->displayWithErrorHandling(Array, Array) (Line: 367)
Twig\Template->display(Array, Array) (Line: 168)
__TwigTemplate_1e024ae630f0bcbb43d72ffc1f96a3db___424038220->doDisplay(Array, Array) (Line: 394)
Twig\Template->displayWithErrorHandling(Array, Array) (Line: 367)
Twig\Template->display(Array) (Line: 40)
__TwigTemplate_1e024ae630f0bcbb43d72ffc1f96a3db->doDisplay(Array, Array) (Line: 394)
Twig\Template->displayWithErrorHandling(Array, Array) (Line: 367)
Twig\Template->display(Array) (Line: 379)
Twig\Template->render(Array, Array) (Line: 40)
Twig\TemplateWrapper->render(Array) (Line: 53)
twig_render_template('core/themes/olivero/templates/navigation/pager.html.twig', Array) (Line: 372)
Drupal\Core\Theme\ThemeManager->render('pager', Array) (Line: 436)

Steps to reproduce

  1. Install a component like the one in the tests for this MR core/modules/sdc/tests/modules/sdc_test/components/array-to-object
  2. Render the component in a page, like done in the test for this MR core/modules/sdc/tests/src/Kernel/ComponentRenderTest::checkArrayObjectTypeCast

Without this patch, the exception above is produced. As proved by the test-only patch. With the full fix, tests come back green.

Proposed resolution

Let's use fuzzy validation, as supported by the validation library of our choice. This will solve the array to object issue.

Additionally, this MR adds better error messages when the validator correctly complains about NULL values for other types. This is a very common error that has come up during our DX research cases. Adding inline suggestion fixes will improve developer experience significantly for these cases.

๐Ÿ› Bug report
Status

Fixed

Version

10.1 โœจ

Component
Renderย  โ†’

Last updated 3 days ago

Created by

๐Ÿ‡บ๐Ÿ‡ธUnited States ctrladel North Carolina, USA

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

Comments & Activities

Production build 0.71.5 2024