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

Breadcrumb

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

function UndefinedConstraint::validateCommonProperties

Validates common properties

Parameters

mixed $value:

mixed $schema:

JsonPointer $path:

string $i:

1 call to UndefinedConstraint::validateCommonProperties()
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 109

Class

UndefinedConstraint
The UndefinedConstraint Constraints

Namespace

JsonSchema\Constraints

Code

protected function validateCommonProperties(&$value, $schema, JsonPointer $path, $i = '') {
    // if it extends another schema, it must pass that schema as well
    if (isset($schema->extends)) {
        if (is_string($schema->extends)) {
            $schema->extends = $this->validateUri($schema, $schema->extends);
        }
        if (is_array($schema->extends)) {
            foreach ($schema->extends as $extends) {
                $this->checkUndefined($value, $extends, $path, $i);
            }
        }
        else {
            $this->checkUndefined($value, $schema->extends, $path, $i);
        }
    }
    // Apply default values from schema
    if (!$path->fromDefault()) {
        $this->applyDefaultValues($value, $schema, $path);
    }
    // Verify required values
    if ($this->getTypeCheck()
        ->isObject($value)) {
        if (!$value instanceof self && isset($schema->required) && is_array($schema->required)) {
            // Draft 4 - Required is an array of strings - e.g. "required": ["foo", ...]
            foreach ($schema->required as $required) {
                if (!$this->getTypeCheck()
                    ->propertyExists($value, $required)) {
                    $this->addError($this->incrementPath($path ?: new JsonPointer(''), $required), 'The property ' . $required . ' is required', 'required');
                }
            }
        }
        elseif (isset($schema->required) && !is_array($schema->required)) {
            // Draft 3 - Required attribute - e.g. "foo": {"type": "string", "required": true}
            if ($schema->required && $value instanceof self) {
                $propertyPaths = $path->getPropertyPaths();
                $propertyName = end($propertyPaths);
                $this->addError($path, 'The property ' . $propertyName . ' is required', 'required');
            }
        }
        else {
            // if the value is both undefined and not required, skip remaining checks
            // in this method which assume an actual, defined instance when validating.
            if ($value instanceof self) {
                return;
            }
        }
    }
    // Verify type
    if (!$value instanceof self) {
        $this->checkType($value, $schema, $path, $i);
    }
    // Verify disallowed items
    if (isset($schema->disallow)) {
        $initErrors = $this->getErrors();
        $typeSchema = new \stdClass();
        $typeSchema->type = $schema->disallow;
        $this->checkType($value, $typeSchema, $path);
        // if no new errors were raised it must be a disallowed value
        if (count($this->getErrors()) == count($initErrors)) {
            $this->addError($path, 'Disallowed value was matched', 'disallow');
        }
        else {
            $this->errors = $initErrors;
        }
    }
    if (isset($schema->not)) {
        $initErrors = $this->getErrors();
        $this->checkUndefined($value, $schema->not, $path, $i);
        // if no new errors were raised then the instance validated against the "not" schema
        if (count($this->getErrors()) == count($initErrors)) {
            $this->addError($path, 'Matched a schema which it should not', 'not');
        }
        else {
            $this->errors = $initErrors;
        }
    }
    // Verify that dependencies are met
    if (isset($schema->dependencies) && $this->getTypeCheck()
        ->isObject($value)) {
        $this->validateDependencies($value, $schema->dependencies, $path);
    }
}

API Navigation

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