You are here

protected static function SecurityAdvisory::validateAdvisoryData in Drupal 9

Validates the security advisory data.

Parameters

mixed[] $data: The advisory data.

Throws

\UnexpectedValueException Thrown if security advisory data is not valid.

1 call to SecurityAdvisory::validateAdvisoryData()
SecurityAdvisory::createFromArray in core/modules/system/src/SecurityAdvisories/SecurityAdvisory.php
Creates a SecurityAdvisories instance from an array.

File

core/modules/system/src/SecurityAdvisories/SecurityAdvisory.php, line 121

Class

SecurityAdvisory
Provides a security advisory value object.

Namespace

Drupal\system\SecurityAdvisories

Code

protected static function validateAdvisoryData(array $data) : void {
  $not_blank_constraints = [
    new Type([
      'type' => 'string',
    ]),
    new NotBlank(),
  ];
  $collection_constraint = new Collection([
    'fields' => [
      'title' => $not_blank_constraints,
      'project' => $not_blank_constraints,
      'type' => $not_blank_constraints,
      'link' => $not_blank_constraints,
      'is_psa' => new Choice([
        'choices' => [
          1,
          '1',
          0,
          '0',
          TRUE,
          FALSE,
        ],
      ]),
      'insecure' => new Type([
        'type' => 'array',
      ]),
    ],
    // Allow unknown fields, in the case that new fields are added to JSON
    // feed validation should still pass.
    'allowExtraFields' => TRUE,
  ]);
  $violations = Validation::createValidator()
    ->validate($data, $collection_constraint);
  if ($violations
    ->count()) {
    foreach ($violations as $violation) {
      $violation_messages[] = "Field " . $violation
        ->getPropertyPath() . ": " . $violation
        ->getMessage();
    }
    throw new \UnexpectedValueException('Malformed security advisory: ' . implode(",\n", $violation_messages));
  }
}