/** * Validate array of items. * @see ItemsConstraint * @override */ public function validate($doc, $context) { $valid = true; if (is_array($doc)) { if (is_array($this->items)) { if ($this->additionalItems === false && sizeof($doc) > sizeof($this->items)) { $valid = new ValidationError($this, "No additional items allowed", $context); } else { foreach ($this->items as $i => $constraint) { if (isset($doc[$i])) { $validation = $constraint->validate($doc[$i], "{$context}{$i}/"); if ($validation instanceof ValidationError) { if ($valid === true) { $valid = new ValidationError($this, "One or more items failed to validate.", $context); } $valid->addChild($validation); if (!$this->continueMode()) { break; } } } } } // If we reach here additionalItems are allowed, but they must pass additionalItems constraint if specified. if ($valid === true && is_object($this->additionalItems)) { for ($i = sizeof($this->items); $i < sizeof($doc); $i++) { $validation = $this->additionalItems->validate($doc[$i], "{$context}{$i}/"); if ($validation instanceof ValidationError) { if ($valid === true) { $valid = new ValidationError($this, "One or more additional items failed validation.", $context); } $valid->addChild($validation); if (!$this->continueMode()) { break; } } } } } else { foreach ($doc as $i => $value) { $validation = $this->items->validate($value, "{$context}{$i}/"); if ($validation instanceof ValidationError) { $valid = new ValidationError($this, "One ore more items failed validation.", $context); $valid->addChild($validation); if (!$this->continueMode()) { break; } } } } } return $valid; }
/** * @override */ public function validate($doc, $context) { $valid = new ValidationError($this, "No constraints passed. At least one required.", $context); foreach ($this->childConstraints as $constraint) { $validation = $constraint->validate($doc, $context); if ($validation instanceof ValidationError) { if ($this->continueMode()) { $valid->addChild($validation); } } else { $valid = true; break; } } return $valid; }
/** * Validate some JSON doc against this symbol. * Although its not clearly stated in the spec, all child constraints must pass. I.e. its an allOf. * @override */ public function validate($doc, $context) { $valid = true; foreach ($this->childConstraints as $constraint) { $validation = $constraint->validate($doc, $context); if ($validation instanceof ValidationError) { if ($valid === true) { $valid = new ValidationError($this, "Not all constraints passed. All required to pass.", $context); } $valid->addChild($validation); if (!$this->continueMode()) { break; } } } return $valid; }
/** * @override */ public function validate($doc, $context) { $valid = true; if (is_object($doc)) { $arrayDoc = (array) $doc; foreach ($arrayDoc as $key => $value) { if (isset($this->dependencies[$key])) { $dependency = $this->dependencies[$key]; if (is_object($dependency)) { $validation = $dependency->validate($doc, $context); // the doc itself not the key.. if ($validation instanceof ValidationError) { if ($valid === true) { $valid = new ValidationError($this, "One or more dependencies unmet.", $context); } $valid->addChild($validation); if (!$this->continueMode()) { break; } } } elseif (is_array($this->dependencies[$key])) { foreach ($dependency as $requiredKey) { if (!isset($arrayDoc[$requiredKey])) { if ($valid === true) { $valid = new ValidationError($this, "One or more dependencies unmet.", $context); } $valid->addChild(new ValidationError($this, "When '{$key}' set '{$requiredKey}' required.", $context)); if (!$this->continueMode()) { break; } } } } } } } return $valid; }
/** * Ensure properties of object match given constraints. * Properties only apply if the property is defined on the target. * @bug Minor. In continueMode first error becomes the parent error, while following errors push onto it. * @override */ public function validate($doc, $context) { $valid = true; if (is_object($doc)) { $arrayDoc = (array) $doc; $seenKeys = []; foreach ($this->properties as $i => $constraint) { if (isset($arrayDoc[$i])) { $validation = $constraint->validate($arrayDoc[$i], "{$context}{$i}/"); $seenKeys[$i] = true; //unset($arrayDoc[$i]); if ($validation instanceof ValidationError) { if ($valid === true) { $valid = new ValidationError($this, "One or more properties failed to validate.", $context); } $valid->addChild($validation); if (!$this->continueMode()) { break; } } } } if ($valid === true || $this->continueMode()) { foreach ($arrayDoc as $docKey => $docItem) { foreach ($this->patternProperties as $pattern => $constraint) { if (preg_match($pattern, $docKey)) { $seenKeys[$docKey] = true; // unset($arrayDoc[$docKey]); $validation = $constraint->validate($docItem, "{$context}{$docKey}/"); if ($validation instanceof ValidationError) { if ($valid === true) { $valid = new ValidationError($this, "One or more pattern properties failed to validate.", $context); } $valid->addChild($validation); if (!$this->continueMode()) { break; } } } } } } // Remove seen keys $arrayDoc = array_diff_key($arrayDoc, $seenKeys); if ($valid === true || $this->continueMode()) { if (is_object($this->additionalProperties)) { foreach ($arrayDoc as $i => $value) { $validation = $this->additionalProperties->validate($arrayDoc[$i], "{$context}{$i}/"); if ($validation instanceof ValidationError) { if ($valid === true) { $valid = new ValidationError($this, "One or more pattern properties failed to validate.", $context); } $valid->addChild($validation); if (!$this->continueMode()) { break; } } } } elseif ($this->additionalProperties === false) { if (sizeof($arrayDoc) > 0) { $additionalPropertiesString = implode(",", array_keys($arrayDoc)); if ($valid === true) { $valid = new ValidationError($this, "Additional properties found ({$additionalPropertiesString}).", $context); } else { $vali->addChild(new ValidationError($this, "Additional properties found ({$additionalPropertiesString}).", $context)); } } } } } return $valid; }