PluginProbe ʕ •ᴥ•ʔ
Secure Custom Fields / trunk
Secure Custom Fields vtrunk
6.9.1 6.9.0 6.8.9 6.8.7 6.8.8 6.8.6 6.8.4 6.8.5 trunk 6.4.0-beta1 6.4.0-beta2 6.4.1 6.4.1-beta3 6.4.1-beta4 6.4.1-beta5 6.4.1-beta6 6.4.1-beta7 6.4.2 6.5.0 6.5.1 6.5.2 6.5.3 6.5.4 6.5.5 6.5.6 6.5.7 6.6.0 6.7.0 6.7.1 6.8.0 6.8.1 6.8.2 6.8.3
secure-custom-fields / vendor / justinrainbow / json-schema / src / JsonSchema / Constraints / SchemaConstraint.php
secure-custom-fields / vendor / justinrainbow / json-schema / src / JsonSchema / Constraints Last commit date
TypeCheck 8 months ago BaseConstraint.php 8 months ago CollectionConstraint.php 8 months ago Constraint.php 8 months ago ConstraintInterface.php 8 months ago EnumConstraint.php 8 months ago Factory.php 8 months ago FormatConstraint.php 8 months ago NumberConstraint.php 8 months ago ObjectConstraint.php 8 months ago SchemaConstraint.php 8 months ago StringConstraint.php 8 months ago TypeConstraint.php 8 months ago UndefinedConstraint.php 8 months ago
SchemaConstraint.php
95 lines
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 <seroscho@googlemail.com>
22 * @author Bruno Prieto Reis <bruno.p.reis@gmail.com>
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 }
95