/** * @param $value * @throws ResourceValidationException */ public function validate($value) { $messages = new MessageCollection(); foreach ($this as $validator) { /** @var Validator $validator */ try { $validator->validate($value); } catch (ValidatorValidationException $e) { $messages->add($e->getValidator()->getErrorMessage($e)); } } if (count($messages) > 0) { throw ResourceValidationException::make($messages); } }
/** * @param $value * @param Property $property * @throws PropertyValidationException */ public function validate(Property $property, $value) { $messages = new MessageCollection(); foreach ($this as $requirement) { /** @var Requirement $requirement */ try { $requirement->validate($property, $value); } catch (RequirementValidationException $e) { $messages->add($e->getRequirement()->getErrorMessage($e)); } } if (count($messages) > 0) { throw PropertyValidationException::make($property, $messages); } }
/** * @param string $path * @return mixed * @throws ResourceValidationException */ public function validate(string $path = '') { $messages = new MessageCollection(); foreach ($this->getResourceDefinition()->getFields() as $field) { /** @var ResourceField $field */ $value = $this->properties->getProperty($field); try { if (!isset($value)) { $field->validate(null, $path); } else { $value->validate($path); } } catch (PropertyValidationException $e) { $messages->merge($e->getMessages()); } } // Also check all resource wide requirements try { $this->getResourceDefinition()->getValidators()->validate($this); } catch (ResourceValidationException $e) { $messages->merge($e->getMessages()); } if (count($messages) > 0) { throw ResourceValidationException::make($messages); } }
/** * @param string $path * @throws PropertyValidationException * @throws ResourceException * @throws \CatLab\Requirements\Exceptions\ResourceValidationException */ public function validate(string $path) { $messages = new MessageCollection(); $field = $this->getField(); if (!$field instanceof RelationshipField) { throw new ResourceException("RelationshipValue found without a RelationshipField."); } if ($field->canCreateNewChildren()) { /* * Now check the children */ foreach ($this->getChildrenToProcess() as $child) { /** @var RESTResource $child */ if ($child) { try { $child->validate($this->appendToPath($path, $field)); } catch (ResourceValidationException $e) { $messages->merge($e->getMessages()); } } else { $this->getField()->validate(null, $this->appendToPath($path, $field)); } } } elseif ($field->canLinkExistingEntities()) { /* * We only need identifiers... */ $identifiers = $field->getChildResource()->getFields()->getIdentifiers(); foreach ($this->getChildrenToProcess() as $child) { if ($child) { /** @var RESTResource $child */ foreach ($identifiers as $identifier) { /** @var IdentifierField $identifier */ $prop = $child->getProperties()->getProperty($identifier); if (!$prop || $prop->getValue() === null) { $identifier->setPath($this->appendToPath($path, $field)); $propertyException = RequirementValidationException::make($identifier, new Exists(), null); $messages = new MessageCollection(); $messages->add($propertyException->getRequirement()->getErrorMessage($propertyException)); throw PropertyValidationException::make($identifier, $messages); } } } else { try { $this->getField()->validate(null, $this->appendToPath($path, $field)); } catch (ResourceValidationException $e) { $messages->merge($e->getMessages()); } } } } if (count($messages) > 0) { throw PropertyValidationException::make($field, $messages); } }