Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. UndefinedConstraint.php

function UndefinedConstraint::validateOfProperties

Validate allOf, anyOf, and oneOf properties

Parameters

mixed $value:

mixed $schema:

JsonPointer $path:

string $i:

1 call to UndefinedConstraint::validateOfProperties()
UndefinedConstraint::check in vendor/justinrainbow/json-schema/src/JsonSchema/Constraints/UndefinedConstraint.php
invokes the validation of an element

File

vendor/justinrainbow/json-schema/src/JsonSchema/Constraints/UndefinedConstraint.php, line 308

Class

UndefinedConstraint
The UndefinedConstraint Constraints

Namespace

JsonSchema\Constraints

Code

protected function validateOfProperties(&$value, $schema, JsonPointer $path, $i = '') {
    // Verify type
    if ($value instanceof self) {
        return;
    }
    if (isset($schema->allOf)) {
        $isValid = true;
        foreach ($schema->allOf as $allOf) {
            $initErrors = $this->getErrors();
            $this->checkUndefined($value, $allOf, $path, $i);
            $isValid = $isValid && count($this->getErrors()) == count($initErrors);
        }
        if (!$isValid) {
            $this->addError($path, 'Failed to match all schemas', 'allOf');
        }
    }
    if (isset($schema->anyOf)) {
        $isValid = false;
        $startErrors = $this->getErrors();
        $caughtException = null;
        foreach ($schema->anyOf as $anyOf) {
            $initErrors = $this->getErrors();
            try {
                $this->checkUndefined($value, $anyOf, $path, $i);
                if ($isValid = count($this->getErrors()) == count($initErrors)) {
                    break;
                }
            } catch (ValidationException $e) {
                $isValid = false;
            }
        }
        if (!$isValid) {
            $this->addError($path, 'Failed to match at least one schema', 'anyOf');
        }
        else {
            $this->errors = $startErrors;
        }
    }
    if (isset($schema->oneOf)) {
        $allErrors = array();
        $matchedSchemas = 0;
        $startErrors = $this->getErrors();
        foreach ($schema->oneOf as $oneOf) {
            try {
                $this->errors = array();
                $this->checkUndefined($value, $oneOf, $path, $i);
                if (count($this->getErrors()) == 0) {
                    $matchedSchemas++;
                }
                $allErrors = array_merge($allErrors, array_values($this->getErrors()));
            } catch (ValidationException $e) {
                // deliberately do nothing here - validation failed, but we want to check
                // other schema options in the OneOf field.
            }
        }
        if ($matchedSchemas !== 1) {
            $this->addErrors(array_merge($allErrors, $startErrors));
            $this->addError($path, 'Failed to match exactly one schema', 'oneOf');
        }
        else {
            $this->errors = $startErrors;
        }
    }
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal