コード例 #1
0
 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);
     });
 }
コード例 #2
0
 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);
     });
 }
コード例 #3
0
 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);
     });
 }
コード例 #4
0
 /**
  * @param array $options
  */
 public function setOptions(array $options)
 {
     if ($schema = $this->getOptionsSchema()) {
         try {
             $this->options = V::attempt($options, $schema);
         } catch (ValidationException $e) {
             throw new \InvalidArgumentException($e);
         }
     } else {
         $this->options = $options;
     }
 }
コード例 #5
0
 /**
  * @param InputValue $input
  * @throws ValidationException
  */
 public function process(InputValue $input)
 {
     foreach ($this->getOption('options') as $schema) {
         try {
             $input->replace(function ($value) use($schema) {
                 return V::attempt($value, $schema);
             });
             return;
         } catch (ValidationException $e) {
         }
     }
     throw new ValidationException('none of the alternatives matched');
 }
コード例 #6
0
 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);
     });
 }
コード例 #7
0
 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);
     });
 }
コード例 #8
0
 /**
  * @param InputValue $input
  * @param $key
  * @param AbstractSchema $schema
  * @throws ValidationException
  */
 private function validateAndReplaceKey(InputValue $input, $key, AbstractSchema $schema)
 {
     try {
         $input->replace(function ($value) use($key, $schema) {
             $value[$key] = V::attempt($value[$key], $schema);
             return $value;
         });
         if ($schema->getOption('strip')) {
             $input->replace(function ($value) use($key) {
                 unset($value[$key]);
                 return $value;
             });
         }
     } catch (ValidationException $e) {
         throw new ValidationException(sprintf('key "%s" is invalid, because [ %s ]', $key, $e->getMessage()));
     }
 }
コード例 #9
0
 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);
     });
 }
コード例 #10
0
 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);
     });
 }
コード例 #11
0
 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);
     });
 }
コード例 #12
0
 /**
  * @return AbstractSchema
  */
 protected function getOptionsSchema()
 {
     return V::arr()->keys(['of' => V::string()->min(1)]);
 }
コード例 #13
0
 public function testAnyDefaultPreset()
 {
     $input = ['firstname' => 'Smok', 'lastname' => 'Wawelski', 'username' => 'foobar', 'created' => '2015-01-01', 'status' => 'foo'];
     $schema = V::arr()->keys(['username' => V::string()->defaultValue(function ($context) {
         return strtolower($context['firstname']) . '-' . strtolower($context['lastname']);
     }), 'firstname' => V::string(), 'lastname' => V::string(), 'created' => V::date()->dateTimeObject()->defaultValue(new \DateTime()), 'status' => V::string()->defaultValue('registered')]);
     $user = V::attempt($input, $schema);
     $this->assertEquals('foobar', $user['username']);
     $this->assertEquals('Smok', $user['firstname']);
     $this->assertEquals('Wawelski', $user['lastname']);
     $this->assertEquals('2015-01-01 00:00:00', $user['created']->format('Y-m-d H:i:s'));
     $this->assertEquals('foo', $user['status']);
 }
コード例 #14
0
ファイル: app.php プロジェクト: PierreCharles/uFramework-PHP
    $data['confirm'] = $request->getParameter('confirm');
    $data['captcha'] = $request->getParameter('captcha');
    $data['error'] = Validation::validationRegisterForm($data['user'], $data['password'], $data['confirm'], $data['captcha']);
    if ($data['error']['nb'] > 0) {
        return $app->render('register.php', $data);
    }
    $userMapper->persist(new User(null, $data['user'], password_hash($data['password'], PASSWORD_DEFAULT)));
    return $app->redirect('/login');
});
// Matches if the HTTP method is PUT -> /
$app->put('/', function () use($app) {
    return $app->render('index.php');
});
// Matches if the HTTP method is DELETE -> /statuses/id
$app->delete('/statuses/(\\d+)', function (Request $request, $id) use($app, $statusFinder, $statusMapper) {
    if (!Validation::isInt($id)) {
        $response = new Response("Incorrect id parameter", 400);
        $response->send();
        return;
    }
    if (null == $statusFinder->findOneById($id)) {
        throw new HttpException(404, 'Status not Found');
    }
    $statusMapper->remove($id);
    return $app->redirect('/statuses');
});
// Firewall
$app->addListener('process.before', function (Request $req) use($app) {
    session_start();
    $allowed = ['/login' => [Request::GET, Request::POST], '/statuses' => [Request::GET, Request::POST], '/statuses/' => [Request::GET, Request::POST], '/register' => [Request::GET, Request::POST], '/' => [Request::GET]];
    if (isset($_SESSION['is_connected']) && true === $_SESSION['is_connected']) {
コード例 #15
0
 /**
  * @return AbstractSchema
  */
 public function getOptionsSchema()
 {
     return V::arr()->keys(['pattern' => V::string()->min(1), 'replace' => V::string()]);
 }
コード例 #16
0
 public function testAssertNegative()
 {
     $this->setExpectedException('\\Validation\\ValidationException');
     V::assert('string', V::number());
 }
コード例 #17
0
 /**
  * @return AbstractSchema
  */
 protected function getOptionsSchema()
 {
     return Validation::arr()->keys(['pattern' => Validation::string()]);
 }
コード例 #18
0
ファイル: rom.php プロジェクト: neel/bong
 public function validate($conf)
 {
     \Validation\Validation::parse($conf, $this->items());
 }
コード例 #19
0
 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);
     });
 }
コード例 #20
0
 /**
  * @return ArraySchema
  */
 protected function getOptionsSchema()
 {
     return V::arr()->keys(['callback' => V::closure()]);
 }
コード例 #21
0
 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);
     });
 }
コード例 #22
0
 protected function getOptionsSchema()
 {
     return V::arr()->keys(['time' => V::date()->dateTimeObject()]);
 }
コード例 #23
0
ファイル: Module.php プロジェクト: dev-learning/project
 /**
  * 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];
 }
コード例 #24
0
 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);
     });
 }
コード例 #25
0
 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());
     });
 }
コード例 #26
0
 /**
  * @return AbstractSchema
  */
 protected function getOptionsSchema()
 {
     return V::arr()->keys(['convert' => V::boolean()]);
 }
コード例 #27
0
 public function testStringTrimInvalidOption()
 {
     $this->setExpectedException('InvalidArgumentException', 'key "convert" is invalid, because [ value is not a boolean ]');
     V::assert('foo', V::string()->trim('foo'));
 }
コード例 #28
0
 /**
  * @return AbstractSchema
  */
 public function getOptionsSchema()
 {
     return V::arr()->keys(['search' => V::alternative(V::string()->min(1), V::arr()->notEmpty()), 'replace' => V::alternative(V::string(), V::arr()->notEmpty())]);
 }
コード例 #29
0
 /**
  * @return AbstractSchema
  */
 protected function getOptionsSchema()
 {
     return Validation::arr()->keys(['disallowed' => Validation::arr()->notEmpty()]);
 }