Validation error on optional properties.

Created on 23 June 2025, 3 months ago

Problem/Motivation

When we pass the null value to the optional property, we receive the following error
NULL value found, but a number or an object is required. This may be because the property is empty instead of having data present.

But we shouldn't receive this kind of error since null is the expected value for the optional field. The fix for it is to add a "null" type to the component, but I guess this should be done by default.

Steps to reproduce

1. Run the composer require justinrainbow/json-schema command. We need a validator so that the \Drupal\Core\Theme\Component\ComponentValidator::validateProps can run its logic.

2. Create a component and add an optional property to it, for example

$schema: https://git.drupalcode.org/project/sdc/-/raw/1.x/src/metadata.schema.json
version: 1.0
name: Error
status: stable
props:
  properties:
    text:
      type: string
      title: Text

3. Render new component:

    $build = [
      '#type' => 'component',
      '#component' => 'module:component_name',
      '#props' => [
        'text' => NULL,
      ]
    ];

Proposed resolution

Automatically add null type to all non-require poperties during the validation.

πŸ› Bug report
Status

Active

Version

10.2 ✨

Component

single-directory components

Created by

πŸ‡ΊπŸ‡¦Ukraine alt.dev

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

Merge Requests

Comments & Activities

Production build 0.71.5 2024