/** * Validates a given user value and checks whether it passes basic sanity checking, such as types. * * @param $userValues The value provided by the user * * @return bool TRUE if the validation passes * @throws \Exception If validation fails */ public function validate($userValues) { // Check inputted type if (!$this->hasCorrectType($userValues)) { throw new \Exception(sprintf('The key provided "%s" has the wrong value type. You provided %s but was expecting %s', $this->name, print_r($userValues, true), $this->type)); } if ($this->isArray()) { foreach ($userValues as $userValue) { $this->itemSchema->validate($userValue); } } elseif ($this->isObject()) { foreach ($userValues as $key => $userValue) { // Check that nested keys are properly defined, but permit arbitrary structures if it's metadata $property = $this->getNestedProperty($key); $property->validate($userValue); } } return true; }
private function validateArray($userValues) { foreach ($userValues as $userValue) { $this->itemSchema->validate($userValue); } }
public function test_it_passes_validation_when_all_subproperties_pass() { $params = ['type' => 'object', 'properties' => ['foo' => ['type' => 'string']]]; $userVals = ['foo' => 'baz']; $param = new Parameter($params); $this->assertTrue($param->validate($userVals)); }
/** * @expectedException \Exception */ public function test_exception_is_thrown_when_value_is_not_in_enum_list() { $data = $this->data; $data['enum'] = ['foo']; $param = new Parameter($data); $param->validate('blah'); }