3 namespace Drupal\Core\Config\Schema;
5 use Drupal\Core\Config\TypedConfigManagerInterface;
6 use Drupal\Core\TypedData\PrimitiveInterface;
7 use Drupal\Core\TypedData\TraversableTypedDataInterface;
8 use Drupal\Core\TypedData\Type\BooleanInterface;
9 use Drupal\Core\TypedData\Type\StringInterface;
10 use Drupal\Core\TypedData\Type\FloatInterface;
11 use Drupal\Core\TypedData\Type\IntegerInterface;
14 * Provides a trait for checking configuration schema.
16 trait SchemaCheckTrait {
19 * The config schema wrapper object for the configuration object under test.
21 * @var \Drupal\Core\Config\Schema\Element
26 * The configuration object name under test.
30 protected $configName;
33 * Checks the TypedConfigManager has a valid schema for the configuration.
35 * @param \Drupal\Core\Config\TypedConfigManagerInterface $typed_config
36 * The TypedConfigManager.
37 * @param string $config_name
38 * The configuration name.
39 * @param array $config_data
40 * The configuration data, assumed to be data for a top-level config object.
43 * FALSE if no schema found. List of errors if any found. TRUE if fully
46 public function checkConfigSchema(TypedConfigManagerInterface $typed_config, $config_name, $config_data) {
47 // We'd like to verify that the top-level type is either config_base,
48 // config_entity, or a derivative. The only thing we can really test though
49 // is that the schema supports having langcode in it. So add 'langcode' to
50 // the data if it doesn't already exist.
51 if (!isset($config_data['langcode'])) {
52 $config_data['langcode'] = 'en';
54 $this->configName = $config_name;
55 if (!$typed_config->hasConfigSchema($config_name)) {
58 $definition = $typed_config->getDefinition($config_name);
59 $data_definition = $typed_config->buildDataDefinition($definition, $config_data);
60 $this->schema = $typed_config->create($data_definition, $config_data);
62 foreach ($config_data as $key => $value) {
63 $errors = array_merge($errors, $this->checkValue($key, $value));
72 * Helper method to check data type.
75 * A string of configuration key.
80 * List of errors found while checking with the corresponding schema.
82 protected function checkValue($key, $value) {
83 $error_key = $this->configName . ':' . $key;
84 $element = $this->schema->get($key);
85 if ($element instanceof Undefined) {
86 return [$error_key => 'missing schema'];
89 // Do not check value if it is defined to be ignored.
90 if ($element && $element instanceof Ignore) {
94 if ($element && is_scalar($value) || $value === NULL) {
96 $type = gettype($value);
97 if ($element instanceof PrimitiveInterface) {
99 ($type == 'integer' && $element instanceof IntegerInterface) ||
100 // Allow integer values in a float field.
101 (($type == 'double' || $type == 'integer') && $element instanceof FloatInterface) ||
102 ($type == 'boolean' && $element instanceof BooleanInterface) ||
103 ($type == 'string' && $element instanceof StringInterface) ||
104 // Null values are allowed for all primitive types.
107 // Array elements can also opt-in for allowing a NULL value.
108 elseif ($element instanceof ArrayElement && $element->isNullable() && $value === NULL) {
111 $class = get_class($element);
113 return [$error_key => "variable type is $type but applied schema class is $class"];
118 if (!$element instanceof TraversableTypedDataInterface) {
119 $errors[$error_key] = 'non-scalar value but not defined as an array (such as mapping or sequence)';
122 // Go on processing so we can get errors on all levels. Any non-scalar
123 // value must be an array so cast to an array.
124 if (!is_array($value)) {
125 $value = (array) $value;
127 // Recurse into any nested keys.
128 foreach ($value as $nested_value_key => $nested_value) {
129 $errors = array_merge($errors, $this->checkValue($key . '.' . $nested_value_key, $nested_value));