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 KeyForEverySdcProp validation constraint
  • core's NotNull validation constraint

  1. Add a new NotNullValueForEveryRequiredSdcProp validation constraint. That should be able to follow the lead of the logic for KeyForEverySdcProp'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 😊

  2. 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.

Command icon 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

wim leers created an issue. See original summary.

wim leers’s picture

Title: Tighten validation of `experience_builder.generated_field_explicit_input_ux: prop_field_definitions` » Tighten validation of `experience_builder.generated_field_explicit_input_ux: prop_field_definitions.[%key].default_value`
libbna’s picture

Assigned: Unassigned » libbna

I will give it a try!

libbna’s picture

Status: Active » Needs work

I'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.

libbna’s picture

Hi @wim leers to test my code I followed the below steps:

  1. edit the existing component and remove the default text from the required prop
  2. saved the component

Let me know if this is correct or have I missed anything?Thanks.

wim leers’s picture

This is not yet passing tests. 😇 Left a review with pointers.

libbna’s picture

I 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.

libbna’s picture

Assigned: libbna » Unassigned

thoward216 made their first commit to this issue’s fork.

thoward216’s picture

Assigned: Unassigned » thoward216
thoward216’s picture

Assigned: thoward216 » Unassigned
Status: Needs work » Needs review
wim leers’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: -Needs tests

Thanks, both of you, looks great! 🤩

  • wim leers committed d83e67ff on 1.x authored by libbna
    Issue #3526707 by wim leers, libbna, thoward216: Tighten validation of `...
wim leers’s picture

Version: 0.x-dev » 1.x-dev
Status: Reviewed & tested by the community » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.