public function testAssertAndValidatorOfArray()
 {
     $validator = validator()->arrayable(['name' => 'string|length_min:5']);
     static::assertInvalidValueException(function () use($validator) {
         $validator->assert(new ArrayObject(['name' => '1234']));
     }, ['length_min:5@name']);
 }
Example #2
0
 public function testArrayWithMultiEmptyArray()
 {
     $validator = validator()->array(['username' => 'required|reg_exp:/^[a-zA-Z0-9_-]{4,30}$/', 'name' => 'length_max:16', 'profile' => validator()->pipeline(['required', ['image' => 'required|length_max:64', 'alt' => 'string']]), 'driver' => ['company' => 'required|length_max:16', 'description' => 'string'], 'company' => ['registration' => 'required|length_max:16', 'description' => 'string']]);
     // all empty
     $validator->assert(null);
     $validator->assert('');
     // only array
     $validator->assert(['username' => 'wan2land', 'profile' => ['image' => 'static/images/000.png']]);
     $this->assertInvalidValueException(function () use($validator) {
         $validator->assert([]);
     }, ['required@username', 'required@profile']);
     $this->assertInvalidValueException(function () use($validator) {
         $validator->assert(['username' => 'wan2land', 'profile' => null]);
     }, ['required@profile']);
     $this->assertInvalidValueException(function () use($validator) {
         $validator->assert(['username' => 'wan2land', 'profile' => []]);
     }, ['*****@*****.**']);
     // with driver
     $validator->assert(['username' => 'wan2land', 'profile' => ['image' => 'static/images/000.png'], 'driver' => null]);
     $validator->assert(['username' => 'wan2land', 'profile' => ['image' => 'static/images/000.png'], 'driver' => '']);
     $validator->assert(['username' => 'wan2land', 'profile' => ['image' => 'static/images/000.png'], 'driver' => ['company' => 'something']]);
     $this->assertInvalidValueException(function () use($validator) {
         $validator->assert(['username' => 'wan2land', 'profile' => ['image' => 'static/images/000.png'], 'driver' => []]);
     }, ['*****@*****.**']);
     // with company
     $validator->assert(['username' => 'wan2land', 'profile' => ['image' => 'static/images/000.png'], 'company' => null]);
     $validator->assert(['username' => 'wan2land', 'profile' => ['image' => 'static/images/000.png'], 'company' => '']);
     $validator->assert(['username' => 'wan2land', 'profile' => ['image' => 'static/images/000.png'], 'company' => ['registration' => '000-1234-1234']]);
     $this->assertInvalidValueException(function () use($validator) {
         $validator->assert(['username' => 'wan2land', 'profile' => ['image' => 'static/images/000.png'], 'company' => []]);
     }, ['*****@*****.**']);
 }
Example #3
0
 public function testAssertAndValidatorOfArray()
 {
     $validator = validator()->object(['name' => 'string|length_min:5']);
     $this->assertInvalidValueException(function () use($validator) {
         $validator->assert((object) ['name' => '1234']);
     }, ['length_min:5@name']);
 }
Example #4
0
 public function testNextValidate()
 {
     $validator = validator()->not(validator()->integer());
     $this->assertFalse($validator->validate(0));
     $this->assertFalse($validator->validate(111));
     $this->assertTrue($validator->validate('1'));
     $this->assertTrue($validator->validate(false));
 }
Example #5
0
 public function testEmailWithOtherValidation()
 {
     $this->assertNotSame(validator()->email(new DNSCheckValidation()), validator()->email(new DNSCheckValidation()));
     validator()->email(new DNSCheckValidation())->assert('*****@*****.**');
     $this->assertInvalidValueException(function () {
         validator()->email(new DNSCheckValidation())->assert('*****@*****.**');
     }, ['email']);
 }
Example #6
0
 public function testRegExpHasComma()
 {
     $validator = validator()->from("reg_exp:/^\\d{3,5}\$/");
     $validator->assert("100");
     $validator->assert("1000");
     $validator->assert("10000");
     $this->assertInvalidValueException(function () use($validator) {
         $validator->assert("10");
     }, ['reg_exp:/^\\d{3,5}$/']);
     $this->assertInvalidValueException(function () use($validator) {
         $validator->assert('100000');
     }, ['reg_exp:/^\\d{3,5}$/']);
 }
 public function testEloquentCollection()
 {
     $validator = validator()->arrayable(['name' => 'string', 'children' => validator()->collection(validator()->arrayable(['name' => 'required|string']))]);
     $user = new CollectionTestUser();
     $children = new Collection([new CollectionTestUser(['name' => 'alex']), new CollectionTestUser(['name' => 'lily'])]);
     $user->setRelation('children', $children);
     $validator->assert($user);
     static::assertInvalidValueException(function () use($validator) {
         $user = new CollectionTestUser();
         $children = new Collection([new CollectionTestUser(['name' => 3030]), new CollectionTestUser([])]);
         $user->setRelation('children', $children);
         $validator->assert($user);
     }, ['*****@*****.**', '*****@*****.**']);
 }
 public function testScalars()
 {
     validator()->printable()->assert('30');
     validator()->printable()->assert(30);
     validator()->printable()->assert(40.5);
     validator()->printable()->assert('string');
     validator()->printable()->assert('string');
     validator()->printable()->assert(new TestPrintableValidator());
     $this->assertInvalidValueException(function () {
         validator()->printable()->assert([]);
     }, ['printable']);
     $this->assertInvalidValueException(function () {
         validator()->printable()->assert(new \stdClass());
     }, ['printable']);
 }
 public function testWithObject()
 {
     $validator = validator()->object(['name' => 'required|string', 'company' => (object) ['name' => 'string', 'age' => 'integer']]);
     $validator->assert((object) ['name' => 'george']);
     $this->assertInvalidValueException(function () use($validator) {
         $validator->assert((object) []);
     }, ['required@name']);
     $validator = validator()->object(['name' => 'string', 'company' => (object) ['name' => 'required|string', 'age' => 'integer']]);
     $validator->assert((object) ['company' => (object) ['name' => 'gogle']]);
     $this->assertInvalidValueException(function () use($validator) {
         $validator->assert((object) ['company' => ['name' => 'gogle']]);
     }, ['object@company', '*****@*****.**']);
     $this->assertInvalidValueException(function () use($validator) {
         $validator->assert((object) []);
     }, ['*****@*****.**']);
 }
Example #10
0
 /**
  * @param array $attributes
  */
 public function __construct(array $attributes = [])
 {
     foreach ($attributes as $name => $validator) {
         $this->attributes[$name] = validator()->from($validator);
     }
 }
Example #11
0
 /**
  * @param array $properties
  */
 public function __construct(array $properties = [])
 {
     foreach ($properties as $name => $validator) {
         $this->properties[$name] = validator()->from($validator);
     }
 }
Example #12
0
 public function testViaCallMethod()
 {
     $validator1 = validator()->pipeline()->min(10)->max(100);
     $validator2 = validator()->pipeline([validator()->min(10), validator()->max(100)]);
     static::assertEquals($validator1, $validator2);
 }
Example #13
0
 /**
  * @param mixed $rule
  */
 public function __construct($rule = null)
 {
     if ($rule) {
         $this->validator = validator()->from($rule);
     }
 }
Example #14
0
 /**
  * @param string $name
  * @param array $arguments
  * @return \Wandu\Validator\Rules\PipelineValidator
  */
 public function __call($name, array $arguments = [])
 {
     $this->validators[] = validator()->__call($name, $arguments);
     return $this;
 }