Configuration schema & required values: add test coverage for `nullable: true` validation support

Created on 1 June 2023, about 1 year ago
Updated 2 April 2024, about 2 months ago

Problem/Motivation

The following things in Drupal core's config schema are using nullable: true:

  1. entity_reference_selection.default:target_bundles (which is type: sequence) since #1978714: Entity reference doesn't update its field settings when referenced entity bundles are deleted in 2015
  2. editor.editor.*:image_upload.max_dimensions.width and editor.editor.*:image_upload.max_dimensions.height (both of which are type: integer) since #2644838: Add test coverage for the editor's max dimension setting, and fix its message when one dimension is unrestricted in 2016
  3. views.display.page:use_admin_theme (which is type: boolean) since New option for Views page displays to use the admin theme Fixed in 2023.
  4. … and several more since this issue was opened: theme_settings:logo.url, media.settings:iframe_domain and update.settings:fetch.url (since 📌 KernelTestBase::$strictConfigSchema = TRUE and BrowserTestBase::$strictConfigSchema = TRUE do not actually strictly validate Fixed )

It's that first one that introduced nullable: true support in the first place. It also added this to \Drupal\Core\Config\Schema\SchemaCheckTrait::checkValue():

      // Array elements can also opt-in for allowing a NULL value.
      elseif ($element instanceof ArrayElement && $element->isNullable() && $value === NULL) {
        $success = TRUE;
      }

\Drupal\Core\Config\Schema\SchemaCheckTrait::checkValue() already allowed NULL on all primitive types (integer, string, etc.).

However, despite the presence of

    // Add the NotNull constraint for required data.
    if ($definition->isRequired()) {
      $constraints['NotNull'] = [];
    }

in \Drupal\Core\TypedData\TypedDataManager::getDefaultConstraints(), no validation constraint violations are generated when calling

$typed_config->createFromNameAndData($config_name, $config_data)
  ->validate()

This would enable modules like https://www.drupal.org/project/config_split , https://www.drupal.org/project/config_filter et cetera to work more reliably.

Steps to reproduce

See included test coverage.

Proposed resolution

  1. ⚠️ DO NOT CHANGE ANY BEHAVIOR FOR EXISTING CODE/CONFIG SCHEMAS! ⚠️ That is the mistake this issue was making until #46. All the points below apply ONLY to config schema type definitions that have
      constraints:
        FullyValidatable: ~
    

    .

    In this issue, three types get that (the one below, system.menu.* and shortcut.set.*, because these already have validation constraints for everything 📌 Create test that reports % of config entity types (and config schema types) that is validatable Postponed ):

    config_test.types.fully_validatable:
      type: config_test.types
      constraints:
        FullyValidatable: ~
    
  2. Assume nullable: false by default, and make \Drupal\Core\Config\TypedConfigManager::buildDataDefinition() mark the built data definition as required by default, unless a config schema type specifies nullable: true, in which case it will be marked optional.
  3. Add test coverage to prove that when nullable: true is present, setting a value of NULL is considered valid, for all data types. And if it's not set, a value of NULL should trigger a validation error.
  4. Add test coverage to prove that simple config is not affected until they opt in: see SchemaCheckTraitTest.
  5. Add test coverage to prove that config entity types are not affected until they opt in: see \Drupal\KernelTests\Core\Config\ConfigEntityValidationTestBase::testRequiredPropertyValuesMissing()

Consequences

  1. This has zero effect on contributed modules' config, because A) config schema type definitions must opt in, B) config validation does not yet run for them (not in tests, not in production, since 🌱 [meta] Make config schema checking something that can be ignored when testing contrib modules Active .
  2. This has zero effect on core modules' config, because A) config schema type definitions must opt in.
  3. The only way this can affect Drupal core/contrib in production: since Add optional validation constraint support to ConfigFormBase Fixed , simple configuration forms that were updated to not use form-based validation, but config schema-based validation, will be affected, if the simple config they're editing (e.g. update.settings, system.site …) have the FullyValidatable constraint. This issue does not add it to any config schema types in Drupal core. Simple config forms already using do run validation constraints, but since setRequired() will never be called, the NotNull constraint validator will never run, and hence this does not cause any behavioral changes.

Remaining tasks

Review.

User interface changes

None.

API changes

None.

Data model changes

None.

Release notes snippet

N/A

📌 Task
Status

Fixed

Version

11.0 🔥

Component
Configuration 

Last updated 2 days ago

Created by

🇧🇪Belgium Wim Leers Ghent 🇧🇪🇪🇺

Live updates comments and jobs are added and updated live.
  • Contributed project blocker

    It denotes an issue that prevents porting of a contributed project to the stable version of Drupal due to missing APIs, regressions, and so on.

Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.69.0 2024