Use NotNull constraint for nullable config schema validation

Created on 16 May 2020, over 4 years ago
Updated 2 April 2024, 10 months ago

Problem/Motivation

There are two different ways that a nullable property of a configuration type can be defined, one that uses a custom property name and custom code to evaluate it and one that uses the standard Symfony NotNull constraint.

The first approach uses a boolean 'nullable' property. Example:

# Schema for the entity reference 'default' selection handler settings.
entity_reference_selection.default:
  type: entity_reference_selection
  label: 'Default selection handler settings'
  mapping:
    target_bundles:
      type: sequence
      label: 'types'
      nullable: true

This property is set if the item is optional; if not set, the item is required. If the 'nullable' property is used, validation is done on a custom basis in \Drupal\Core\Config\Schema\SchemaCheckTrait::checkValue().

Second, by adding NotNull to an array of constraints. Example:

system.site:
  type: config_object
  label: 'Site information'
  mapping:
    uuid:
      type: uuid
      label: 'Site UUID'
      constraints:
        Uuid: []
        NotNull: []

If NotNull is used, the item is required, while if its absent it is presumed to be optional (though, in fact, is required because of the handling of 'nullable'). Validation for NotNull is done via a Symfony constraint.

Proposed resolution

Probably most consistent to standardize on NotNull constraint, but doing so would require changes to almost all configuration items, as the default (not nullable) would now require an explicit NotNull constraint.

Alternately, remove the NotNull constraint on UUID in system.site and, maybe, switch to applying NotNull constraint if 'nullable' property is empty in place of current custom validation code.

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

πŸ“Œ Task
Status

Closed: duplicate

Version

11.0 πŸ”₯

Component
ConfigurationΒ  β†’

Last updated about 17 hours ago

Created by

πŸ‡¨πŸ‡¦Canada nedjo

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

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.71.5 2024