function ComponentValidator::validateClassProps
Same name in this branch
- 11.x core/lib/Drupal/Core/Theme/Component/ComponentValidator.php \Drupal\Core\Theme\Component\ComponentValidator::validateClassProps()
Same name in other branches
- 10 core/modules/sdc/src/Component/ComponentValidator.php \Drupal\sdc\Component\ComponentValidator::validateClassProps()
- 10 core/lib/Drupal/Core/Theme/Component/ComponentValidator.php \Drupal\Core\Theme\Component\ComponentValidator::validateClassProps()
Validates the props that are not JSON Schema.
This validates that the props are instances of the class/interface declared in the metadata file.
Parameters
array $props_schema: The schema for all the props in the component.
array $props_raw: The props provided to the component.
string $component_id: The component ID. Used for error reporting.
Return value
array A tuple containing the new $props_schema and the new $props_raw. We mutate the schema to be `type: null` and the prop value to be `NULL` for the props that are validated as class objects. This is done so these props can pass validation later on when validating against the JSON Schema. We can do this because we have already validated these props manually.
Throws
\Drupal\sdc\Exception\InvalidComponentException
1 call to ComponentValidator::validateClassProps()
- ComponentValidator::validateProps in core/
modules/ sdc/ src/ Component/ ComponentValidator.php - Validates that the props provided to the component.
File
-
core/
modules/ sdc/ src/ Component/ ComponentValidator.php, line 231
Class
- ComponentValidator
- Validates a component based on its definition and the component schema.
Namespace
Drupal\sdc\ComponentCode
private function validateClassProps(array $props_schema, array $props_raw, string $component_id) : array {
$error_messages = [];
$classes_per_prop = $this->getClassProps($props_schema);
$properties = $props_schema['properties'] ?? [];
foreach ($properties as $prop_name => $prop_def) {
$class_types = $classes_per_prop[$prop_name] ?? [];
$prop = $props_raw[$prop_name] ?? NULL;
if (empty($class_types) || is_null($prop)) {
continue;
}
$is_valid = array_reduce($class_types, static fn(bool $valid, string $class_name) => $valid || $prop instanceof $class_name, FALSE);
if (!$is_valid) {
$error_messages[] = sprintf('Data provided to prop "%s" for component "%s" is not a valid instance of "%s"', $prop_name, $component_id, implode(', ', $class_types));
}
// Remove the non JSON Schema types for later JSON Schema validation.
$props_raw[$prop_name] = NULL;
}
$props_schema = $this->nullifyClassPropsSchema($props_schema, $classes_per_prop);
if (!empty($error_messages)) {
$message = implode("/n", $error_messages);
throw new InvalidComponentException($message);
}
return [
$props_schema,
$props_raw,
];
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.