| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* This file is part of the JsonSchema package. |
| 5 |
* |
| 6 |
* For the full copyright and license information, please view the LICENSE |
| 7 |
* file that was distributed with this source code. |
| 8 |
*/ |
| 9 |
|
| 10 |
namespace JsonSchema\Constraints; |
| 11 |
|
| 12 |
use JsonSchema\Entity\JsonPointer; |
| 13 |
use JsonSchema\Exception\InvalidArgumentException; |
| 14 |
use JsonSchema\Exception\InvalidSchemaException; |
| 15 |
use JsonSchema\Exception\RuntimeException; |
| 16 |
use JsonSchema\Validator; |
| 17 |
|
| 18 |
/** |
| 19 |
* The SchemaConstraint Constraints, validates an element against a given schema |
| 20 |
* |
| 21 |
* @author Robert Schönthal <[email protected]> |
| 22 |
* @author Bruno Prieto Reis <[email protected]> |
| 23 |
*/ |
| 24 |
class SchemaConstraint extends Constraint |
| 25 |
{ |
| 26 |
const DEFAULT_SCHEMA_SPEC = 'http://json-schema.org/draft-04/schema#'; |
| 27 |
|
| 28 |
/** |
| 29 |
* {@inheritdoc} |
| 30 |
*/ |
| 31 |
public function check(&$element, $schema = null, ?JsonPointer $path = null, $i = null) |
| 32 |
{ |
| 33 |
if ($schema !== null) { |
| 34 |
// passed schema |
| 35 |
$validationSchema = $schema; |
| 36 |
} elseif ($this->getTypeCheck()->propertyExists($element, $this->inlineSchemaProperty)) { |
| 37 |
// inline schema |
| 38 |
$validationSchema = $this->getTypeCheck()->propertyGet($element, $this->inlineSchemaProperty); |
| 39 |
} else { |
| 40 |
throw new InvalidArgumentException('no schema found to verify against'); |
| 41 |
} |
| 42 |
|
| 43 |
// cast array schemas to object |
| 44 |
if (is_array($validationSchema)) { |
| 45 |
$validationSchema = BaseConstraint::arrayToObjectRecursive($validationSchema); |
| 46 |
} |
| 47 |
|
| 48 |
// validate schema against whatever is defined in $validationSchema->$schema. If no |
| 49 |
// schema is defined, assume self::DEFAULT_SCHEMA_SPEC (currently draft-04). |
| 50 |
if ($this->factory->getConfig(self::CHECK_MODE_VALIDATE_SCHEMA)) { |
| 51 |
if (!$this->getTypeCheck()->isObject($validationSchema)) { |
| 52 |
throw new RuntimeException('Cannot validate the schema of a non-object'); |
| 53 |
} |
| 54 |
if ($this->getTypeCheck()->propertyExists($validationSchema, '$schema')) { |
| 55 |
$schemaSpec = $this->getTypeCheck()->propertyGet($validationSchema, '$schema'); |
| 56 |
} else { |
| 57 |
$schemaSpec = self::DEFAULT_SCHEMA_SPEC; |
| 58 |
} |
| 59 |
|
| 60 |
// get the spec schema |
| 61 |
$schemaStorage = $this->factory->getSchemaStorage(); |
| 62 |
if (!$this->getTypeCheck()->isObject($schemaSpec)) { |
| 63 |
$schemaSpec = $schemaStorage->getSchema($schemaSpec); |
| 64 |
} |
| 65 |
|
| 66 |
// save error count, config & subtract CHECK_MODE_VALIDATE_SCHEMA |
| 67 |
$initialErrorCount = $this->numErrors(); |
| 68 |
$initialConfig = $this->factory->getConfig(); |
| 69 |
$initialContext = $this->factory->getErrorContext(); |
| 70 |
$this->factory->removeConfig(self::CHECK_MODE_VALIDATE_SCHEMA | self::CHECK_MODE_APPLY_DEFAULTS); |
| 71 |
$this->factory->addConfig(self::CHECK_MODE_TYPE_CAST); |
| 72 |
$this->factory->setErrorContext(Validator::ERROR_SCHEMA_VALIDATION); |
| 73 |
|
| 74 |
// validate schema |
| 75 |
try { |
| 76 |
$this->check($validationSchema, $schemaSpec); |
| 77 |
} catch (\Exception $e) { |
| 78 |
if ($this->factory->getConfig(self::CHECK_MODE_EXCEPTIONS)) { |
| 79 |
throw new InvalidSchemaException('Schema did not pass validation', 0, $e); |
| 80 |
} |
| 81 |
} |
| 82 |
if ($this->numErrors() > $initialErrorCount) { |
| 83 |
$this->addError($path, 'Schema is not valid', 'schema'); |
| 84 |
} |
| 85 |
|
| 86 |
// restore the initial config |
| 87 |
$this->factory->setConfig($initialConfig); |
| 88 |
$this->factory->setErrorContext($initialContext); |
| 89 |
} |
| 90 |
|
| 91 |
// validate element against $validationSchema |
| 92 |
$this->checkUndefined($element, $validationSchema, $path, $i); |
| 93 |
} |
| 94 |
} |