public function testWrongParameterDepth() { $this->s->str('1_1')->struct('1_3', (new SB())->str('2_1')->struct('2_2', (new SB())->str('3_1')->struct('3_2', (new SB())->struct('4_1', (new SB())->bool('5_1')->int('5_2'))))); $input = ['1_3' => ['2_2' => ['3_2' => ['4_1' => ['5_1' => 'Not boolean', '5_2' => 'Not integer']]]]]; $badPaths = ['1_3.2_2.3_2.4_1.5_1', '1_3.2_2.3_2.4_1.5_2']; $badFields = []; try { $this->v->validate($input); } catch (InvalidStructureException $e) { $badFields = $e->getBadFields(); } foreach ($badPaths as $v) { $this->assertTrue(isset($badFields[$v]), 'Field [' . $v . '] should be missing'); } }
/** * Check if a certain parameter is valid * @param string $name * @param mixed $value * @throws InvalidParameterTypeException * @throws InvalidStructureException * @throws UnexpectedParameterException */ public function validateParam($name, $value) { $item = $this->structure->getItemByName($name); if ($item === null) { throw new UnexpectedParameterException(); } if ($item instanceof StructureItem) { // Recursive validation $validator = new self($item->structure, $this->parameterDepthStack); $validator->validate($value); } else { if (!$item->validate($value)) { $e = new InvalidParameterTypeException(); $e->expectedItem = $item; throw $e; } } }
public function testEmptyArrayIsValid() { $this->s->int('numbers')->multiple(); $input = ['numbers' => []]; $this->assertTrue($this->v->validate($input), 'Empty array is a valid type'); }