Validation error on optional properties.

Created on 23 June 2025, 5 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

  • Issue created by @alt.dev
  • πŸ‡³πŸ‡ΏNew Zealand quietone

    If this problem was discovered on a version of Drupal that is not 11.x, add that information in the issue summary and leave the version at 11.x. In Drupal core changes are made on on 11.x (our main development branch) first, and are then back ported as needed according to the Core change policies β†’ . Also mentioned on the version β†’ section of the list of issue fields documentation.

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

    Added the fix for the problem. A test for the case is needed.

  • Pipeline finished with Failed
    5 months ago
    Total: 789s
    #531266
  • πŸ‡¨πŸ‡¦Canada nedjo

    Thanks for reporting this issue. While the patch adds null as a valid type for top-level non-required properties, those properties may themselves have child properties that are not yet handled.

  • πŸ‡¨πŸ‡¦Canada nedjo

    Instead of adding null as a type, can we instead skip validation for properties that are both optional and null?

  • πŸ‡ΊπŸ‡¦Ukraine Foxy-vikvik

    Works fine with Drupal 10

  • Assigned to phenaproxima
  • Status changed to Needs work 2 months ago
  • Pipeline finished with Failed
    2 months ago
    #597170
  • Pipeline finished with Failed
    2 months ago
    #597178
  • Pipeline finished with Running
    2 months ago
    #597249
  • Pipeline finished with Failed
    2 months ago
    #597287
  • Pipeline finished with Failed
    27 days ago
    Total: 4165s
    #630000
  • Pipeline finished with Failed
    27 days ago
    Total: 816s
    #630105
  • Pipeline finished with Failed
    26 days ago
    #630407
  • Pipeline finished with Success
    26 days ago
    Total: 1093s
    #630419
  • Pipeline finished with Failed
    14 days ago
    Total: 900s
    #643440
Production build 0.71.5 2024