From 6a161b9baacf59f897c1296ca3709ff7ade7e3b4 Mon Sep 17 00:00:00 2001
From: Alex Pott <alex.a.pott@googlemail.com>
Date: Tue, 13 May 2025 15:38:53 +0100
Subject: [PATCH] Issue #3516348 by bbrala, longwave, andypost, larowlan,
 jackfoust, mlubbers: Allow 6.x version of justinrainbow/json-schema

---
 composer.json                                            | 2 +-
 composer.lock                                            | 2 +-
 composer/Metapackage/DevDependencies/composer.json       | 2 +-
 .../Drupal/Core/Theme/Component/ComponentValidator.php   | 4 +++-
 .../Core/Theme/Component/ComponentValidatorTest.php      | 9 ++++++++-
 5 files changed, 14 insertions(+), 5 deletions(-)

diff --git a/composer.json b/composer.json
index 490263451f73..cdb213f557c0 100644
--- a/composer.json
+++ b/composer.json
@@ -20,7 +20,7 @@
         "colinodell/psr-testlogger": "^1.2",
         "composer/composer": "^2.8.1",
         "drupal/coder": "^8.3.10",
-        "justinrainbow/json-schema": "^5.2",
+        "justinrainbow/json-schema": "^5.2 || ^6.3",
         "lullabot/mink-selenium2-driver": "^1.7",
         "lullabot/php-webdriver": "^2.0.4",
         "mglaman/phpstan-drupal": "^1.2.12",
diff --git a/composer.lock b/composer.lock
index 1e7ff31add09..98ed514e5e5c 100644
--- a/composer.lock
+++ b/composer.lock
@@ -4,7 +4,7 @@
         "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
         "This file is @generated automatically"
     ],
-    "content-hash": "38510ee3fa6d0ddb39c85b7172e095fc",
+    "content-hash": "4c1d292a8a8199682b920af13dd37ab2",
     "packages": [
         {
             "name": "asm89/stack-cors",
diff --git a/composer/Metapackage/DevDependencies/composer.json b/composer/Metapackage/DevDependencies/composer.json
index 2d102e1a9771..76d83d826ded 100644
--- a/composer/Metapackage/DevDependencies/composer.json
+++ b/composer/Metapackage/DevDependencies/composer.json
@@ -12,7 +12,7 @@
         "colinodell/psr-testlogger": "^1.2",
         "composer/composer": "^2.8.1",
         "drupal/coder": "^8.3.10",
-        "justinrainbow/json-schema": "^5.2",
+        "justinrainbow/json-schema": "^5.2 || ^6.3",
         "lullabot/mink-selenium2-driver": "^1.7",
         "lullabot/php-webdriver": "^2.0.4",
         "mglaman/phpstan-drupal": "^1.2.12",
diff --git a/core/lib/Drupal/Core/Theme/Component/ComponentValidator.php b/core/lib/Drupal/Core/Theme/Component/ComponentValidator.php
index 82a9f93ff2c8..d747b1354c84 100644
--- a/core/lib/Drupal/Core/Theme/Component/ComponentValidator.php
+++ b/core/lib/Drupal/Core/Theme/Component/ComponentValidator.php
@@ -177,7 +177,9 @@ public function validateProps(array $context, Component $component): bool {
     $errors = array_filter(
       $this->validator->getErrors(),
       function (array $error) use ($context): bool {
-        if (($error['constraint'] ?? '') !== 'type') {
+        // Support 5.0 ($error['constraint']) and 6.0
+        // ($error['constraint']['name']) at the same time.
+        if (($error['constraint']['name'] ?? $error['constraint'] ?? '') !== 'type') {
           return TRUE;
         }
         return !Element::isRenderArray($context[$error['property']] ?? NULL);
diff --git a/core/tests/Drupal/Tests/Core/Theme/Component/ComponentValidatorTest.php b/core/tests/Drupal/Tests/Core/Theme/Component/ComponentValidatorTest.php
index 1d52d4fb5a26..792f38603a8a 100644
--- a/core/tests/Drupal/Tests/Core/Theme/Component/ComponentValidatorTest.php
+++ b/core/tests/Drupal/Tests/Core/Theme/Component/ComponentValidatorTest.php
@@ -9,6 +9,7 @@
 use Drupal\Core\Theme\Component\ComponentValidator;
 use Drupal\Core\Render\Component\Exception\InvalidComponentException;
 use Drupal\Core\Plugin\Component;
+use JsonSchema\ConstraintError;
 use JsonSchema\Constraints\Factory;
 use JsonSchema\Constraints\FormatConstraint;
 use JsonSchema\Entity\JsonPointer;
@@ -279,7 +280,13 @@ public function check(&$element, $schema = NULL, ?JsonPointer $path = NULL, $i =
     }
     if ($schema->format === 'uri') {
       if (\is_string($element) && !UrlHelper::isValid($element)) {
-        $this->addError($path, 'Invalid URL format', 'format', ['format' => $schema->format]);
+        if (class_exists(ConstraintError::class)) {
+          $this->addError(ConstraintError::FORMAT_URL(), $path, ['format' => $schema->format]);
+        }
+        else {
+          // @todo Remove when we no longer support justinrainbow/json-schema v5.
+          $this->addError($path, 'Invalid URL format', 'format', ['format' => $schema->format]);
+        }
       }
       return;
     }
-- 
GitLab