Overview
In HEAD:
default_value:
# TRICKY: this MUST be a sequence, to allow for a multi-value default for `type: array` prop shapes.
type: sequence
label: 'Default values'
sequence:
type: field.value.[%parent.%parent.field_type]
# @todo Add validation constraint because this is only *conditionally* nullable: if the SDC prop is optional
nullable: true
This issue is about adding that missing validation.
Proposed resolution
Take inspiration from:
- XB's
KeyForEverySdcPropvalidation constraint - core's
NotNullvalidation constraint
⇒
- Add a new
NotNullValueForEveryRequiredSdcPropvalidation constraint. That should be able to follow the lead of the logic forKeyForEverySdcProp's validator. However, that uses\Drupal\experience_builder\PropShape\PropShape::getComponentProps(), which only looks at shapes, not required vs optional. To do that, use
assert($component_plugin instanceof Drupal\Core\Plugin\Component); $component_schema = $component_plugin->metadata->schema ?? []; $required_props = $component_schema['required'] ?? [];For each of
$required_props, a key must exist in the sequence, and it must have a not-null value. That's the validation logic we need 😊 - Add new
ComponentValidationTest::testInvalidWidgetSettings()test that does
$settings = $this->entity->getSettings(); assert($settings['prop_field_definitions']['text']['default_value'] !== NULL); $settings['prop_field_definitions']['text']['default_value'] = NULL; $this->entity->setSettings($settings); $this->assertValidationErrors(['settings.prop_field_definitions.text.default_value' => 'a meaningfulerror message']);
User interface changes
None.
Issue fork experience_builder-3526707
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
wim leersComment #3
wim leersComment #4
libbna commentedI will give it a try!
Comment #6
libbna commentedI've implemented a new validation constraint NotNullValueForEveryRequiredSdcProp to ensure that all required props defined in a component's schema have non-null entries in the default_value mapping. This is based on the required array from the component plugin's metadata schema. The logic closely follows the pattern used in KeyForEverySdcProp.
I've tried to achieve the requirement as described—please let me know if anything is missed or needs adjustment.
Comment #7
libbna commentedHi @wim leers to test my code I followed the below steps:
Let me know if this is correct or have I missed anything?Thanks.
Comment #8
wim leersThis is not yet passing tests. 😇 Left a review with pointers.
Comment #9
libbna commentedI resolved the phpstan issue by adding " // @phpstan-ignore argument.type" comment by taking reference from "KeyForEverySdcProp" validator file. Please review and let me know if anymore changes are required.
Unassinging myself so that someone else can work for test cases and keeping the issue to needs work only.
Comment #10
libbna commentedComment #12
thoward216 commentedComment #13
thoward216 commentedComment #14
wim leersThanks, both of you, looks great! 🤩
Comment #16
wim leers