/**
  * @dataProvider negativeTestsProvider
  */
 public function testNegative($data, $test)
 {
     print "Negative test [{$test}] is run\n";
     $validator = new Validator\LIVR($data['rules']);
     $output = $validator->validate($data['input']);
     $this->assertFalse($output ? true : false, 'Validator should return false');
     $this->assertEquals($data['errors'], $validator->getErrors(), 'Validator should contain valid errors');
 }
 public function assertLIVR($data, $livr, $params = array())
 {
     $validator = new \Validator\LIVR($livr);
     $validated = $validator->validate($data);
     $errors = $validator->getErrors();
     if ($errors) {
         throw new \Exception("Failed asserting by LIVR rules. " . (count($params) ? "Params: " . print_r($params, 1) : '') . "Errors: " . print_r($errors, 1) . "data: " . print_r($data, 1));
     }
     return $validated;
 }
 public function testNegative()
 {
     print "NEGATIVE: Validate data with automatic trim\n";
     $input = array('code' => '   ', 'password' => ' 12  ', 'address' => array('street' => '  hell '));
     $expectedErrors = array('code' => 'REQUIRED', 'password' => 'TOO_SHORT', 'address' => array('street' => 'TOO_SHORT'));
     $validator = new Validator\LIVR($this->rules, true);
     $output = $validator->validate($input);
     if ($output) {
         throw new \Exception('Should contain error codes');
     }
     $this->assertEquals($expectedErrors, $validator->getErrors());
 }
Example #4
0
 public static function validate($data, $livr)
 {
     \Validator\LIVR::registerDefaultRules(['latin_string' => function () {
         return function ($value) {
             if (!isset($value) || $value === '') {
                 return;
             }
             if (!is_string($value)) {
                 return 'FORMAT_ERROR';
             }
             $validStringReg = '/^[a-zA-Z0-9\\-\\_\\+\\#№"\']+$/';
             if (!preg_match($validStringReg, $value)) {
                 return 'WRONG_STRING';
             }
             return;
         };
     }, 'phone' => function () {
         return function ($value) {
             if (!isset($value) || $value === '') {
                 return;
             }
             if (!is_string($value)) {
                 return 'FORMAT_ERROR';
             }
             $reg = '/^[0-9]{10}$/';
             if (!preg_match($reg, $value)) {
                 return 'WRONG_FORMAT';
             }
             return;
         };
     }]);
     $validator = new \Validator\LIVR($livr);
     $validated = $validator->validate($data);
     $errors = $validator->getErrors();
     if ($errors) {
         throw new X(['Type' => 'FORMAT_ERROR', 'Fields' => $errors]);
     }
     return $validated;
 }
Example #5
0
 public static function listOfObjects($livr, $ruleBuilders)
 {
     $validator = new \Validator\LIVR($livr);
     $validator->registerRules($ruleBuilders)->prepare();
     return function ($objects, $params, &$outputArr) use($validator) {
         if (!isset($objects) || $objects === '') {
             return;
         }
         if (!is_array($objects) || \Validator\LIVR\Util::isAssocArray($objects)) {
             return 'FORMAT_ERROR';
         }
         $results = array();
         $errors = array();
         $hasErrors = false;
         foreach ($objects as $object) {
             $result = $validator->validate($object);
             if ($result) {
                 $errors[] = null;
                 $results[] = $result;
             } else {
                 $hasErrors = true;
                 $errors[] = $validator->getErrors();
                 $results[] = null;
             }
         }
         if ($hasErrors) {
             return $errors;
         } else {
             $outputArr = $results;
             return;
         }
     };
 }
Example #6
0
 private function buildAliasedRule($alias)
 {
     if (!$alias['name']) {
         throw new \Exception("Alias name required");
     }
     if (!$alias['rules']) {
         throw new \Exception("Alias rules required");
     }
     return function ($ruleBuilders) use($alias) {
         $validator = new \Validator\LIVR(array('value' => $alias['rules']));
         $validator->registerRules($ruleBuilders)->prepare();
         return function ($value, $params, &$outputArr) use($validator, $alias) {
             $result = $validator->validate(array('value' => $value));
             if ($result) {
                 $outputArr = $result['value'];
                 return;
             } else {
                 $errors = $validator->getErrors();
                 return isset($alias['error']) ? $alias['error'] : $errors['value'];
             }
         };
     };
 }