From d3171b88720859269cf23811fcf89d25933fc6fd Mon Sep 17 00:00:00 2001 From: Tom Breese Date: Sat, 13 Oct 2018 12:57:50 -0400 Subject: [PATCH 1/5] Adding examples for regular and alternate syntax to docs --- README.md | 730 +++++++++++++++++++++++++++- tests/Valitron/ValidateTest.php | 831 +++++++++++++++++++++++++++++++- 2 files changed, 1559 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 337412b..a7d2344 100644 --- a/README.md +++ b/README.md @@ -161,7 +161,7 @@ extension for greater accuracy and reliability. The extension is not required for Valitron to work, but Valitron will use it if available, and it is highly recommended. -## Required fields +## required fields usage the `required` rule checks if a field exists in the data array, and is not null or an empty string. ```php $v->rule('required', 'field_name'); @@ -171,6 +171,664 @@ Using an extra parameter, you can make this rule more flexible, and only check i ```php $v->rule('required', 'field_name', true); ``` + +Alternate syntax. +```php +$v = new Valitron\Validator(['username' => 'spiderman', 'password' => 'Gr33nG0Blin', 'required_but_null' => null]); +$v->rules([ + 'required' => [ + ['username'], + ['password'], + ['required_but_null', true] // boolean flag allows empty value so long as the field name is set on the data array + ] +]); +$v->validate(); +``` + +Example using alternate syntax. + +## equals fields usage +The `equals` rule checks if two fields are equals in the data array, and that the second field is not null. +```php +$v->rule('equals', 'password', 'confirmPassword'); +``` + +Alternate syntax. +```php +$v = new Valitron\Validator(['password' => 'youshouldnotseethis', 'confirmPassword' => 'youshouldnotseethis']); +$v->rules([ + 'equals' => [ + ['password', 'confirmPassword'] + ] +]); +$v->validate(); +``` + +## different fields usage +The `different` rule checks if two fields are not the same, or different, in the data array and that the second field is not null. +```php +$v->rule('different', 'username', 'password'); +``` + +Alternate syntax. +```php +$v = new Valitron\Validator(['username' => 'spiderman', 'password' => 'Gr33nG0Blin']); +$v->rules([ + 'different' => [ + ['username', 'password'] + ] +]); +$v->validate(); +``` + +## accepted fields usage +The `accepted` rule checks if the field is either 'yes', 'on', 1, or true. +```php +$v->rule('accepted', 'remember_me'); +``` + +Alternate syntax. +```php +$v = new Valitron\Validator(['remember_me' => true]); +$v->rules([ + 'accepted' => [ + ['remember_me'] + ] +]); +$v->validate(); +``` + +## numeric fields usage +The `numeric` rule checks if the field is number. This is analogous to php's is_numeric() function. +```php +$v->rule('numeric', 'amount'); +``` + +Alternate syntax. +```php +$v = new Valitron\Validator(['amount' => 3.14]); +$v->rules([ + 'numeric' => [ + ['amount'] + ] +]); +$v->validate(); +``` + +## integer fields usage +The `integer` rule checks if the field is an integer number. +```php +$v->rule('integer', 'age'); +``` + +Alternate syntax. +```php +$v = new Valitron\Validator(['age' => '27']); +$v->rules([ + 'integer' => [ + ['age'] + ] +]); +$v->validate(); +``` + +*Note* the optional boolean flag for strict mode will allow for integers to be supplied as negative values. So the following rule would evaluate to true: +```php +$v = new Valitron\Validator(['age' => '-27']); +$v->rules([ + 'integer' => [ + ['age', true] + ] +]); +$v->validate(); +``` +Whereas the same for a positive (+) value would evaluate to false, as the + in this case is redundant: +```php +$v = new Valitron\Validator(['age' => '+27']); +$v->rules([ + 'integer' => [ + ['age', true] + ] +]); +$v->validate(); +``` + +## boolean fields usage +The `boolean` rule checks if the field is a boolean. This is analogous to php's is_bool() function. +```php +$v->rule('boolean', 'remember_me'); +``` + +Alternate syntax. +```php +$v = new Valitron\Validator(['remember_me' => true]); +$v->rules([ + 'boolean' => [ + ['remember_me'] + ] +]); +$v->validate(); +``` + +## array fields usage +The `array` rule checks if the field is an array. This is analogous to php's is_array() function. +```php +$v->rule('array', 'user_notifications'); +``` + +Alternate Syntax. +```php +$v = new Valitron\Validator(['user_notifications' => ['bulletin_notifications' => true, 'marketing_notifications' => false, 'message_notification' => true]]); +$v->rules([ + 'array' => [ + ['user_notifications'] + ] +]); +$v->validate(); +``` + +## length fields usage +The `length` rule checks if the field is exactly a given length and that the field is a valid string. +```php +$v->rule('length', 'username', 10); +``` + +Alternate syntax. +```php +$v = new Valitron\Validator(['username' => 'bobburgers']); +$v->rules([ + 'length' => [ + ['username', 10] + ] +]); +$v->validate(); +``` + +## lengthBetween fields usage +The `lengthBetween` rule checks if the field is between a given length tange and that the field is a valid string. +```php +$v->rule('lengthBetween', 'username', 1, 10); +``` + +Alternate syntax. +```php +$v = new Valitron\Validator(['username' => 'bobburgers']); +$v->rules([ + 'lengthBetween' => [ + ['username', 1, 10] + ] +]); +$v->validate(); +``` + +## lengthMin fields usage +The `lengthMin` rule checks if the field is at least a given length and that the field is a valid string. +```php +$v->rule('lengthMin', 'username', 5); +``` + +Alternate syntax. +```php +$v = new Valitron\Validator(['username' => 'martha']); +$v->rules([ + 'lengthMin' => [ + ['username', 5] + ] +]); +$v->validate(); +``` + +## lengthMax fields usage +The `lengthMax` rule checks if the field is at most a given length and that the field is a valid string. +```php +$v->rule('lengthMax', 'username', 10); +``` + +Alternate syntax. +```php +$v = new Valitron\Validator(['username' => 'bruins91']); +$v->rules([ + 'lengthMax' => [ + ['username', 10] + ] +]); +$v->validate(); +``` + +## min fields usage +The `min` rule checks if the field is at least a given value and that the provided value is numeric. +```php +$v->rule('min', 'age', 18); +``` + +Alternate syntax. +```php +$v = new Valitron\Validator(['age' => 28]); +$v->rules([ + 'min' => [ + ['age', 18] + ] +]); +$v->validate(); +``` + +## max fields usage +The `max` rule checks if the field is at most a given value and that the provided value is numeric. +```php +$v->rule('max', 'age', 12); +``` + +Alternate syntax. +```php +$v = new Valitron\Validator(['age' => 10]); +$v->rules([ + 'max' => [ + ['age', 12] + ] +]); +$v->validate(); +``` + +## in fields usage +The `in` rule checks that the field is present in a given array of values. +```php +$v->rule('in', 'color', ['blue', 'green', 'red', 'purple']); +``` + +Alternate syntax. +```php +$v = new Valitron\Validator(['color' => 'purple']); +$v->rules([ + 'in' => [ + ['color', ['blue', 'green', 'red', 'purple']] + ] +]); +$v->validate(); +``` + +## notIn fields usage +The `notIn` rule checks that the field is NOT present in a given array of values. +```php +$v->rule('notIn', 'color', ['blue', 'green', 'red', 'yellow']); +``` + +Alternate syntax. +```php +$v = new Valitron\Validator(['color' => 'purple']); +$v->rules([ + 'notIn' => [ + ['color', ['blue', 'green', 'red', 'yellow']] + ] +]); +$v->validate(); +``` + +## ip fields usage +The `ip` rule checks that the field is a valid ip address. This includes IPv4, IPv6, private, and reserved ranges. +```php +$v->rule('ip', 'user_ip'); +``` + +Alternate syntax. +```php +$v = new Valitron\Validator(['user_ip' => '127.0.0.1']); +$v->rules([ + 'ip' => [ + ['user_ip'] + ] +]); +$v->validate(); +``` + +## ipv4 fields usage +The `ipv4` rule checks that the field is a valid IPv4 address. +```php +$v->rule('ipv4', 'user_ip'); +``` + +Alternate syntax. +```php +$v = new Valitron\Validator(['user_ip' => '127.0.0.1']); +$v->rules([ + 'ipv4' => [ + ['user_ip'] + ] +]); +$v->validate(); +``` + +## ipv6 fields usage +The `ipv6` rule checks that the field is a valid IPv6 address. +```php +$v->rule('ipv6', 'user_ip'); +``` + +Alternate syntax. +```php +$v = new Valitron\Validator(['user_ip' => '0:0:0:0:0:0:0:1']); +$v->rules([ + 'ipv6' => [ + ['user_ip'] + ] +]); +$v->validate(); +``` + +## email fields usage +The `email` rule checks that the field is a valid email address. +```php +$v->rule('email', 'user_email'); +``` + +Alternate syntax. +```php +$v = new Valitron\Validator(['user_email' => 'someone@example.com']); +$v->rules([ + 'email' => [ + ['user_email'] + ] +]); +$v->validate(); +``` + +## emailDNS fields usage +The `emailDNS` rule validates the field is a valid email address with an active DNS record or any type. +```php +$v->rule('emailDNS', 'user_email'); +``` + +Alternate syntax. +```php +$v = new Valitron\Validator(['user_email' => 'some_fake_email_address@gmail.com']); +$v->rules([ + 'emailDNS' => [ + ['user_email'] + ] +]); +$v->validate(); +``` + +## url fields usage +The `url` rule checks the field is a valid url. +```php +$v->rule('url', 'website'); +``` + +Alternate syntax. +```php +$v = new Valitron\Validator(['website' => 'https://example.com/contact']); +$v->rules([ + 'url' => [ + ['website'] + ] +]); +$v->validate(); +``` + +## urlActive fields usage +The `urlActive` rule checks the field is a valid url with an active A, AAAA, or CNAME record. +```php +$v->rule('urlActive', 'website'); +``` + +Alternate syntax. +```php +$v = new Valitron\Validator(['website' => 'https://example.com/contact']); +$v->rules([ + 'urlActive' => [ + ['website'] + ] +]); +$v->validate(); +``` + +## alpha fields usage +The `alpha` rule checks the field is alphabetic characters only. +```php +$v->rule('alpha', 'username'); +``` + +Alternate syntax. +```php +$v = new Valitron\Validator(['username' => 'batman']); +$v->rules([ + 'alpha' => [ + ['username'] + ] +]); +$v->validate(); +``` + +## alphaNum fields usage +The `alphaNum` rule checks the field contains only alphabetic or numeric characters. +```php +$v->rule('alphaNum', 'username'); +``` + +Alternate syntax. +```php +$v = new Valitron\Validator(['username' => 'batman123']); +$v->rules([ + 'alphaNum' => [ + ['username'] + ] +]); +$v->validate(); +``` + +## ascii fields usage +The `ascii` rule checks the field contains only characters in the ascii character set. +```php +$v->rule('ascii', 'username'); +``` + +Alternate syntax. +```php +$v = new Valitron\Validator(['username' => 'batman123']); +$v->rules([ + 'ascii' => [ + ['username'] + ] +]); +$v->validate(); +``` + +## slug fields usage +The `slug` rule checks that the field only contains URL slug characters (a-z, 0-9, -, _). +```php +$v->rule('slug', 'username'); +``` + +Alternate syntax. +```php +$v = new Valitron\Validator(['username' => 'L337-H4ckZ0rz_123']); +$v->rules([ + 'slug' => [ + ['username'] + ] +]); +$v->validate(); +``` + +## regex fields usage +The `regex` rule ensures the field matches a given regex pattern. +(This regex checks the string is alpha numeric between 5-10 characters). +```php +$v->rule('regex', 'username', '/^[a-zA-Z0-9]{5,10}$/'); +``` + +Alternate syntax. +```php +$v = new Valitron\Validator(['username' => 'Batman123']); +$v->rules([ + 'regex' => [ + ['username', '/^[a-zA-Z0-9]{5,10}$/'] + ] +]); +$v->validate(); +``` + +## date fields usage +The `date` rule checks if the supplied field is a valid \DateTime object or if the string can be converted to a unix timestamp via strtotime(). +```php +$v->rule('date', 'created_at'); +``` + +Alternate syntax. +```php +$v = new Valitron\Validator(['created_at' => '2018-10-13']); +$v->rules([ + 'date' => [ + ['created_at'] + ] +]); +$v->validate(); +``` + +## dateFormat fields usage +The `dateFormat` rule checks that the supplied field is a valid date in a specified date format. +```php +$v->rule('dateFormat', 'created_at', 'Y-m-d'); +``` + +Alternate syntax. +```php +$v = new Valitron\Validator(['created_at' => '2018-10-13']); +$v->rules([ + 'dateFormat' => [ + ['created_at', 'Y-m-d'] + ] +]); +$v->validate(); +``` + +## dateBefore fields usage +The `dateBefore` rule checks that the supplied field is a valid date before a specified date. +```php +$v->rule('dateBefore', 'created_at', '2018-10-13'); +``` + +Alternate syntax. +```php +$v = new Valitron\Validator(['created_at' => '2018-09-01']); +$v->rules([ + 'dateBefore' => [ + ['created_at', '2018-10-13'] + ] +]); +$v->validate(); +``` + +## dateAfter fields usage +The `dateAfter` rule checks that the supplied field is a valid date after a specified date. +```php +$v->rule('dateAfter', 'created_at', '2018-10-13'); +``` + +Alternate syntax. +```php +$v = new Valitron\Validator(['created_at' => '2018-09-01']); +$v->rules([ + 'dateAfter' => [ + ['created_at', '2018-01-01'] + ] +]); +$v->validate(); +``` + +## contains fields usage +The `contains` rule checks that a given string exists within the field and checks that the field and the search value are both valid strings. +```php +$v->rule('contains', 'username', 'man'); +``` + +Alternate syntax. +```php +$v = new Valitron\Validator(['username' => 'Batman123']); +$v->rules([ + 'contains' => [ + ['username', 'man'] + ] +]); +$v->validate(); +``` + +*Note* You can use the optional strict flag to ensure a case-sensitive match. +The following example will return true: +```php +$v = new Valitron\Validator(['username' => 'Batman123']); +$v->rules([ + 'contains' => [ + ['username', 'man'] + ] +]); +$v->validate(); +``` +Whereas, this would return false, as the M in the search string is not uppercase in the provided value: +```php +$v = new Valitron\Validator(['username' => 'Batman123']); +$v->rules([ + 'contains' => [ + ['username', 'Man', true] + ] +]); +$v->validate(); +``` + +## subset fields usage +The `subset` rule checks that the field is either a scalar or array field and that all of it's values are contained within a given set of values. +```php +$v->rule('subset', 'colors', ['green', 'blue', 'orange']); +``` + +Alternate syntax. +```php +$v = new Valitron\Validator(['colors' => ['green', 'blue']]); +$v->rules([ + 'subset' => [ + ['colors', ['orange', 'green', 'blue', 'red']] + ] +]); +$v->validate(); +``` +This example would return false, as the provided color, purple, does not exist in the array of accepted values we're providing. +```php +$v = new Valitron\Validator(['colors' => ['purple', 'blue']]); +$v->rules([ + 'subset' => [ + ['colors', ['orange', 'green', 'blue', 'red']] + ] +]); +$v->validate(); +``` + +## containsUnique fields usage +The `containsUnique` rule checks that the provided field is an array and that all values contained within are unique, i.e. no duplicate values in the array. +```php +$v->rule('containsUnique', 'colors'); +``` + +Alternate syntax. +```php +$v = new Valitron\Validator(['colors' => ['purple', 'blue']]); +$v->rules([ + 'containsUnique' => [ + ['colors'] + ] +]); +$v->validate(); +``` +This example would return false, as the values in the provided array are duplicates. +```php +$v = new Valitron\Validator(['colors' => ['purple', 'purple']]); +$v->rules([ + 'containsUnique' => [ + ['colors'] + ] +]); +$v->validate(); +``` + ## Credit Card Validation usage Credit card validation currently allows you to validate a Visa `visa`, @@ -204,6 +862,68 @@ $v->rule('creditCard', 'credit_card', $cardType, ['visa', 'mastercard']); $v->validate(); // false ``` +## instanceOf fields usage +The `instanceOf` rule checks that the field is an instance of a given class. +```php +$v->rule('instanceOf', 'date', \DateTime); +``` + +Alternate syntax. +```php +$v = new Valitron\Validator(['date' => new \DateTime()]); +$v->rules([ + 'instanceOf' => [ + ['date', 'DateTime'] + ] +]); +$v->validate(); +``` +*Note* You can also compare the value against a given object as opposed to the string class name. +This example would also return true: +```php +$v = new Valitron\Validator(['date' => new \DateTime()]); +$existingDateObject = new \DateTime(); +$v->rules([ + 'instanceOf' => [ + ['date', $existingDateObject] + ] +]); +$v->validate(); +``` + +## optional fields usage +The `optional` rule ensures that if the field is present in the data set that it passes all validation rules. +```php +$v->rule('optional', 'username'); +``` + +Alternate syntax. +This example would return true either when the 'username' field is not present or in the case where the username is only alphabetic characters. +```php +$v = new Valitron\Validator(['username' => 'batman']); +$v->rules([ + 'alpha' => [ + ['username'] + ], + 'optional' => [ + ['username'] + ] +]); +$v->validate(); +``` +This example would return false, as although the field is optional, since it is provided it must pass all the validation rules, which in this case it does not. +```php +$v = new Valitron\Validator(['username' => 'batman123']); +$v->rules([ + 'alpha' => [ + ['username'] + ], + 'optional' => [ + ['username'] + ] +]); +$v->validate(); +``` ## Adding Custom Validation Rules @@ -240,6 +960,14 @@ This rule will take the same parameters as `Validator::addRule` but it has to be called on a `Validator` instance. +## Chaining rules + +You can chain multiple rules together using the following syntax. +```php +$v = new Valitron\Validator(['email_address' => 'test@test.com']); +$v->rule('required', 'email_address')->rule('email', 'email_address'); +$v->validate(); +``` ## Alternate syntax for adding rules diff --git a/tests/Valitron/ValidateTest.php b/tests/Valitron/ValidateTest.php index ba59117..8cbe9f7 100644 --- a/tests/Valitron/ValidateTest.php +++ b/tests/Valitron/ValidateTest.php @@ -51,6 +51,19 @@ public function testRequiredValid() $this->assertTrue($v->validate()); } + public function testRequiredValidAltSyntax() + { + $v = new Valitron\Validator(['username' => 'spiderman', 'password' => 'Gr33nG0Blin', 'required_but_null' => null]); + $v->rules([ + 'required' => [ + ['username'], + ['password'], + ['required_but_null', true] // boolean flag allows empty value so long as the field name is set on the data array + ] + ]); + $this->assertTrue($v->validate()); + } + public function testRequiredNonExistentField() { $v = new Validator(array('name' => 'Chester Tester')); @@ -58,6 +71,18 @@ public function testRequiredNonExistentField() $this->assertFalse($v->validate()); } + public function testRequiredNonExistentFieldAltSyntax() + { + $v = new Valitron\Validator(['boozername' => 'spiderman', 'notPassword' => 'Gr33nG0Blin']); + $v->rules([ + 'required' => [ + ['username'], + ['password'] + ] + ]); + $this->assertFalse($v->validate()); + } + public function testEqualsValid() { $v = new Validator(array('foo' => 'bar', 'bar' => 'bar')); @@ -65,6 +90,17 @@ public function testEqualsValid() $this->assertTrue($v->validate()); } + public function testEqualsValidAltSyntax() + { + $v = new Validator(['password' => 'youshouldnotseethis', 'confirmPassword' => 'youshouldnotseethis']); + $v->rules([ + 'equals' => [ + ['password', 'confirmPassword'] + ] + ]); + $this->assertTrue($v->validate()); + } + public function testEqualsInvalid() { $v = new Validator(array('foo' => 'foo', 'bar' => 'bar')); @@ -72,6 +108,16 @@ public function testEqualsInvalid() $this->assertFalse($v->validate()); } + public function testEqualsInvalidAltSyntax() + { + $v = new Validator(['password' => 'youshouldnotseethis', 'confirmPassword' => 'differentpassword']); + $v->rules([ + 'equals' => [ + ['password', 'confirmPassword'] + ] + ]); + } + public function testEqualsBothNull() { $v = new Validator(array('foo' => null, 'bar' => null)); @@ -109,6 +155,17 @@ public function testDifferentValid() $this->assertTrue($v->validate()); } + public function testDifferentValidAltSyntax() + { + $v = new Validator(['username' => 'test', 'password' => 'test123']); + $v->rules([ + 'different' => [ + ['username', 'password'] + ] + ]); + $this->assertTrue($v->validate()); + } + public function testDifferentInvalid() { $v = new Validator(array('foo' => 'baz', 'bar' => 'baz')); @@ -116,6 +173,16 @@ public function testDifferentInvalid() $this->assertFalse($v->validate()); } + public function testDifferentInvalidAltSyntax() + { + $v = new Validator(['username' => 'test', 'password' => 'test']); + $v->rules([ + 'different' => [ + ['username', 'password'] + ] + ]); + } + public function testDifferentBothNull() { $v = new Validator(array('foo' => null, 'bar' => null)); @@ -153,6 +220,17 @@ public function testAcceptedValid() $this->assertTrue($v->validate()); } + public function testAcceptedValidAltSyntax() + { + $v = new Valitron\Validator(['remember_me' => true]); + $v->rules([ + 'accepted' => [ + ['remember_me'] + ] + ]); + $this->assertTrue($v->validate()); + } + public function testAcceptedInvalid() { $v = new Validator(array('agree' => 'no')); @@ -160,8 +238,18 @@ public function testAcceptedInvalid() $this->assertFalse($v->validate()); } - public function testAcceptedNotSet() + public function testAcceptedInvalidAltSyntax() { + $v = new Valitron\Validator(['remember_me' => false]); + $v->rules([ + 'accepted' => [ + ['remember_me'] + ] + ]); + $this->assertFalse($v->validate()); + } + + public function testAcceptedNotSet(){ $v = new Validator(); $v->rule('accepted', 'agree'); $this->assertFalse($v->validate()); @@ -174,6 +262,17 @@ public function testNumericValid() $this->assertTrue($v->validate()); } + public function testNumericValidAltSyntax() + { + $v = new Valitron\Validator(['amount' => 3.14]); + $v->rules([ + 'numeric' => [ + ['amount'] + ] + ]); + $this->assertTrue($v->validate()); + } + public function testNumericInvalid() { $v = new Validator(array('num' => 'nope')); @@ -181,6 +280,17 @@ public function testNumericInvalid() $this->assertFalse($v->validate()); } + public function testNumericInvalidAltSyntax() + { + $v = new Valitron\Validator(['amount' => 'banana']); + $v->rules([ + 'numeric' => [ + ['amount'] + ] + ]); + $this->assertFalse($v->validate()); + } + public function testIntegerValid() { $v = new Validator(array('num' => '41243')); @@ -192,6 +302,17 @@ public function testIntegerValid() $this->assertTrue($v->validate()); } + public function testIntegerValidAltSyntax() + { + $v = new Valitron\Validator(['age' => 27]); + $v->rules([ + 'integer' => [ + ['age', true] + ] + ]); + $this->assertTrue($v->validate()); + } + public function testIntegerStrict() { @@ -255,6 +376,17 @@ public function testIntegerInvalid() $this->assertFalse($v->validate()); } + public function testIntegerInvalidAltSyntax() + { + $v = new Valitron\Validator(['age' => 3.14]); + $v->rules([ + 'integer' => [ + ['age'] + ] + ]); + $this->assertFalse($v->validate()); + } + public function testLengthValid() { $v = new Validator(array('str' => 'happy')); @@ -262,6 +394,17 @@ public function testLengthValid() $this->assertTrue($v->validate()); } + public function testLengthValidAltSyntax() + { + $v = new Valitron\Validator(['username' => 'bobburgers']); + $v->rules([ + 'length' => [ + ['username', 10] + ] + ]); + $this->assertTrue($v->validate()); + } + public function testLengthInvalid() { $v = new Validator(array('str' => 'sad')); @@ -277,6 +420,17 @@ public function testLengthInvalid() $this->assertFalse($v->validate()); } + public function testLengthInvalidAltSyntax() + { + $v = new Valitron\Validator(['username' => 'hi']); + $v->rules([ + 'length' => [ + ['username', 10] + ] + ]); + $this->assertFalse($v->validate()); + } + public function testLengthBetweenValid() { $v = new Validator(array('str' => 'happy')); @@ -284,6 +438,17 @@ public function testLengthBetweenValid() $this->assertTrue($v->validate()); } + public function testLengthBetweenValidAltSyntax() + { + $v = new Valitron\Validator(['username' => 'bobburgers']); + $v->rules([ + 'lengthBetween' => [ + ['username', 1, 10] + ] + ]); + $this->assertTrue($v->validate()); + } + public function testLengthBetweenInvalid() { $v = new Validator(array('str' => 'sad')); @@ -299,12 +464,34 @@ public function testLengthBetweenInvalid() $this->assertFalse($v->validate()); } + public function testLengthBetweenInvalidAltSyntax() + { + $v = new Valitron\Validator(['username' => 'hi']); + $v->rules([ + 'lengthBetween' => [ + ['username', 3, 10] + ] + ]); + $this->assertFalse($v->validate()); + } + public function testLengthMinValid() { $v = new Validator(array('str' => 'happy')); $v->rule('lengthMin', 'str', 4); $this->assertTrue($v->validate()); } + + public function testLengthMinValidAltSyntax() + { + $v = new Valitron\Validator(['username' => 'martha']); + $v->rules([ + 'lengthMin' => [ + ['username', 5] + ] + ]); + $this->assertTrue($v->validate()); + } public function testLengthMinInvalid() { @@ -313,6 +500,17 @@ public function testLengthMinInvalid() $this->assertFalse($v->validate()); } + public function testLengthMinInvalidAltSyntax() + { + $v = new Valitron\Validator(['username' => 'abc']); + $v->rules([ + 'lengthMin' => [ + ['username', 5] + ] + ]); + $this->assertFalse($v->validate()); + } + public function testLengthMaxValid() { $v = new Validator(array('str' => 'sad')); @@ -320,6 +518,17 @@ public function testLengthMaxValid() $this->assertTrue($v->validate()); } + public function testLengthMaxValidAltSyntax() + { + $v = new Valitron\Validator(['username' => 'bruins91']); + $v->rules([ + 'lengthMax' => [ + ['username', 10] + ] + ]); + $this->assertTrue($v->validate()); + } + public function testLengthMaxInvalid() { $v = new Validator(array('str' => 'sad')); @@ -327,6 +536,17 @@ public function testLengthMaxInvalid() $this->assertFalse($v->validate()); } + public function testLengthMaxInvalidAltSyntax() + { + $v = new Valitron\Validator(['username' => 'bruins91']); + $v->rules([ + 'lengthMax' => [ + ['username', 3] + ] + ]); + $this->assertFalse($v->validate()); + } + public function testMinValid() { $v = new Validator(array('num' => 5)); @@ -338,6 +558,17 @@ public function testMinValid() $this->assertTrue($v->validate()); } + public function testMinValidAltSyntax() + { + $v = new Valitron\Validator(['age' => 28]); + $v->rules([ + 'min' => [ + ['age', 18] + ] + ]); + $this->assertTrue($v->validate()); + } + public function testMinValidFloat() { if (!function_exists('bccomp')) { @@ -368,6 +599,17 @@ public function testMinInvalid() $this->assertFalse($v->validate()); } + public function testMinInvalidAltSyntax() + { + $v = new Valitron\Validator(['age' => 16]); + $v->rules([ + 'min' => [ + ['age', 18] + ] + ]); + $this->assertFalse($v->validate()); + } + public function testMinInvalidFloat() { $v = new Validator(array('num' => 0.5)); @@ -386,6 +628,17 @@ public function testMaxValid() $this->assertTrue($v->validate()); } + public function testMaxValidAltSyntax() + { + $v = new Valitron\Validator(['age' => 10]); + $v->rules([ + 'max' => [ + ['age', 12] + ] + ]); + $this->assertTrue($v->validate()); + } + public function testMaxValidFloat() { if (!function_exists('bccomp')) { @@ -416,6 +669,17 @@ public function testMaxInvalid() $this->assertFalse($v->validate()); } + public function testMaxInvalidAltSyntax() + { + $v = new Valitron\Validator(['age' => 29]); + $v->rules([ + 'max' => [ + ['age', 12] + ] + ]); + $this->assertFalse($v->validate()); + } + public function testMaxInvalidFloat() { $v = new Validator(array('num' => 0.9)); @@ -466,6 +730,28 @@ public function testInValid() $this->assertTrue($v->validate()); } + public function testInValidAltSyntax() + { + $v = new Valitron\Validator(['color' => 'purple']); + $v->rules([ + 'in' => [ + ['color', ['blue', 'green', 'red', 'purple']] + ] + ]); + $this->assertTrue($v->validate()); + } + + public function testInInvalidAltSyntax() + { + $v = new Valitron\Validator(['color' => 'orange']); + $v->rules([ + 'in' => [ + ['color', ['blue', 'green', 'red', 'purple']] + ] + ]); + $this->assertFalse($v->validate()); + } + public function testInValidAssociativeArray() { $v = new Validator(array('color' => 'green')); @@ -491,6 +777,17 @@ public function testArrayValid() $this->assertTrue($v->validate()); } + public function testArrayValidAltSyntax() + { + $v = new Valitron\Validator(['user_notifications' => ['bulletin_notifications' => true, 'marketing_notifications' => false, 'message_notification' => true]]); + $v->rules([ + 'array' => [ + ['user_notifications'] + ] + ]); + $this->assertTrue($v->validate()); + } + public function testAssocArrayValid() { $v = new Validator(array('settings' => array('color' => 'yellow'))); @@ -498,6 +795,17 @@ public function testAssocArrayValid() $this->assertTrue($v->validate()); } + public function testArrayInvalidAltSyntax() + { + $v = new Valitron\Validator(['user_notifications' => 'string']); + $v->rules([ + 'array' => [ + ['user_notifications'] + ] + ]); + $this->assertFalse($v->validate()); + } + public function testArrayInvalid() { $v = new Validator(array('colors' => 'yellow')); @@ -614,6 +922,17 @@ public function testNotInValid() $this->assertTrue($v->validate()); } + public function testNotInValidAltSyntax() + { + $v = new Valitron\Validator(['color' => 'purple']); + $v->rules([ + 'notIn' => [ + ['color', ['blue', 'green', 'red', 'yellow']] + ] + ]); + $this->assertTrue($v->validate()); + } + public function testNotInInvalid() { $v = new Validator(array('color' => 'blue')); @@ -621,6 +940,16 @@ public function testNotInInvalid() $this->assertFalse($v->validate()); } + public function testNotInInvalidAltSyntax() + { + $v = new Valitron\Validator(['color' => 'yellow']); + $v->rules([ + 'notIn' => [ + ['color', ['blue', 'green', 'red', 'yellow']] + ] + ]); + } + public function testAsciiValid() { $v = new Validator(array('text' => '12345 abcde')); @@ -628,6 +957,17 @@ public function testAsciiValid() $this->assertTrue($v->validate()); } + public function testAsciiValidAltSyntax() + { + $v = new Valitron\Validator(['username' => 'batman123']); + $v->rules([ + 'ascii' => [ + ['username'] + ] + ]); + $this->assertTrue($v->validate()); + } + public function testAsciiInvalid() { $v = new Validator(array('text' => '12345 abcdé')); @@ -635,6 +975,17 @@ public function testAsciiInvalid() $this->assertFalse($v->validate()); } + public function testAsciiInvalidAltSyntax() + { + $v = new Valitron\Validator(['username' => '12345 abcdé']); + $v->rules([ + 'ascii' => [ + ['username'] + ] + ]); + $this->assertFalse($v->validate()); + } + public function testIpValid() { $v = new Validator(array('ip' => '127.0.0.1')); @@ -642,6 +993,17 @@ public function testIpValid() $this->assertTrue($v->validate()); } + public function testIpValidAltSyntax() + { + $v = new Valitron\Validator(['user_ip' => '127.0.0.1']); + $v->rules([ + 'ip' => [ + ['user_ip'] + ] + ]); + $this->assertTrue($v->validate()); + } + public function testIpInvalid() { $v = new Validator(array('ip' => 'buy viagra now!')); @@ -649,6 +1011,17 @@ public function testIpInvalid() $this->assertFalse($v->validate()); } + public function testIpInvalidAltSyntax() + { + $v = new Valitron\Validator(['user_ip' => '127.0.0.1.345']); + $v->rules([ + 'ip' => [ + ['user_ip'] + ] + ]); + $this->assertFalse($v->validate()); + } + public function testIpv4Valid() { $v = new Validator(array('ip' => '127.0.0.1')); @@ -656,6 +1029,17 @@ public function testIpv4Valid() $this->assertTrue($v->validate()); } + public function testIpv4ValidAltSyntax() + { + $v = new Valitron\Validator(['user_ip' => '127.0.0.1']); + $v->rules([ + 'ipv4' => [ + ['user_ip'] + ] + ]); + $this->assertTrue($v->validate()); + } + public function testIpv4Invalid() { $v = new Validator(array('ip' => 'FE80::0202:B3FF:FE1E:8329')); @@ -663,6 +1047,17 @@ public function testIpv4Invalid() $this->assertFalse($v->validate()); } + public function testIpv4InvalidAltSyntax() + { + $v = new Valitron\Validator(['user_ip' => '127.0.0.1.234']); + $v->rules([ + 'ipv4' => [ + ['user_ip'] + ] + ]); + $this->assertFalse($v->validate()); + } + public function testIpv6Valid() { $v = new Validator(array('ip' => 'FE80::0202:B3FF:FE1E:8329')); @@ -670,6 +1065,17 @@ public function testIpv6Valid() $this->assertTrue($v->validate()); } + public function testIpv6ValidAltSyntax() + { + $v = new Valitron\Validator(['user_ipv6' => '0:0:0:0:0:0:0:1']); + $v->rules([ + 'ipv6' => [ + ['user_ipv6'] + ] + ]); + $this->assertTrue($v->validate()); + } + public function testIpv6Invalid() { $v = new Validator(array('ip' => '127.0.0.1')); @@ -677,6 +1083,17 @@ public function testIpv6Invalid() $this->assertFalse($v->validate()); } + public function testIpv6InvalidAltSyntax() + { + $v = new Valitron\Validator(['user_ipv6' => '0:0:0:0:0:0:0:1:3:4:5']); + $v->rules([ + 'ipv6' => [ + ['user_ipv6'] + ] + ]); + $this->assertFalse($v->validate()); + } + public function testEmailValid() { $v = new Validator(array('name' => 'Chester Tester', 'email' => 'chester@tester.com')); @@ -684,6 +1101,17 @@ public function testEmailValid() $this->assertTrue($v->validate()); } + public function testEmailValidAltSyntax() + { + $v = new Valitron\Validator(['user_email' => 'someone@example.com']); + $v->rules([ + 'email' => [ + ['user_email'] + ] + ]); + $this->assertTrue($v->validate()); + } + public function testEmailInvalid() { $v = new Validator(array('name' => 'Chester Tester', 'email' => 'chestertesterman')); @@ -691,6 +1119,17 @@ public function testEmailInvalid() $this->assertFalse($v->validate()); } + public function testEmailInvalidAltSyntax() + { + $v = new Valitron\Validator(['user_email' => 'example.com']); + $v->rules([ + 'email' => [ + ['user_email'] + ] + ]); + $this->assertFalse($v->validate()); + } + public function testEmailDnsValid() { $v = new Validator(array('name' => 'Chester Tester', 'email' => 'chester@tester.com')); @@ -698,6 +1137,17 @@ public function testEmailDnsValid() $this->assertTrue($v->validate()); } + public function testEmailDnsValidAltSyntax() + { + $v = new Valitron\Validator(['user_email' => 'some_fake_email_address@gmail.com']); + $v->rules([ + 'emailDNS' => [ + ['user_email'] + ] + ]); + $this->assertTrue($v->validate()); + } + public function testEmailDnsInvalid() { $v = new Validator(array('name' => 'Chester Tester', 'email' => 'chester@tester.zyx')); @@ -705,6 +1155,17 @@ public function testEmailDnsInvalid() $this->assertFalse($v->validate()); } + public function testEmailDnsInvalidAltSyntax() + { + $v = new Valitron\Validator(['user_email' => 'some_fake_email_address@gmail.zyx']); + $v->rules([ + 'emailDNS' => [ + ['user_email'] + ] + ]); + $this->assertFalse($v->validate()); + } + public function testUrlValid() { $v = new Validator(array('website' => 'http://google.com')); @@ -712,6 +1173,17 @@ public function testUrlValid() $this->assertTrue($v->validate()); } + public function testUrlValidAltSyntax() + { + $v = new Valitron\Validator(['website' => 'https://example.com/contact']); + $v->rules([ + 'url' => [ + ['website'] + ] + ]); + $this->assertTrue($v->validate()); + } + public function testUrlInvalid() { $v = new Validator(array('website' => 'shoobedobop')); @@ -719,6 +1191,17 @@ public function testUrlInvalid() $this->assertFalse($v->validate()); } + public function testUrlInvalidAltSyntax() + { + $v = new Valitron\Validator(['website' => 'thisisjusttext']); + $v->rules([ + 'url' => [ + ['website'] + ] + ]); + $this->assertFalse($v->validate()); + } + public function testUrlActive() { $v = new Validator(array('website' => 'http://google.com')); @@ -726,6 +1209,17 @@ public function testUrlActive() $this->assertTrue($v->validate()); } + public function testUrlActiveValidAltSyntax() + { + $v = new Valitron\Validator(['website' => 'https://example.com/contact']); + $v->rules([ + 'urlActive' => [ + ['website'] + ] + ]); + $this->assertTrue($v->validate()); + } + public function testUrlInactive() { $v = new Validator(array('website' => 'http://example-test-domain-' . md5(time()) . '.com')); @@ -733,6 +1227,17 @@ public function testUrlInactive() $this->assertFalse($v->validate()); } + public function testUrlActiveInvalidAltSyntax() + { + $v = new Valitron\Validator(['website' => 'https://example-domain']); + $v->rules([ + 'urlActive' => [ + ['website'] + ] + ]); + $this->assertFalse($v->validate()); + } + public function testAlphaValid() { $v = new Validator(array('test' => 'abcDEF')); @@ -740,6 +1245,17 @@ public function testAlphaValid() $this->assertTrue($v->validate()); } + public function testAlphaValidAltSyntax() + { + $v = new Valitron\Validator(['username' => 'batman']); + $v->rules([ + 'alpha' => [ + ['username'] + ] + ]); + $this->assertTrue($v->validate()); + } + public function testAlphaInvalid() { $v = new Validator(array('test' => 'abc123')); @@ -747,6 +1263,17 @@ public function testAlphaInvalid() $this->assertFalse($v->validate()); } + public function testAlphaInvalidAltSyntax() + { + $v = new Valitron\Validator(['username' => '123456asdf']); + $v->rules([ + 'alpha' => [ + ['username'] + ] + ]); + $this->assertFalse($v->validate()); + } + public function testAlphaNumValid() { $v = new Validator(array('test' => 'abc123')); @@ -754,6 +1281,17 @@ public function testAlphaNumValid() $this->assertTrue($v->validate()); } + public function testAlphaNumValidAltSyntax() + { + $v = new Valitron\Validator(['username' => 'batman123']); + $v->rules([ + 'alphaNum' => [ + ['username'] + ] + ]); + $this->assertTrue($v->validate()); + } + public function testAlphaNumInvalid() { $v = new Validator(array('test' => 'abc123$%^')); @@ -761,6 +1299,17 @@ public function testAlphaNumInvalid() $this->assertFalse($v->validate()); } + public function testAlphaNumInvalidAltSyntax() + { + $v = new Valitron\Validator(['username' => 'batman123-$']); + $v->rules([ + 'alphaNum' => [ + ['username'] + ] + ]); + $this->assertFalse($v->validate()); + } + public function testAlphaDashValid() { $v = new Validator(array('test' => 'abc-123_DEF')); @@ -768,6 +1317,17 @@ public function testAlphaDashValid() $this->assertTrue($v->validate()); } + public function testSlugValidAltSyntax() + { + $v = new Valitron\Validator(['username' => 'L337-H4ckZ0rz_123']); + $v->rules([ + 'slug' => [ + ['username'] + ] + ]); + $this->assertTrue($v->validate()); + } + public function testAlphaDashInvalid() { $v = new Validator(array('test' => 'abc-123_DEF $%^')); @@ -775,6 +1335,17 @@ public function testAlphaDashInvalid() $this->assertFalse($v->validate()); } + public function testSlugInvalidAltSyntax() + { + $v = new Valitron\Validator(['username' => 'L337-H4ckZ0rz_123 $%^']); + $v->rules([ + 'slug' => [ + ['username'] + ] + ]); + $this->assertFalse($v->validate()); + } + public function testNoErrorFailOnArray() { $v = new Validator(array('test' => array())); @@ -789,6 +1360,17 @@ public function testRegexValid() $this->assertTrue($v->validate()); } + public function testRegexValidAltSyntax() + { + $v = new Valitron\Validator(['username' => 'Batman123']); + $v->rules([ + 'regex' => [ + ['username', '/^[a-zA-Z0-9]{5,10}$/'] + ] + ]); + $this->assertTrue($v->validate()); + } + public function testRegexInvalid() { $v = new Validator(array('test' => 'istheanswer')); @@ -796,6 +1378,17 @@ public function testRegexInvalid() $this->assertFalse($v->validate()); } + public function testRegexInvalidAltSyntax() + { + $v = new Valitron\Validator(['username' => 'Batman_123']); + $v->rules([ + 'regex' => [ + ['username', '/^[a-zA-Z0-9]{5,10}$/'] + ] + ]); + $this->assertFalse($v->validate()); + } + public function testDateValid() { $v = new Validator(array('date' => '2013-01-27')); @@ -803,6 +1396,17 @@ public function testDateValid() $this->assertTrue($v->validate()); } + public function testDateValidAltSyntax() + { + $v = new Valitron\Validator(['created_at' => '2018-10-13']); + $v->rules([ + 'date' => [ + ['created_at'] + ] + ]); + $this->assertTrue($v->validate()); + } + public function testDateValidWithDateTimeObject() { $v = new Validator(array('date' => new DateTime())); @@ -817,6 +1421,17 @@ public function testDateInvalid() $this->assertFalse($v->validate()); } + public function testDateInvalidAltSyntax() + { + $v = new Valitron\Validator(['created_at' => 'bananas']); + $v->rules([ + 'date' => [ + ['created_at'] + ] + ]); + $this->assertFalse($v->validate()); + } + /** * @group issue-13 */ @@ -834,6 +1449,17 @@ public function testDateFormatValid() $this->assertTrue($v->validate()); } + public function testDateFormatValidAltSyntax() + { + $v = new Valitron\Validator(['created_at' => '2018-10-13']); + $v->rules([ + 'dateFormat' => [ + ['created_at', 'Y-m-d'] + ] + ]); + $this->assertTrue($v->validate()); + } + public function testDateFormatInvalid() { $v = new Validator(array('date' => 'no thanks')); @@ -845,6 +1471,17 @@ public function testDateFormatInvalid() $this->assertFalse($v->validate()); } + public function testDateFormatInvalidAltSyntax() + { + $v = new Valitron\Validator(['created_at' => '10-13-2018']); + $v->rules([ + 'dateFormat' => [ + ['created_at', 'Y-m-d'] + ] + ]); + $this->assertFalse($v->validate()); + } + public function testDateBeforeValid() { $v = new Validator(array('date' => '2013-01-27')); @@ -852,6 +1489,17 @@ public function testDateBeforeValid() $this->assertTrue($v->validate()); } + public function testDateBeforeValidAltSyntax() + { + $v = new Valitron\Validator(['created_at' => '2018-09-01']); + $v->rules([ + 'dateBefore' => [ + ['created_at', '2018-10-13'] + ] + ]); + $this->assertTrue($v->validate()); + } + public function testDateWarningsWithObjectParams() { $v = new Validator(array('startDate' => '2013-01-27', 'endDate' => '2013-05-08')); @@ -885,6 +1533,17 @@ public function testDateBeforeInvalid() $this->assertFalse($v->validate()); } + public function testDateBeforeInvalidAltSyntax() + { + $v = new Valitron\Validator(['created_at' => '2018-11-01']); + $v->rules([ + 'dateBefore' => [ + ['created_at', '2018-10-13'] + ] + ]); + $this->assertFalse($v->validate()); + } + public function testDateAfterValid() { $v = new Validator(array('date' => '2013-01-27')); @@ -892,6 +1551,17 @@ public function testDateAfterValid() $this->assertTrue($v->validate()); } + public function testDateAfterValidAltSyntax() + { + $v = new Valitron\Validator(['created_at' => '2018-09-01']); + $v->rules([ + 'dateAfter' => [ + ['created_at', '2018-01-01'] + ] + ]); + $this->assertTrue($v->validate()); + } + public function testDateAfterInvalid() { $v = new Validator(array('date' => '2013-01-27')); @@ -899,6 +1569,17 @@ public function testDateAfterInvalid() $this->assertFalse($v->validate()); } + public function testDateAfterInvalidAltSyntax() + { + $v = new Valitron\Validator(['created_at' => '2017-09-01']); + $v->rules([ + 'dateAfter' => [ + ['created_at', '2018-01-01'] + ] + ]); + $this->assertFalse($v->validate()); + } + public function testContainsValid() { $v = new Validator(array('test_string' => 'this is a Test')); @@ -906,6 +1587,18 @@ public function testContainsValid() $this->assertTrue($v->validate()); } + public function testContainsValidAltSyntax() + { + $v = new Valitron\Validator(['username' => 'Batman123']); + $v->rules([ + 'contains' => [ + ['username', 'man'], + ['username', 'man', true] + ] + ]); + $this->assertTrue($v->validate()); + } + public function testContainsNonStrictValid() { $v = new Validator(array('test_string' => 'this is a Test')); @@ -920,6 +1613,17 @@ public function testContainsInvalid() $this->assertFalse($v->validate()); } + public function testContainsInvalidAltSyntax() + { + $v = new Valitron\Validator(['username' => 'Batman123']); + $v->rules([ + 'contains' => [ + ['username', 'Man', true] + ] + ]); + $this->assertFalse($v->validate()); + } + public function testContainsStrictInvalid() { $v = new Validator(array('test_string' => 'this is a Test')); @@ -964,6 +1668,17 @@ public function testSubsetValid() $this->assertTrue($v->validate()); } + public function testSubsetValidAltSyntax() + { + $v = new Valitron\Validator(['colors' => ['green', 'blue']]); + $v->rules([ + 'subset' => [ + ['colors', ['orange', 'green', 'blue', 'red']] + ] + ]); + $this->assertTrue($v->validate()); + } + public function testSubsetInvalid() { $v = new Validator(array('test_field' => array(81, false, 'orange'))); @@ -971,6 +1686,17 @@ public function testSubsetInvalid() $this->assertFalse($v->validate()); } + public function testSubsetInvalidAltSyntax() + { + $v = new Valitron\Validator(['colors' => ['purple', 'blue']]); + $v->rules([ + 'subset' => [ + ['colors', ['orange', 'green', 'blue', 'red']] + ] + ]); + $this->assertFalse($v->validate()); + } + public function testSubsetInvalidValue() { $v = new Validator(array('test_field' => 'black 45')); @@ -1009,6 +1735,17 @@ public function testContainsUniqueValid() $this->assertTrue($v->validate()); } + public function testContainsUniqueValidAltSyntax() + { + $v = new Valitron\Validator(['colors' => ['purple', 'blue']]); + $v->rules([ + 'containsUnique' => [ + ['colors'] + ] + ]); + $this->assertTrue($v->validate()); + } + public function testContainsUniqueInvalid() { $v = new Validator(array('test_field' => array(81, false, 'orange', false))); @@ -1016,6 +1753,17 @@ public function testContainsUniqueInvalid() $this->assertFalse($v->validate()); } + public function testContainsUniqueInvalidAltSyntax() + { + $v = new Valitron\Validator(['colors' => ['purple', 'purple']]); + $v->rules([ + 'containsUnique' => [ + ['colors'] + ] + ]); + $this->assertFalse($v->validate()); + } + public function testContainsUniqueInvalidValue() { $v = new Validator(array('test_field' => 'lorem ipsum')); @@ -1286,6 +2034,17 @@ public function testBooleanValid() $this->assertTrue($v->validate()); } + public function testBooleanValidAltSyntax() + { + $v = new Valitron\Validator(['remember_me' => true]); + $v->rules([ + 'boolean' => [ + ['remember_me'] + ] + ]); + $this->assertTrue($v->validate()); + } + public function testBooleanInvalid() { $v = new Validator(array('test' => 'true')); @@ -1293,6 +2052,17 @@ public function testBooleanInvalid() $this->assertFalse($v->validate()); } + public function testBooleanInvalidAltSyntax() + { + $v = new Valitron\Validator(['remember_me' => 'lobster']); + $v->rules([ + 'boolean' => [ + ['remember_me'] + ] + ]); + $this->assertFalse($v->validate()); + } + public function testZeroStillTriggersValidation() { $v = new Validator(array('test' => 0)); @@ -1366,6 +2136,19 @@ public function testInstanceOfValidWithString() $this->assertTrue($v->validate()); } + public function testInstanceOfValidAltSyntax() + { + $v = new Valitron\Validator(['date' => new \DateTime()]); + $existingDateObject = new \DateTime(); + $v->rules([ + 'instanceOf' => [ + ['date', 'DateTime'], + ['date', $existingDateObject] + ] + ]); + $this->assertTrue($v->validate()); + } + public function testInstanceOfInvalidWithInstance() { $v = new Validator(array('attributeName' => new stdClass())); @@ -1373,6 +2156,17 @@ public function testInstanceOfInvalidWithInstance() $this->assertFalse($v->validate()); } + public function testInstanceOfInvalidAltSyntax() + { + $v = new Valitron\Validator(['date' => new \DateTime()]); + $v->rules([ + 'instanceOf' => [ + ['date', 'stdClass'] + ] + ]); + $this->assertFalse($v->validate()); + } + public function testInstanceOfValidWithInstance() { $v = new Validator(array('attributeName' => new stdClass())); @@ -1459,6 +2253,20 @@ public function testOptionalProvidedValid() $this->assertTrue($v->validate()); } + public function testOptionalProvidedValidAltSyntax() + { + $v = new Valitron\Validator(['username' => 'batman']); + $v->rules([ + 'alpha' => [ + ['username'] + ], + 'optional' => [ + ['username'] + ] + ]); + $this->assertTrue($v->validate()); + } + public function testOptionalProvidedInvalid() { $v = new Validator(array('address' => 'userexample.com')); @@ -1466,6 +2274,27 @@ public function testOptionalProvidedInvalid() $this->assertFalse($v->validate()); } + public function testChainingRules() + { + $v = new Valitron\Validator(['email_address' => 'test@test.com']); + $v->rule('required', 'email_address')->rule('email', 'email_address'); + $this->assertTrue($v->validate()); + } + + public function testOptionalProvidedInvalidAltSyntax() + { + $v = new Valitron\Validator(['username' => 'batman123']); + $v->rules([ + 'alpha' => [ + ['username'] + ], + 'optional' => [ + ['username'] + ] + ]); + $this->assertFalse($v->validate()); + } + public function testOptionalNotProvided() { $v = new Validator(array()); From ad4ab084437c2a8c4a90a827f971eeb073d0ff25 Mon Sep 17 00:00:00 2001 From: Tom Breese Date: Sat, 13 Oct 2018 13:04:12 -0400 Subject: [PATCH 2/5] usage-docs; adding dot notation usage --- tests/Valitron/ValidateTest.php | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/tests/Valitron/ValidateTest.php b/tests/Valitron/ValidateTest.php index 8cbe9f7..1ff1161 100644 --- a/tests/Valitron/ValidateTest.php +++ b/tests/Valitron/ValidateTest.php @@ -2281,17 +2281,24 @@ public function testChainingRules() $this->assertTrue($v->validate()); } + public function testNestedDotNotation() + { + $v = new Valitron\Validator(array('user' => array('first_name' => 'Steve', 'last_name' => 'Smith', 'username' => 'Batman123'))); + $v->rule('alpha', 'user.first_name')->rule('alpha', 'user.last_name')->rule('alphaNum', 'user.username'); + $this->assertTrue($v->validate()); + } + public function testOptionalProvidedInvalidAltSyntax() { $v = new Valitron\Validator(['username' => 'batman123']); - $v->rules([ - 'alpha' => [ - ['username'] - ], - 'optional' => [ - ['username'] - ] - ]); + $v->rules(array( + 'alpha' => array( + array('username') + ), + 'optional' => array( + array('username') + ) + )); $this->assertFalse($v->validate()); } From ac5148995e3c9cb5857b034800fbe29205cb102d Mon Sep 17 00:00:00 2001 From: Tom Breese Date: Sat, 13 Oct 2018 13:06:00 -0400 Subject: [PATCH 3/5] usage-docs; adding dot notation usage --- README.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/README.md b/README.md index a7d2344..9eb0f82 100644 --- a/README.md +++ b/README.md @@ -99,6 +99,19 @@ if($v->validate()) { } ``` +You can also access nested values using dot notation: + +```php +$v = new Valitron\Validator(array('user' => array('first_name' => 'Steve', 'last_name' => 'Smith', 'username' => 'Batman123'))); +$v->rule('alpha', 'user.first_name')->rule('alpha', 'user.last_name')->rule('alphaNum', 'user.username'); +if($v->validate()) { + echo "Yay! We're all good!"; +} else { + // Errors + print_r($v->errors()); +} +``` + Setting language and language dir globally: ```php From 4b60befc618d27fe07b6e052e917543b44814a0b Mon Sep 17 00:00:00 2001 From: Tom Breese Date: Sat, 13 Oct 2018 13:24:43 -0400 Subject: [PATCH 4/5] usage-docs; updating arrays to php 5.3 compatible syntax --- tests/Valitron/ValidateTest.php | 904 ++++++++++++++++---------------- 1 file changed, 453 insertions(+), 451 deletions(-) diff --git a/tests/Valitron/ValidateTest.php b/tests/Valitron/ValidateTest.php index 1ff1161..54ad706 100644 --- a/tests/Valitron/ValidateTest.php +++ b/tests/Valitron/ValidateTest.php @@ -53,14 +53,14 @@ public function testRequiredValid() public function testRequiredValidAltSyntax() { - $v = new Valitron\Validator(['username' => 'spiderman', 'password' => 'Gr33nG0Blin', 'required_but_null' => null]); - $v->rules([ - 'required' => [ - ['username'], - ['password'], - ['required_but_null', true] // boolean flag allows empty value so long as the field name is set on the data array - ] - ]); + $v = new Valitron\Validator(array('username' => 'spiderman', 'password' => 'Gr33nG0Blin', 'required_but_null' => null)); + $v->rules(array( + 'required' => array( + array('username'), + array('password'), + array('required_but_null', true) // boolean flag allows empty value so long as the field name is set on the data array + ) + )); $this->assertTrue($v->validate()); } @@ -73,13 +73,13 @@ public function testRequiredNonExistentField() public function testRequiredNonExistentFieldAltSyntax() { - $v = new Valitron\Validator(['boozername' => 'spiderman', 'notPassword' => 'Gr33nG0Blin']); - $v->rules([ - 'required' => [ - ['username'], - ['password'] - ] - ]); + $v = new Valitron\Validator(array('boozername' => 'spiderman', 'notPassword' => 'Gr33nG0Blin')); + $v->rules(array( + 'required' => array( + array('username'), + array('password') + ) + )); $this->assertFalse($v->validate()); } @@ -92,12 +92,12 @@ public function testEqualsValid() public function testEqualsValidAltSyntax() { - $v = new Validator(['password' => 'youshouldnotseethis', 'confirmPassword' => 'youshouldnotseethis']); - $v->rules([ - 'equals' => [ - ['password', 'confirmPassword'] - ] - ]); + $v = new Validator(array('password' => 'youshouldnotseethis', 'confirmPassword' => 'youshouldnotseethis')); + $v->rules(array( + 'equals' => array( + array('password', 'confirmPassword') + ) + )); $this->assertTrue($v->validate()); } @@ -110,12 +110,12 @@ public function testEqualsInvalid() public function testEqualsInvalidAltSyntax() { - $v = new Validator(['password' => 'youshouldnotseethis', 'confirmPassword' => 'differentpassword']); - $v->rules([ - 'equals' => [ - ['password', 'confirmPassword'] - ] - ]); + $v = new Validator(array('password' => 'youshouldnotseethis', 'confirmPassword' => 'differentpassword')); + $v->rules(array( + 'equals' => array( + array('password', 'confirmPassword') + ) + )); } public function testEqualsBothNull() @@ -157,12 +157,12 @@ public function testDifferentValid() public function testDifferentValidAltSyntax() { - $v = new Validator(['username' => 'test', 'password' => 'test123']); - $v->rules([ - 'different' => [ - ['username', 'password'] - ] - ]); + $v = new Validator(array('username' => 'test', 'password' => 'test123')); + $v->rules(array( + 'different' => array( + array('username', 'password') + ) + )); $this->assertTrue($v->validate()); } @@ -175,12 +175,13 @@ public function testDifferentInvalid() public function testDifferentInvalidAltSyntax() { - $v = new Validator(['username' => 'test', 'password' => 'test']); - $v->rules([ - 'different' => [ - ['username', 'password'] - ] - ]); + $v = new Validator(array('username' => 'test', 'password' => 'test')); + $v->rules(array( + 'different' => array( + array('username', 'password') + ) + )); + $this->assertFalse($v->validate()); } public function testDifferentBothNull() @@ -222,12 +223,12 @@ public function testAcceptedValid() public function testAcceptedValidAltSyntax() { - $v = new Valitron\Validator(['remember_me' => true]); - $v->rules([ - 'accepted' => [ - ['remember_me'] - ] - ]); + $v = new Valitron\Validator(array('remember_me' => true)); + $v->rules(array( + 'accepted' => array( + array('remember_me') + ) + )); $this->assertTrue($v->validate()); } @@ -240,16 +241,17 @@ public function testAcceptedInvalid() public function testAcceptedInvalidAltSyntax() { - $v = new Valitron\Validator(['remember_me' => false]); - $v->rules([ - 'accepted' => [ - ['remember_me'] - ] - ]); + $v = new Valitron\Validator(array('remember_me' => false)); + $v->rules(array( + 'accepted' => array( + array('remember_me') + ) + )); $this->assertFalse($v->validate()); } - public function testAcceptedNotSet(){ + public function testAcceptedNotSet() + { $v = new Validator(); $v->rule('accepted', 'agree'); $this->assertFalse($v->validate()); @@ -264,12 +266,12 @@ public function testNumericValid() public function testNumericValidAltSyntax() { - $v = new Valitron\Validator(['amount' => 3.14]); - $v->rules([ - 'numeric' => [ - ['amount'] - ] - ]); + $v = new Valitron\Validator(array('amount' => 3.14)); + $v->rules(array( + 'numeric' => array( + array('amount') + ) + )); $this->assertTrue($v->validate()); } @@ -282,12 +284,12 @@ public function testNumericInvalid() public function testNumericInvalidAltSyntax() { - $v = new Valitron\Validator(['amount' => 'banana']); - $v->rules([ - 'numeric' => [ - ['amount'] - ] - ]); + $v = new Valitron\Validator(array('amount' => 'banana')); + $v->rules(array( + 'numeric' => array( + array('amount') + ) + )); $this->assertFalse($v->validate()); } @@ -304,12 +306,12 @@ public function testIntegerValid() public function testIntegerValidAltSyntax() { - $v = new Valitron\Validator(['age' => 27]); - $v->rules([ - 'integer' => [ - ['age', true] - ] - ]); + $v = new Valitron\Validator(array('age' => 27)); + $v->rules(array( + 'integer' => array( + array('age', true) + ) + )); $this->assertTrue($v->validate()); } @@ -378,12 +380,12 @@ public function testIntegerInvalid() public function testIntegerInvalidAltSyntax() { - $v = new Valitron\Validator(['age' => 3.14]); - $v->rules([ - 'integer' => [ - ['age'] - ] - ]); + $v = new Valitron\Validator(array('age' => 3.14)); + $v->rules(array( + 'integer' => array( + array('age') + ) + )); $this->assertFalse($v->validate()); } @@ -396,12 +398,12 @@ public function testLengthValid() public function testLengthValidAltSyntax() { - $v = new Valitron\Validator(['username' => 'bobburgers']); - $v->rules([ - 'length' => [ - ['username', 10] - ] - ]); + $v = new Valitron\Validator(array('username' => 'bobburgers')); + $v->rules(array( + 'length' => array( + array('username', 10) + ) + )); $this->assertTrue($v->validate()); } @@ -422,12 +424,12 @@ public function testLengthInvalid() public function testLengthInvalidAltSyntax() { - $v = new Valitron\Validator(['username' => 'hi']); - $v->rules([ - 'length' => [ - ['username', 10] - ] - ]); + $v = new Valitron\Validator(array('username' => 'hi')); + $v->rules(array( + 'length' => array( + array('username', 10) + ) + )); $this->assertFalse($v->validate()); } @@ -440,12 +442,12 @@ public function testLengthBetweenValid() public function testLengthBetweenValidAltSyntax() { - $v = new Valitron\Validator(['username' => 'bobburgers']); - $v->rules([ - 'lengthBetween' => [ - ['username', 1, 10] - ] - ]); + $v = new Valitron\Validator(array('username' => 'bobburgers')); + $v->rules(array( + 'lengthBetween' => array( + array('username', 1, 10) + ) + )); $this->assertTrue($v->validate()); } @@ -466,12 +468,12 @@ public function testLengthBetweenInvalid() public function testLengthBetweenInvalidAltSyntax() { - $v = new Valitron\Validator(['username' => 'hi']); - $v->rules([ - 'lengthBetween' => [ - ['username', 3, 10] - ] - ]); + $v = new Valitron\Validator(array('username' => 'hi')); + $v->rules(array( + 'lengthBetween' => array( + array('username', 3, 10) + ) + )); $this->assertFalse($v->validate()); } @@ -484,12 +486,12 @@ public function testLengthMinValid() public function testLengthMinValidAltSyntax() { - $v = new Valitron\Validator(['username' => 'martha']); - $v->rules([ - 'lengthMin' => [ - ['username', 5] - ] - ]); + $v = new Valitron\Validator(array('username' => 'martha')); + $v->rules(array( + 'lengthMin' => array( + array('username', 5) + ) + )); $this->assertTrue($v->validate()); } @@ -502,12 +504,12 @@ public function testLengthMinInvalid() public function testLengthMinInvalidAltSyntax() { - $v = new Valitron\Validator(['username' => 'abc']); - $v->rules([ - 'lengthMin' => [ - ['username', 5] - ] - ]); + $v = new Valitron\Validator(array('username' => 'abc')); + $v->rules(array( + 'lengthMin' => array( + array('username', 5) + ) + )); $this->assertFalse($v->validate()); } @@ -520,12 +522,12 @@ public function testLengthMaxValid() public function testLengthMaxValidAltSyntax() { - $v = new Valitron\Validator(['username' => 'bruins91']); - $v->rules([ - 'lengthMax' => [ - ['username', 10] - ] - ]); + $v = new Valitron\Validator(array('username' => 'bruins91')); + $v->rules(array( + 'lengthMax' => array( + array('username', 10) + ) + )); $this->assertTrue($v->validate()); } @@ -538,12 +540,12 @@ public function testLengthMaxInvalid() public function testLengthMaxInvalidAltSyntax() { - $v = new Valitron\Validator(['username' => 'bruins91']); - $v->rules([ - 'lengthMax' => [ - ['username', 3] - ] - ]); + $v = new Valitron\Validator(array('username' => 'bruins91')); + $v->rules(array( + 'lengthMax' => array( + array('username', 3) + ) + )); $this->assertFalse($v->validate()); } @@ -560,12 +562,12 @@ public function testMinValid() public function testMinValidAltSyntax() { - $v = new Valitron\Validator(['age' => 28]); - $v->rules([ - 'min' => [ - ['age', 18] - ] - ]); + $v = new Valitron\Validator(array('age' => 28)); + $v->rules(array( + 'min' => array( + array('age', 18) + ) + )); $this->assertTrue($v->validate()); } @@ -601,12 +603,12 @@ public function testMinInvalid() public function testMinInvalidAltSyntax() { - $v = new Valitron\Validator(['age' => 16]); - $v->rules([ - 'min' => [ - ['age', 18] - ] - ]); + $v = new Valitron\Validator(array('age' => 16)); + $v->rules(array( + 'min' => array( + array('age', 18) + ) + )); $this->assertFalse($v->validate()); } @@ -630,12 +632,12 @@ public function testMaxValid() public function testMaxValidAltSyntax() { - $v = new Valitron\Validator(['age' => 10]); - $v->rules([ - 'max' => [ - ['age', 12] - ] - ]); + $v = new Valitron\Validator(array('age' => 10)); + $v->rules(array( + 'max' => array( + array('age', 12) + ) + )); $this->assertTrue($v->validate()); } @@ -671,12 +673,12 @@ public function testMaxInvalid() public function testMaxInvalidAltSyntax() { - $v = new Valitron\Validator(['age' => 29]); - $v->rules([ - 'max' => [ - ['age', 12] - ] - ]); + $v = new Valitron\Validator(array('age' => 29)); + $v->rules(array( + 'max' => array( + array('age', 12) + ) + )); $this->assertFalse($v->validate()); } @@ -732,23 +734,23 @@ public function testInValid() public function testInValidAltSyntax() { - $v = new Valitron\Validator(['color' => 'purple']); - $v->rules([ - 'in' => [ - ['color', ['blue', 'green', 'red', 'purple']] - ] - ]); + $v = new Valitron\Validator(array('color' => 'purple')); + $v->rules(array( + 'in' => array( + array('color', array('blue', 'green', 'red', 'purple')) + ) + )); $this->assertTrue($v->validate()); } public function testInInvalidAltSyntax() { - $v = new Valitron\Validator(['color' => 'orange']); - $v->rules([ - 'in' => [ - ['color', ['blue', 'green', 'red', 'purple']] - ] - ]); + $v = new Valitron\Validator(array('color' => 'orange')); + $v->rules(array( + 'in' => array( + array('color', array('blue', 'green', 'red', 'purple')) + ) + )); $this->assertFalse($v->validate()); } @@ -779,12 +781,12 @@ public function testArrayValid() public function testArrayValidAltSyntax() { - $v = new Valitron\Validator(['user_notifications' => ['bulletin_notifications' => true, 'marketing_notifications' => false, 'message_notification' => true]]); - $v->rules([ - 'array' => [ - ['user_notifications'] - ] - ]); + $v = new Valitron\Validator(array('user_notifications' => array('bulletin_notifications' => true, 'marketing_notifications' => false, 'message_notification' => true))); + $v->rules(array( + 'array' => array( + array('user_notifications') + ) + )); $this->assertTrue($v->validate()); } @@ -797,12 +799,12 @@ public function testAssocArrayValid() public function testArrayInvalidAltSyntax() { - $v = new Valitron\Validator(['user_notifications' => 'string']); - $v->rules([ - 'array' => [ - ['user_notifications'] - ] - ]); + $v = new Valitron\Validator(array('user_notifications' => 'string')); + $v->rules(array( + 'array' => array( + array('user_notifications') + ) + )); $this->assertFalse($v->validate()); } @@ -924,12 +926,12 @@ public function testNotInValid() public function testNotInValidAltSyntax() { - $v = new Valitron\Validator(['color' => 'purple']); - $v->rules([ - 'notIn' => [ - ['color', ['blue', 'green', 'red', 'yellow']] - ] - ]); + $v = new Valitron\Validator(array('color' => 'purple')); + $v->rules(array( + 'notIn' => array( + array('color', array('blue', 'green', 'red', 'yellow')) + ) + )); $this->assertTrue($v->validate()); } @@ -942,12 +944,12 @@ public function testNotInInvalid() public function testNotInInvalidAltSyntax() { - $v = new Valitron\Validator(['color' => 'yellow']); - $v->rules([ - 'notIn' => [ - ['color', ['blue', 'green', 'red', 'yellow']] - ] - ]); + $v = new Valitron\Validator(array('color' => 'yellow')); + $v->rules(array( + 'notIn' => array( + array('color', array('blue', 'green', 'red', 'yellow')) + ) + )); } public function testAsciiValid() @@ -959,12 +961,12 @@ public function testAsciiValid() public function testAsciiValidAltSyntax() { - $v = new Valitron\Validator(['username' => 'batman123']); - $v->rules([ - 'ascii' => [ - ['username'] - ] - ]); + $v = new Valitron\Validator(array('username' => 'batman123')); + $v->rules(array( + 'ascii' => array( + array('username') + ) + )); $this->assertTrue($v->validate()); } @@ -977,12 +979,12 @@ public function testAsciiInvalid() public function testAsciiInvalidAltSyntax() { - $v = new Valitron\Validator(['username' => '12345 abcdé']); - $v->rules([ - 'ascii' => [ - ['username'] - ] - ]); + $v = new Valitron\Validator(array('username' => '12345 abcdé')); + $v->rules(array( + 'ascii' => array( + array('username') + ) + )); $this->assertFalse($v->validate()); } @@ -995,12 +997,12 @@ public function testIpValid() public function testIpValidAltSyntax() { - $v = new Valitron\Validator(['user_ip' => '127.0.0.1']); - $v->rules([ - 'ip' => [ - ['user_ip'] - ] - ]); + $v = new Valitron\Validator(array('user_ip' => '127.0.0.1')); + $v->rules(array( + 'ip' => array( + array('user_ip') + ) + )); $this->assertTrue($v->validate()); } @@ -1013,12 +1015,12 @@ public function testIpInvalid() public function testIpInvalidAltSyntax() { - $v = new Valitron\Validator(['user_ip' => '127.0.0.1.345']); - $v->rules([ - 'ip' => [ - ['user_ip'] - ] - ]); + $v = new Valitron\Validator(array('user_ip' => '127.0.0.1.345')); + $v->rules(array( + 'ip' => array( + array('user_ip') + ) + )); $this->assertFalse($v->validate()); } @@ -1031,12 +1033,12 @@ public function testIpv4Valid() public function testIpv4ValidAltSyntax() { - $v = new Valitron\Validator(['user_ip' => '127.0.0.1']); - $v->rules([ - 'ipv4' => [ - ['user_ip'] - ] - ]); + $v = new Valitron\Validator(array('user_ip' => '127.0.0.1')); + $v->rules(array( + 'ipv4' => array( + array('user_ip') + ) + )); $this->assertTrue($v->validate()); } @@ -1049,12 +1051,12 @@ public function testIpv4Invalid() public function testIpv4InvalidAltSyntax() { - $v = new Valitron\Validator(['user_ip' => '127.0.0.1.234']); - $v->rules([ - 'ipv4' => [ - ['user_ip'] - ] - ]); + $v = new Valitron\Validator(array('user_ip' => '127.0.0.1.234')); + $v->rules(array( + 'ipv4' => array( + array('user_ip') + ) + )); $this->assertFalse($v->validate()); } @@ -1067,12 +1069,12 @@ public function testIpv6Valid() public function testIpv6ValidAltSyntax() { - $v = new Valitron\Validator(['user_ipv6' => '0:0:0:0:0:0:0:1']); - $v->rules([ - 'ipv6' => [ - ['user_ipv6'] - ] - ]); + $v = new Valitron\Validator(array('user_ipv6' => '0:0:0:0:0:0:0:1')); + $v->rules(array( + 'ipv6' => array( + array('user_ipv6') + ) + )); $this->assertTrue($v->validate()); } @@ -1085,12 +1087,12 @@ public function testIpv6Invalid() public function testIpv6InvalidAltSyntax() { - $v = new Valitron\Validator(['user_ipv6' => '0:0:0:0:0:0:0:1:3:4:5']); - $v->rules([ - 'ipv6' => [ - ['user_ipv6'] - ] - ]); + $v = new Valitron\Validator(array('user_ipv6' => '0:0:0:0:0:0:0:1:3:4:5')); + $v->rules(array( + 'ipv6' => array( + array('user_ipv6') + ) + )); $this->assertFalse($v->validate()); } @@ -1103,12 +1105,12 @@ public function testEmailValid() public function testEmailValidAltSyntax() { - $v = new Valitron\Validator(['user_email' => 'someone@example.com']); - $v->rules([ - 'email' => [ - ['user_email'] - ] - ]); + $v = new Valitron\Validator(array('user_email' => 'someone@example.com')); + $v->rules(array( + 'email' => array( + array('user_email') + ) + )); $this->assertTrue($v->validate()); } @@ -1121,12 +1123,12 @@ public function testEmailInvalid() public function testEmailInvalidAltSyntax() { - $v = new Valitron\Validator(['user_email' => 'example.com']); - $v->rules([ - 'email' => [ - ['user_email'] - ] - ]); + $v = new Valitron\Validator(array('user_email' => 'example.com')); + $v->rules(array( + 'email' => array( + array('user_email') + ) + )); $this->assertFalse($v->validate()); } @@ -1139,12 +1141,12 @@ public function testEmailDnsValid() public function testEmailDnsValidAltSyntax() { - $v = new Valitron\Validator(['user_email' => 'some_fake_email_address@gmail.com']); - $v->rules([ - 'emailDNS' => [ - ['user_email'] - ] - ]); + $v = new Valitron\Validator(array('user_email' => 'some_fake_email_address@gmail.com')); + $v->rules(array( + 'emailDNS' => array( + array('user_email') + ) + )); $this->assertTrue($v->validate()); } @@ -1157,12 +1159,12 @@ public function testEmailDnsInvalid() public function testEmailDnsInvalidAltSyntax() { - $v = new Valitron\Validator(['user_email' => 'some_fake_email_address@gmail.zyx']); - $v->rules([ - 'emailDNS' => [ - ['user_email'] - ] - ]); + $v = new Valitron\Validator(array('user_email' => 'some_fake_email_address@gmail.zyx')); + $v->rules(array( + 'emailDNS' => array( + array('user_email') + ) + )); $this->assertFalse($v->validate()); } @@ -1175,12 +1177,12 @@ public function testUrlValid() public function testUrlValidAltSyntax() { - $v = new Valitron\Validator(['website' => 'https://example.com/contact']); - $v->rules([ - 'url' => [ - ['website'] - ] - ]); + $v = new Valitron\Validator(array('website' => 'https://example.com/contact')); + $v->rules(array( + 'url' => array( + array('website') + ) + )); $this->assertTrue($v->validate()); } @@ -1193,12 +1195,12 @@ public function testUrlInvalid() public function testUrlInvalidAltSyntax() { - $v = new Valitron\Validator(['website' => 'thisisjusttext']); - $v->rules([ - 'url' => [ - ['website'] - ] - ]); + $v = new Valitron\Validator(array('website' => 'thisisjusttext')); + $v->rules(array( + 'url' => array( + array('website') + ) + )); $this->assertFalse($v->validate()); } @@ -1211,12 +1213,12 @@ public function testUrlActive() public function testUrlActiveValidAltSyntax() { - $v = new Valitron\Validator(['website' => 'https://example.com/contact']); - $v->rules([ - 'urlActive' => [ - ['website'] - ] - ]); + $v = new Valitron\Validator(array('website' => 'https://example.com/contact')); + $v->rules(array( + 'urlActive' => array( + array('website') + ) + )); $this->assertTrue($v->validate()); } @@ -1229,12 +1231,12 @@ public function testUrlInactive() public function testUrlActiveInvalidAltSyntax() { - $v = new Valitron\Validator(['website' => 'https://example-domain']); - $v->rules([ - 'urlActive' => [ - ['website'] - ] - ]); + $v = new Valitron\Validator(array('website' => 'https://example-domain')); + $v->rules(array( + 'urlActive' => array( + array('website') + ) + )); $this->assertFalse($v->validate()); } @@ -1247,12 +1249,12 @@ public function testAlphaValid() public function testAlphaValidAltSyntax() { - $v = new Valitron\Validator(['username' => 'batman']); - $v->rules([ - 'alpha' => [ - ['username'] - ] - ]); + $v = new Valitron\Validator(array('username' => 'batman')); + $v->rules(array( + 'alpha' => array( + array('username') + ) + )); $this->assertTrue($v->validate()); } @@ -1265,12 +1267,12 @@ public function testAlphaInvalid() public function testAlphaInvalidAltSyntax() { - $v = new Valitron\Validator(['username' => '123456asdf']); - $v->rules([ - 'alpha' => [ - ['username'] - ] - ]); + $v = new Valitron\Validator(array('username' => '123456asdf')); + $v->rules(array( + 'alpha' => array( + array('username') + ) + )); $this->assertFalse($v->validate()); } @@ -1283,12 +1285,12 @@ public function testAlphaNumValid() public function testAlphaNumValidAltSyntax() { - $v = new Valitron\Validator(['username' => 'batman123']); - $v->rules([ - 'alphaNum' => [ - ['username'] - ] - ]); + $v = new Valitron\Validator(array('username' => 'batman123')); + $v->rules(array( + 'alphaNum' => array( + array('username') + ) + )); $this->assertTrue($v->validate()); } @@ -1301,12 +1303,12 @@ public function testAlphaNumInvalid() public function testAlphaNumInvalidAltSyntax() { - $v = new Valitron\Validator(['username' => 'batman123-$']); - $v->rules([ - 'alphaNum' => [ - ['username'] - ] - ]); + $v = new Valitron\Validator(array('username' => 'batman123-$')); + $v->rules(array( + 'alphaNum' => array( + array('username') + ) + )); $this->assertFalse($v->validate()); } @@ -1319,12 +1321,12 @@ public function testAlphaDashValid() public function testSlugValidAltSyntax() { - $v = new Valitron\Validator(['username' => 'L337-H4ckZ0rz_123']); - $v->rules([ - 'slug' => [ - ['username'] - ] - ]); + $v = new Valitron\Validator(array('username' => 'L337-H4ckZ0rz_123')); + $v->rules(array( + 'slug' => array( + array('username') + ) + )); $this->assertTrue($v->validate()); } @@ -1337,12 +1339,12 @@ public function testAlphaDashInvalid() public function testSlugInvalidAltSyntax() { - $v = new Valitron\Validator(['username' => 'L337-H4ckZ0rz_123 $%^']); - $v->rules([ - 'slug' => [ - ['username'] - ] - ]); + $v = new Valitron\Validator(array('username' => 'L337-H4ckZ0rz_123 $%^')); + $v->rules(array( + 'slug' => array( + array('username') + ) + )); $this->assertFalse($v->validate()); } @@ -1356,36 +1358,36 @@ public function testNoErrorFailOnArray() public function testRegexValid() { $v = new Validator(array('test' => '42')); - $v->rule('regex', 'test', '/[\d]+/'); + $v->rule('regex', 'test', '/array(\d)+/'); $this->assertTrue($v->validate()); } public function testRegexValidAltSyntax() { - $v = new Valitron\Validator(['username' => 'Batman123']); - $v->rules([ - 'regex' => [ - ['username', '/^[a-zA-Z0-9]{5,10}$/'] - ] - ]); + $v = new Valitron\Validator(array('username' => 'Batman123')); + $v->rules(array( + 'regex' => array( + array('username', '/^array(a-zA-Z0-9){5,10}$/') + ) + )); $this->assertTrue($v->validate()); } public function testRegexInvalid() { $v = new Validator(array('test' => 'istheanswer')); - $v->rule('regex', 'test', '/[\d]+/'); + $v->rule('regex', 'test', '/array(\d)+/'); $this->assertFalse($v->validate()); } public function testRegexInvalidAltSyntax() { - $v = new Valitron\Validator(['username' => 'Batman_123']); - $v->rules([ - 'regex' => [ - ['username', '/^[a-zA-Z0-9]{5,10}$/'] - ] - ]); + $v = new Valitron\Validator(array('username' => 'Batman_123')); + $v->rules(array( + 'regex' => array( + array('username', '/^array(a-zA-Z0-9){5,10}$/') + ) + )); $this->assertFalse($v->validate()); } @@ -1398,12 +1400,12 @@ public function testDateValid() public function testDateValidAltSyntax() { - $v = new Valitron\Validator(['created_at' => '2018-10-13']); - $v->rules([ - 'date' => [ - ['created_at'] - ] - ]); + $v = new Valitron\Validator(array('created_at' => '2018-10-13')); + $v->rules(array( + 'date' => array( + array('created_at') + ) + )); $this->assertTrue($v->validate()); } @@ -1423,12 +1425,12 @@ public function testDateInvalid() public function testDateInvalidAltSyntax() { - $v = new Valitron\Validator(['created_at' => 'bananas']); - $v->rules([ - 'date' => [ - ['created_at'] - ] - ]); + $v = new Valitron\Validator(array('created_at' => 'bananas')); + $v->rules(array( + 'date' => array( + array('created_at') + ) + )); $this->assertFalse($v->validate()); } @@ -1451,12 +1453,12 @@ public function testDateFormatValid() public function testDateFormatValidAltSyntax() { - $v = new Valitron\Validator(['created_at' => '2018-10-13']); - $v->rules([ - 'dateFormat' => [ - ['created_at', 'Y-m-d'] - ] - ]); + $v = new Valitron\Validator(array('created_at' => '2018-10-13')); + $v->rules(array( + 'dateFormat' => array( + array('created_at', 'Y-m-d') + ) + )); $this->assertTrue($v->validate()); } @@ -1473,12 +1475,12 @@ public function testDateFormatInvalid() public function testDateFormatInvalidAltSyntax() { - $v = new Valitron\Validator(['created_at' => '10-13-2018']); - $v->rules([ - 'dateFormat' => [ - ['created_at', 'Y-m-d'] - ] - ]); + $v = new Valitron\Validator(array('created_at' => '10-13-2018')); + $v->rules(array( + 'dateFormat' => array( + array('created_at', 'Y-m-d') + ) + )); $this->assertFalse($v->validate()); } @@ -1491,12 +1493,12 @@ public function testDateBeforeValid() public function testDateBeforeValidAltSyntax() { - $v = new Valitron\Validator(['created_at' => '2018-09-01']); - $v->rules([ - 'dateBefore' => [ - ['created_at', '2018-10-13'] - ] - ]); + $v = new Valitron\Validator(array('created_at' => '2018-09-01')); + $v->rules(array( + 'dateBefore' => array( + array('created_at', '2018-10-13') + ) + )); $this->assertTrue($v->validate()); } @@ -1535,12 +1537,12 @@ public function testDateBeforeInvalid() public function testDateBeforeInvalidAltSyntax() { - $v = new Valitron\Validator(['created_at' => '2018-11-01']); - $v->rules([ - 'dateBefore' => [ - ['created_at', '2018-10-13'] - ] - ]); + $v = new Valitron\Validator(array('created_at' => '2018-11-01')); + $v->rules(array( + 'dateBefore' => array( + array('created_at', '2018-10-13') + ) + )); $this->assertFalse($v->validate()); } @@ -1553,12 +1555,12 @@ public function testDateAfterValid() public function testDateAfterValidAltSyntax() { - $v = new Valitron\Validator(['created_at' => '2018-09-01']); - $v->rules([ - 'dateAfter' => [ - ['created_at', '2018-01-01'] - ] - ]); + $v = new Valitron\Validator(array('created_at' => '2018-09-01')); + $v->rules(array( + 'dateAfter' => array( + array('created_at', '2018-01-01') + ) + )); $this->assertTrue($v->validate()); } @@ -1571,12 +1573,12 @@ public function testDateAfterInvalid() public function testDateAfterInvalidAltSyntax() { - $v = new Valitron\Validator(['created_at' => '2017-09-01']); - $v->rules([ - 'dateAfter' => [ - ['created_at', '2018-01-01'] - ] - ]); + $v = new Valitron\Validator(array('created_at' => '2017-09-01')); + $v->rules(array( + 'dateAfter' => array( + array('created_at', '2018-01-01') + ) + )); $this->assertFalse($v->validate()); } @@ -1589,13 +1591,13 @@ public function testContainsValid() public function testContainsValidAltSyntax() { - $v = new Valitron\Validator(['username' => 'Batman123']); - $v->rules([ - 'contains' => [ - ['username', 'man'], - ['username', 'man', true] - ] - ]); + $v = new Valitron\Validator(array('username' => 'Batman123')); + $v->rules(array( + 'contains' => array( + array('username', 'man'), + array('username', 'man', true) + ) + )); $this->assertTrue($v->validate()); } @@ -1615,12 +1617,12 @@ public function testContainsInvalid() public function testContainsInvalidAltSyntax() { - $v = new Valitron\Validator(['username' => 'Batman123']); - $v->rules([ - 'contains' => [ - ['username', 'Man', true] - ] - ]); + $v = new Valitron\Validator(array('username' => 'Batman123')); + $v->rules(array( + 'contains' => array( + array('username', 'Man', true) + ) + )); $this->assertFalse($v->validate()); } @@ -1670,12 +1672,12 @@ public function testSubsetValid() public function testSubsetValidAltSyntax() { - $v = new Valitron\Validator(['colors' => ['green', 'blue']]); - $v->rules([ - 'subset' => [ - ['colors', ['orange', 'green', 'blue', 'red']] - ] - ]); + $v = new Valitron\Validator(array('colors' => array('green', 'blue'))); + $v->rules(array( + 'subset' => array( + array('colors', array('orange', 'green', 'blue', 'red')) + ) + )); $this->assertTrue($v->validate()); } @@ -1688,12 +1690,12 @@ public function testSubsetInvalid() public function testSubsetInvalidAltSyntax() { - $v = new Valitron\Validator(['colors' => ['purple', 'blue']]); - $v->rules([ - 'subset' => [ - ['colors', ['orange', 'green', 'blue', 'red']] - ] - ]); + $v = new Valitron\Validator(array('colors' => array('purple', 'blue'))); + $v->rules(array( + 'subset' => array( + array('colors', array('orange', 'green', 'blue', 'red')) + ) + )); $this->assertFalse($v->validate()); } @@ -1737,12 +1739,12 @@ public function testContainsUniqueValid() public function testContainsUniqueValidAltSyntax() { - $v = new Valitron\Validator(['colors' => ['purple', 'blue']]); - $v->rules([ - 'containsUnique' => [ - ['colors'] - ] - ]); + $v = new Valitron\Validator(array('colors' => array('purple', 'blue'))); + $v->rules(array( + 'containsUnique' => array( + array('colors') + ) + )); $this->assertTrue($v->validate()); } @@ -1755,12 +1757,12 @@ public function testContainsUniqueInvalid() public function testContainsUniqueInvalidAltSyntax() { - $v = new Valitron\Validator(['colors' => ['purple', 'purple']]); - $v->rules([ - 'containsUnique' => [ - ['colors'] - ] - ]); + $v = new Valitron\Validator(array('colors' => array('purple', 'purple'))); + $v->rules(array( + 'containsUnique' => array( + array('colors') + ) + )); $this->assertFalse($v->validate()); } @@ -2036,12 +2038,12 @@ public function testBooleanValid() public function testBooleanValidAltSyntax() { - $v = new Valitron\Validator(['remember_me' => true]); - $v->rules([ - 'boolean' => [ - ['remember_me'] - ] - ]); + $v = new Valitron\Validator(array('remember_me' => true)); + $v->rules(array( + 'boolean' => array( + array('remember_me') + ) + )); $this->assertTrue($v->validate()); } @@ -2054,12 +2056,12 @@ public function testBooleanInvalid() public function testBooleanInvalidAltSyntax() { - $v = new Valitron\Validator(['remember_me' => 'lobster']); - $v->rules([ - 'boolean' => [ - ['remember_me'] - ] - ]); + $v = new Valitron\Validator(array('remember_me' => 'lobster')); + $v->rules(array( + 'boolean' => array( + array('remember_me') + ) + )); $this->assertFalse($v->validate()); } @@ -2138,14 +2140,14 @@ public function testInstanceOfValidWithString() public function testInstanceOfValidAltSyntax() { - $v = new Valitron\Validator(['date' => new \DateTime()]); + $v = new Valitron\Validator(array('date' => new \DateTime())); $existingDateObject = new \DateTime(); - $v->rules([ - 'instanceOf' => [ - ['date', 'DateTime'], - ['date', $existingDateObject] - ] - ]); + $v->rules(array( + 'instanceOf' => array( + array('date', 'DateTime'), + array('date', $existingDateObject) + ) + )); $this->assertTrue($v->validate()); } @@ -2158,12 +2160,12 @@ public function testInstanceOfInvalidWithInstance() public function testInstanceOfInvalidAltSyntax() { - $v = new Valitron\Validator(['date' => new \DateTime()]); - $v->rules([ - 'instanceOf' => [ - ['date', 'stdClass'] - ] - ]); + $v = new Valitron\Validator(array('date' => new \DateTime())); + $v->rules(array( + 'instanceOf' => array( + array('date', 'stdClass') + ) + )); $this->assertFalse($v->validate()); } @@ -2255,15 +2257,15 @@ public function testOptionalProvidedValid() public function testOptionalProvidedValidAltSyntax() { - $v = new Valitron\Validator(['username' => 'batman']); - $v->rules([ - 'alpha' => [ - ['username'] - ], - 'optional' => [ - ['username'] - ] - ]); + $v = new Valitron\Validator(array('username' => 'batman')); + $v->rules(array( + 'alpha' => array( + array('username') + ), + 'optional' => array( + array('username') + ) + )); $this->assertTrue($v->validate()); } @@ -2276,7 +2278,7 @@ public function testOptionalProvidedInvalid() public function testChainingRules() { - $v = new Valitron\Validator(['email_address' => 'test@test.com']); + $v = new Valitron\Validator(array('email_address' => 'test@test.com')); $v->rule('required', 'email_address')->rule('email', 'email_address'); $this->assertTrue($v->validate()); } @@ -2290,7 +2292,7 @@ public function testNestedDotNotation() public function testOptionalProvidedInvalidAltSyntax() { - $v = new Valitron\Validator(['username' => 'batman123']); + $v = new Valitron\Validator(array('username' => 'batman123')); $v->rules(array( 'alpha' => array( array('username') From 3f192174874af68929784cc2a0bd92819fbb7fda Mon Sep 17 00:00:00 2001 From: Tom Breese Date: Sat, 13 Oct 2018 13:28:03 -0400 Subject: [PATCH 5/5] update-docs; fixing regex after mass replacing php 5.4 array with php 5.3 array --- tests/Valitron/ValidateTest.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/tests/Valitron/ValidateTest.php b/tests/Valitron/ValidateTest.php index 54ad706..169c0e6 100644 --- a/tests/Valitron/ValidateTest.php +++ b/tests/Valitron/ValidateTest.php @@ -116,6 +116,7 @@ public function testEqualsInvalidAltSyntax() array('password', 'confirmPassword') ) )); + $this->assertFalse($v->validate()); } public function testEqualsBothNull() @@ -1358,7 +1359,7 @@ public function testNoErrorFailOnArray() public function testRegexValid() { $v = new Validator(array('test' => '42')); - $v->rule('regex', 'test', '/array(\d)+/'); + $v->rule('regex', 'test', '/[\d]+/'); $this->assertTrue($v->validate()); } @@ -1367,7 +1368,7 @@ public function testRegexValidAltSyntax() $v = new Valitron\Validator(array('username' => 'Batman123')); $v->rules(array( 'regex' => array( - array('username', '/^array(a-zA-Z0-9){5,10}$/') + array('username', '/^[a-zA-Z0-9]{5,10}$/') ) )); $this->assertTrue($v->validate()); @@ -1376,7 +1377,7 @@ public function testRegexValidAltSyntax() public function testRegexInvalid() { $v = new Validator(array('test' => 'istheanswer')); - $v->rule('regex', 'test', '/array(\d)+/'); + $v->rule('regex', 'test', '/[\d]+/'); $this->assertFalse($v->validate()); } @@ -1385,7 +1386,7 @@ public function testRegexInvalidAltSyntax() $v = new Valitron\Validator(array('username' => 'Batman_123')); $v->rules(array( 'regex' => array( - array('username', '/^array(a-zA-Z0-9){5,10}$/') + array('username', '/^[a-zA-Z0-9]{5,10}$/') ) )); $this->assertFalse($v->validate());