Skip to content
Snippets Groups Projects
Verified Commit 046cefe4 authored by Lauri Timmanee's avatar Lauri Timmanee
Browse files

Issue #3365480 by ctrlADel, e0ipso, sharkbaitdc, smustgrave, sarahjean,...

Issue #3365480 by ctrlADel, e0ipso, sharkbaitdc, smustgrave, sarahjean, carolpettirossi: [SDC] Improve error handling during prop validation errors

(cherry picked from commit 80174a6c)
parent 05dfdaa2
No related branches found
No related tags found
27 merge requests!11628Update file MediaLibraryWidget.php,!7564Revert "Issue #3364773 by roshnichordiya, Chris Matthews, thakurnishant_06,...,!5752Issue #3275828 by joachim, quietone, bradjones1, Berdir: document the reason...,!5627Issue #3261805: Field not saved when change of 0 on string start,!5427Issue #3338518: send credentials in ajax if configured in CORS settings.,!5395Issue #3387916 by fjgarlin, Spokje: Each GitLab job exposes user email,!5217Issue #3386607 by alexpott: Improve spell checking in commit-code-check.sh,!5064Issue #3379522 by finnsky, Gauravvvv, kostyashupenko, smustgrave, Chi: Revert...,!5040SDC ComponentElement: Transform slots scalar values to #plain_text instead of throwing an exception,!4958Issue #3392147: Whitelist IP for a Ban module.,!4942Issue #3365945: Errors: The following table(s) do not have a primary key: forum_index,!4894Issue #3280279: Add API to allow sites to opt in to upload SVG images in CKEditor 5,!4857Issue #3336994: StringFormatter always displays links to entity even if the user in context does not have access,!4856Issue #3336994: StringFormatter always displays links to entity even if the user in context does not have access,!4788Issue #3272985: RSS Feed header reverts to text/html when cached,!4716Issue #3362929: Improve 400 responses for broken/invalid image style routes,!4553Draft: Issue #2980951: Permission to see own unpublished comments in comment thread,!4273Add UUID to sections,!4192Issue #3367204: [CKEditor5] Missing dependency on drupal.ajax,!3679Issue #115801: Allow password on registration without disabling e-mail verification,!3106Issue #3017548: "Filtered HTML" text format does not support manual teaser break (<!--break-->),!3066Issue #3325175: Deprecate calling \Drupal\menu_link_content\Form\MenuLinkContentForm::_construct() with the $language_manager argument,!3004Issue #2463967: Use .user.ini file for PHP settings,!2851Issue #2264739: Allow multiple field widgets to not use tabledrag,!1484Exposed filters get values from URL when Ajax is on,!925Issue #2339235: Remove taxonomy hard dependency on node module,!872Draft: Issue #3221319: Race condition when creating menu links and editing content deletes menu links
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
use Drupal\sdc\Exception\InvalidComponentException; use Drupal\sdc\Exception\InvalidComponentException;
use Drupal\sdc\Plugin\Component; use Drupal\sdc\Plugin\Component;
use Drupal\sdc\Utilities; use Drupal\sdc\Utilities;
use JsonSchema\Constraints\Constraint;
use JsonSchema\Validator; use JsonSchema\Validator;
/** /**
...@@ -164,7 +165,7 @@ public function validateProps(array $context, Component $component): bool { ...@@ -164,7 +165,7 @@ public function validateProps(array $context, Component $component): bool {
$schema = Validator::arrayToObjectRecursive($schema); $schema = Validator::arrayToObjectRecursive($schema);
$props = Validator::arrayToObjectRecursive($props_raw); $props = Validator::arrayToObjectRecursive($props_raw);
$validator = new Validator(); $validator = new Validator();
$validator->validate($props, $schema); $validator->validate($props, $schema, Constraint::CHECK_MODE_TYPE_CAST);
$validator->getErrors(); $validator->getErrors();
if ($validator->isValid()) { if ($validator->isValid()) {
return TRUE; return TRUE;
...@@ -183,7 +184,17 @@ function (array $error) use ($context): bool { ...@@ -183,7 +184,17 @@ function (array $error) use ($context): bool {
return TRUE; return TRUE;
} }
$message_parts = array_map( $message_parts = array_map(
static fn(array $error): string => sprintf("[%s] %s", $error['property'], $error['message']), static function (array $error): string {
// We check the error message instead of values and definitions here
// because it's hard to access both given the possible complexity of a
// schema. Since this is a small non critical DX improvement error
// message checking should be sufficient.
if (str_contains($error['message'], 'NULL value found, but a ')) {
$error['message'] .= '. This may be because the property is empty instead of having data present. If possible fix the source data, use the |default() twig filter, or update the schema to allow multiple types.';
}
return sprintf("[%s] %s", $error['property'], $error['message']);
},
$errors $errors
); );
$message = implode("/n", $message_parts); $message = implode("/n", $message_parts);
......
$schema: https://git.drupalcode.org/project/sdc/-/raw/1.x/src/metadata.schema.json
name: Array to Object
props:
type: object
properties:
testProp:
title: 'Needs object'
type: object
<div>
{{ testProp }}
</div>
...@@ -37,6 +37,7 @@ public function testRender(): void { ...@@ -37,6 +37,7 @@ public function testRender(): void {
$this->checkIncludeDataMapping(); $this->checkIncludeDataMapping();
$this->checkEmbedWithNested(); $this->checkEmbedWithNested();
$this->checkPropValidation(); $this->checkPropValidation();
$this->checkArrayObjectTypeCast();
$this->checkNonExistingComponent(); $this->checkNonExistingComponent();
$this->checkLibraryOverrides(); $this->checkLibraryOverrides();
$this->checkAttributeMerging(); $this->checkAttributeMerging();
...@@ -171,6 +172,25 @@ protected function checkPropValidation(): void { ...@@ -171,6 +172,25 @@ protected function checkPropValidation(): void {
} }
} }
/**
* Ensure fuzzy coercing of arrays and objects works properly.
*/
protected function checkArrayObjectTypeCast(): void {
$content = ['test' => []];
$build = [
'#type' => 'inline_template',
'#context' => ['content' => $content],
'#template' => "{{ include('sdc_test:array-to-object', { testProp: content.test }, with_context = false) }}",
];
try {
$this->renderComponentRenderArray($build);
$this->addToAssertionCount(1);
}
catch (\Throwable $e) {
$this->fail('Empty array was not converted to object');
}
}
/** /**
* Ensures that including an invalid component creates an error. * Ensures that including an invalid component creates an error.
*/ */
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment