class ValidKeysConstraint
Checks that all the keys of a mapping are valid and required keys present.
Hierarchy
- class \Symfony\Component\Validator\Constraint
- class \Drupal\Core\Validation\Plugin\Validation\Constraint\ValidKeysConstraint extends \Symfony\Component\Validator\Constraint
Expanded class hierarchy of ValidKeysConstraint
File
-
core/
lib/ Drupal/ Core/ Validation/ Plugin/ Validation/ Constraint/ ValidKeysConstraint.php, line 17
Namespace
Drupal\Core\Validation\Plugin\Validation\ConstraintView source
class ValidKeysConstraint extends SymfonyConstraint {
/**
* The error message if a key is invalid.
*
* @var string
*/
public string $invalidKeyMessage = "'@key' is not a supported key.";
/**
* The error message if a key is unknown for the resolved type.
*
* @var string
*/
public string $dynamicInvalidKeyMessage = "'@key' is an unknown key because @dynamic_type_property_path is @dynamic_type_property_value (see config schema type @resolved_dynamic_type).";
/**
* The error message if a key is missing.
*
* @var string
*/
public string $missingRequiredKeyMessage = "'@key' is a required key.";
/**
* The error message if a dynamically required key is missing.
*
* @var string
*/
public string $dynamicMissingRequiredKeyMessage = "'@key' is a required key because @dynamic_type_property_path is @dynamic_type_property_value (see config schema type @resolved_dynamic_type).";
/**
* The error message if the array being validated is a list.
*
* @var string
*/
public string $indexedArrayMessage = 'Numerically indexed arrays are not allowed.';
/**
* Keys which are allowed in the validated array, or `<infer>` to auto-detect.
*
* @var array|string
*/
public array|string $allowedKeys;
/**
* {@inheritdoc}
*/
public function getDefaultOption() : ?string {
return 'allowedKeys';
}
/**
* {@inheritdoc}
*/
public function getRequiredOptions() : array {
return [
'allowedKeys',
];
}
/**
* Returns the list of valid keys.
*
* @param \Symfony\Component\Validator\Context\ExecutionContextInterface $context
* The current execution context.
*
* @return string[]
* The keys that will be considered valid.
*/
public function getAllowedKeys(ExecutionContextInterface $context) : array {
$mapping = $context->getObject();
assert($mapping instanceof Mapping);
$resolved_type = $mapping->getDataDefinition()
->getDataType();
$valid_keys = $mapping->getValidKeys();
// If we were given an explicit array of allowed keys, return that.
if (is_array($this->allowedKeys)) {
if (!empty(array_diff($this->allowedKeys, $valid_keys))) {
throw new InvalidArgumentException(sprintf('The type \'%s\' explicitly specifies the allowed keys (%s), but they are not a subset of the statically defined mapping keys in the schema (%s).', $resolved_type, implode(', ', $this->allowedKeys), implode(', ', $valid_keys)));
}
return array_intersect($valid_keys, $this->allowedKeys);
}
elseif ($this->allowedKeys === '<infer>') {
return $mapping->getValidKeys();
}
throw new InvalidArgumentException("'{$this->allowedKeys}' is not a valid set of allowed keys.");
}
}
Members
Title Sort descending | Modifiers | Object type | Summary | Overriden Title | Overrides |
---|---|---|---|---|---|
Constraint::$groups | public | property | The groups that the constraint belongs to. | ||
Constraint::$payload | public | property | Domain-specific data attached to a constraint. | ||
Constraint::addImplicitGroupName | public | function | Adds the given group if this constraint is in the Default group. | 2 | |
Constraint::CLASS_CONSTRAINT | public | constant | Marks a constraint that can be put onto classes. | ||
Constraint::DEFAULT_GROUP | public | constant | The name of the group given to all constraints with no explicit group. | ||
Constraint::ERROR_NAMES | protected | constant | Maps error codes to the names of their constants. | 59 | |
Constraint::getErrorName | public static | function | Returns the name of the given error code. | ||
Constraint::getTargets | public | function | Returns whether the constraint can be put onto classes, properties or both. |
8 | |
Constraint::normalizeOptions | protected | function | |||
Constraint::PROPERTY_CONSTRAINT | public | constant | Marks a constraint that can be put onto properties. | ||
Constraint::validatedBy | public | function | Returns the name of the class that validates this constraint. | 9 | |
Constraint::__construct | public | function | Initializes the constraint with options. | 58 | |
Constraint::__get | public | function | Returns the value of a lazily initialized option. | 2 | |
Constraint::__isset | public | function | 1 | ||
Constraint::__set | public | function | Sets the value of a lazily initialized option. | 1 | |
Constraint::__sleep | public | function | Optimizes the serialized value to minimize storage space. | ||
ValidKeysConstraint::$allowedKeys | public | property | Keys which are allowed in the validated array, or `<infer>` to auto-detect. | ||
ValidKeysConstraint::$dynamicInvalidKeyMessage | public | property | The error message if a key is unknown for the resolved type. | ||
ValidKeysConstraint::$dynamicMissingRequiredKeyMessage | public | property | The error message if a dynamically required key is missing. | ||
ValidKeysConstraint::$indexedArrayMessage | public | property | The error message if the array being validated is a list. | ||
ValidKeysConstraint::$invalidKeyMessage | public | property | The error message if a key is invalid. | ||
ValidKeysConstraint::$missingRequiredKeyMessage | public | property | The error message if a key is missing. | ||
ValidKeysConstraint::getAllowedKeys | public | function | Returns the list of valid keys. | ||
ValidKeysConstraint::getDefaultOption | public | function | Returns the name of the default option. | Overrides Constraint::getDefaultOption | |
ValidKeysConstraint::getRequiredOptions | public | function | Returns the name of the required options. | Overrides Constraint::getRequiredOptions |