public function testValidateWithoutCallbackNegative() { $result = V::validate('string', V::number()); $this->assertArrayHasKey('err', $result); $this->assertArrayHasKey('output', $result); $this->assertEquals('value is not a number', (string) $result['err']); $this->assertNull($result['output']); }
public function testAnyStrip() { $input = ['foo' => 'bar', 'baz' => 'quux']; $schema = V::arr()->keys(['foo' => V::string(), 'baz' => V::string()->strip()]); V::validate($input, $schema, function ($err, $output) { $this->assertNull($err); $this->assertEquals(['foo' => 'bar'], $output); }); }
public function testBooleanFalse() { V::validate(false, V::boolean()->false(), function ($err, $output) { $this->assertNull($err); $this->assertFalse($output); }); V::validate(true, V::boolean()->false(), function ($err, $output) { $this->assertEquals('value is not FALSE', $err); $this->assertNull($output); }); }
public function testNumberFloat() { V::validate(1.23, V::number()->float(), function ($err, $output) { $this->assertNull($err); $this->assertEquals(1.23, $output); }); V::validate(123, V::number()->float(), function ($err, $output) { $this->assertEquals('value is not a float', $err); $this->assertNull($output); }); }
public function testObjectInstance() { $instance = new \DateTime(); V::validate($instance, V::object()->instance('\\DateTime'), function ($err, $output) use($instance) { $this->assertNull($err); $this->assertEquals($instance, $output); }); V::validate(new \stdClass(), V::object()->instance('\\DateTime'), function ($err, $output) { $this->assertEquals('object is not an instance of \\DateTime', $err); $this->assertNull($output); }); }
public function testDateConvertToObjectWithoutConversion() { $dateTime = new \DateTime(); V::validate($dateTime, V::date()->dateTimeObject(false), function ($err, $output) use($dateTime) { $this->assertNull($err); $this->assertInstanceOf('\\DateTime', $output); /** @var \DateTime $output */ $this->assertEquals($dateTime->format('Y-m-d'), $output->format('Y-m-d')); }); V::validate('2015-01-01', V::date()->dateTimeObject(false), function ($err, $output) { $this->assertEquals('value is not a DateTime object', $err); $this->assertNull($output); }); }
public function testAlternative() { V::validate('foo', V::alternative()->any(V::string()->valid('foo'), V::boolean()), function ($err, $output) { $this->assertNull($err); $this->assertEquals('foo', $output); }); V::validate(true, V::alternative()->any(V::string()->valid('foo'), V::boolean()), function ($err, $output) { $this->assertNull($err); $this->assertTrue(true, $output); }); V::validate(null, V::alternative()->any(V::string()->valid('foo'), V::boolean()), function ($err, $output) { $this->assertEquals('none of the alternatives matched', $err); $this->assertNull($output); }); }
public function testResourceType() { $fileHandler = fopen(__FILE__, 'r'); V::validate($fileHandler, V::resource(), function ($err, $output) use($fileHandler) { $this->assertNull($err); $this->assertEquals($fileHandler, $output); }); fclose($fileHandler); V::validate('123', V::resource(), function ($err, $output) { $this->assertEquals('value is not a resource', $err); $this->assertNull($output); }); V::validate([], V::resource(), function ($err, $output) { $this->assertEquals('value is not a resource', $err); $this->assertNull($output); }); V::validate(new \stdClass(), V::resource(), function ($err, $output) { $this->assertEquals('value is not a resource', $err); $this->assertNull($output); }); }
public function testClosureType() { $function = function () { return null; }; V::validate($function, V::closure(), function ($err, $output) use($function) { $this->assertNull($err); $this->assertEquals($function, $output); }); V::validate('123', V::closure(), function ($err, $output) { $this->assertEquals('value is not callable', $err); $this->assertNull($output); }); V::validate([], V::closure(), function ($err, $output) { $this->assertEquals('value is not callable', $err); $this->assertNull($output); }); V::validate(new \stdClass(), V::closure(), function ($err, $output) { $this->assertEquals('value is not callable', $err); $this->assertNull($output); }); }
public function testArrayMax() { V::validate([1, 2, 3], V::arr()->max(3), function ($err, $output) { $this->assertNull($err); $this->assertEquals([1, 2, 3], $output); }); V::validate([1, 2, 3], V::arr()->max(4), function ($err, $output) { $this->assertNull($err); $this->assertEquals([1, 2, 3], $output); }); V::validate([1, 2, 3], V::arr()->max(2), function ($err, $output) { $this->assertEquals('array needs to have at most 2 items', $err); $this->assertNull($output); }); }
public function testNumberBetween() { V::validate(0, V::number()->between(5, 10), function ($err, $output) { $this->assertEquals('value must be >= 5', $err); $this->assertNull($output); }); V::validate(4, V::number()->between(5, 10), function ($err, $output) { $this->assertEquals('value must be >= 5', $err); $this->assertNull($output); }); V::validate(5, V::number()->between(5, 10), function ($err, $output) { $this->assertNull($err); $this->assertEquals(5, $output); }); V::validate(7, V::number()->between(5, 10), function ($err, $output) { $this->assertNull($err); $this->assertEquals(7, $output); }); V::validate(10, V::number()->between(5, 10), function ($err, $output) { $this->assertNull($err); $this->assertEquals(10, $output); }); V::validate(11, V::number()->between(5, 10), function ($err, $output) { $this->assertEquals('value must be <= 10', $err); $this->assertNull($output); }); V::validate(100, V::number()->between(5, 10), function ($err, $output) { $this->assertEquals('value must be <= 10', $err); $this->assertNull($output); }); }
/** * handle post after validation if needs * @param $post * @return array|bool */ public function handlePost($post) { if (count($post) < 1) { return false; } $database = new Database(); $invalidFields = []; $fields = []; foreach ($this->getForm()->getFields() as $field) { if ($field->isStorable()) { $field->setValue($post[$field->getName()]); if ($field->getValidation() && Validation::validate($field->getValidation(), $field->getValue())->isValid() != true) { $invalidFields[$field->getName()] = $field->getValue(); } $fields[$field->getName()] = $field->getValue(); } } if (count($invalidFields) > 0 && (!isset($post['delete']) || $post['delete'] != 1)) { return ['status' => false, 'invalidFields' => $invalidFields]; } switch ($this->getMethod()) { case self::_edit: if (isset($post['delete']) && $post['delete'] == 1) { $database->delete($this->getName(), ['ID' => $this->getID()]); $location = $this->getName(); } else { $database->update($this->getName(), $fields, ['ID' => $this->getID()]); $location = $this->getName() . '/' . $this->getMethod() . '/' . $this->getID(); } break; case self::_new: $lastInsertedID = $database->insert($this->getName(), $fields, true); $location = $this->getName() . '/' . self::_edit . '/' . $lastInsertedID; break; } return ['status' => true, 'location' => $location]; }
public function testStringTrimWithoutConversion() { V::validate('foo', V::string()->trim(false), function ($err, $output) { $this->assertNull($err); $this->assertEquals('foo', $output); }); V::validate(' foo', V::string()->trim(false), function ($err, $output) { $this->assertEquals('value is not trimmed', $err); $this->assertNull($output); }); V::validate('foo ', V::string()->trim(false), function ($err, $output) { $this->assertEquals('value is not trimmed', $err); $this->assertNull($output); }); V::validate(' foo ', V::string()->trim(false), function ($err, $output) { $this->assertEquals('value is not trimmed', $err); $this->assertNull($output); }); }
public function testAnyCustomClassMethodNegative() { $obj = new TestAnyCustomClassMethod(); V::validate('string', V::any()->custom([$obj, 'throwException']), function ($err, $output) { $this->assertEquals('A custom validation message', $err); $this->assertNull($output); }); }
public function testStringNotEmpty() { V::validate('foo', V::string()->notEmpty(), function ($err, $output) { $this->assertNull($err); $this->assertEquals('foo', $output); }); V::validate('', V::string()->notEmpty(), function ($err, $output) { $this->assertEquals('value length < 1', $err); $this->assertNull($output); }); }
public function testBetween() { V::validate('2015-05-30', V::date()->between('2015-05-01', '2015-06-01'), function ($err, $output) { $this->assertNotNull($output); $this->assertNull($err); }); V::validate('2015-06-01 06:00:00', V::date()->between('2015-06-01 05:59:59', '2015-06-01 06:00:01'), function ($err, $output) { $this->assertNotNull($output); $this->assertNull($err); }); V::validate('2015-06-01 06:00:00', V::date()->between('2015-06-01 06:00:00', '2015-06-01 07:00:00'), function ($err, $output) { $this->assertNotNull($output); $this->assertNull($err); }); V::validate('2015-06-01 05:00:00', V::date()->between('2015-06-01 06:00:00', '2015-06-01 07:00:00'), function ($err, $output) { $this->assertNull($output); /** @var ValidationException $err */ $this->assertContains('Date should be between', $err->getMessage()); }); }