Skip to content

Commit b2ccf4d

Browse files
committed
Implement validation rule for min and max length individually
1 parent 739571d commit b2ccf4d

File tree

5 files changed

+127
-1
lines changed

5 files changed

+127
-1
lines changed

src/Rules/MaxLength.php

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Webhkp\Pvalidate\Rules;
6+
7+
use Attribute;
8+
9+
#[Attribute(Attribute::TARGET_PROPERTY)]
10+
class MaxLength extends Length {
11+
public function __construct(
12+
readonly private float $length
13+
) {
14+
parent::__construct(max: $length);
15+
}
16+
}

src/Rules/MinLength.php

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Webhkp\Pvalidate\Rules;
6+
7+
use Attribute;
8+
9+
#[Attribute(Attribute::TARGET_PROPERTY)]
10+
class MinLength extends Length {
11+
public function __construct(
12+
readonly private float $length
13+
) {
14+
parent::__construct(min: $length);
15+
}
16+
}

tests/Feature/LengthValidationTest.php

+39-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?php
22

33
use Webhkp\Pvalidate\Rules\Length;
4+
use Webhkp\Pvalidate\Rules\MaxLength;
5+
use Webhkp\Pvalidate\Rules\MinLength;
46
use Webhkp\Pvalidate\ValidationBuilder;
57
use Webhkp\Pvalidate\Validator;
68

@@ -20,6 +22,12 @@ public function __construct() {
2022

2123
#[Length(min: 20, max: 50)]
2224
public string $fourthString = 'my fourth string';
25+
26+
#[MinLength(20)]
27+
public string $minLengthTestString = 'abc';
28+
29+
#[MaxLength(10)]
30+
public string $maxLengthTestString = 'abc def ghi jkl mno';
2331
};
2432
});
2533

@@ -30,6 +38,8 @@ public function __construct() {
3038
$this->testObj->secondString = "abc def ghi jkl mno pqr stu vwx yz";
3139
$this->testObj->thirdString = "abc def";
3240
$this->testObj->fourthString = "abc def ghi jkl mno pqr stu vwx yz";
41+
$this->testObj->minLengthTestString = "abc def ghi jkl mno pqr stu vwx yz";
42+
$this->testObj->maxLengthTestString = "abc def";
3343

3444
$validationResponse = Validator::validate($this->testObj);
3545

@@ -41,7 +51,7 @@ public function __construct() {
4151
$validationResponse = Validator::validate($this->testObj);
4252

4353
expect($validationResponse->isValid())->toBeFalse();
44-
expect($validationResponse->getErrors())->toHaveKeys(['firstString', 'secondString', 'thirdString', 'fourthString']);
54+
expect($validationResponse->getErrors())->toHaveKeys(['firstString', 'secondString', 'thirdString', 'fourthString', 'minLengthTestString', 'maxLengthTestString']);
4555
});
4656
});
4757

@@ -52,6 +62,34 @@ public function __construct() {
5262
expect($validation->isValid())->toBeFalse();
5363
expect($validation->getErrors())->toHaveKeys(['length.errors.maxLength']);
5464
});
65+
66+
it('Should return error for minLength violation', function () {
67+
$validation = ValidationBuilder::minLength(20)->safeParse('abc def');
68+
69+
expect($validation->isValid())->toBeFalse();
70+
expect($validation->getErrors())->toHaveKeys(['minLength.errors.minLength']);
71+
});
72+
73+
it('Should return error for maxLength violation', function () {
74+
$validation = ValidationBuilder::maxLength(20)->safeParse('abc def ghi jkl mno pqr stu vwx yz');
75+
76+
expect($validation->isValid())->toBeFalse();
77+
expect($validation->getErrors())->toHaveKeys(['maxLength.errors.maxLength']);
78+
});
79+
80+
it('Should return error for length(min or max) violation', function () {
81+
$validation = ValidationBuilder::minLength(10)->maxLength(20);
82+
83+
$validationResult = $validation->safeParse('abc');
84+
85+
expect($validationResult->isValid())->toBeFalse();
86+
expect($validationResult->getErrors())->toHaveKeys(['minLength.errors.minLength']);
87+
88+
$validationResult = $validation->safeParse('abc def ghi jkl mno pqr stu vwx yz');
89+
90+
expect($validationResult->isValid())->toBeFalse();
91+
expect($validationResult->getErrors())->toHaveKeys(['maxLength.errors.maxLength']);
92+
});
5593
});
5694
});
5795

tests/Unit/MaxLengthTest.php

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
use Webhkp\Pvalidate\Exceptions\PvalidateException;
4+
use Webhkp\Pvalidate\Rules\MaxLength;
5+
6+
beforeEach(function () {
7+
$this->rule = new MaxLength(10);
8+
});
9+
10+
describe("MaxLength validation", function () {
11+
it('Should be valid', function () {
12+
$validationResult = $this->rule->safeParse('abc');
13+
14+
expect($validationResult->isValid())->toBeTrue();
15+
expect($validationResult->getErrors())->toBeEmpty();
16+
});
17+
18+
it('Should return \'maxLength\' error on safeParse', function () {
19+
$validationResult = $this->rule->safeParse('abc def ghi jkl mno');
20+
21+
expect($validationResult->isValid())->toBeFalse();
22+
expect($validationResult->getErrors())->toHaveKey('maxLength');
23+
});
24+
25+
it('Should throw exception on parse', function () {
26+
$this->rule->parse('abc def ghi jkl mno');
27+
})->throws(PvalidateException::class);
28+
});

tests/Unit/MinRuleTest.php

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
use Webhkp\Pvalidate\Exceptions\PvalidateException;
4+
use Webhkp\Pvalidate\Rules\MinLength;
5+
6+
beforeEach(function () {
7+
$this->rule = new MinLength(10);
8+
});
9+
10+
describe("MinLength validation", function () {
11+
it('Should be valid', function () {
12+
$validationResult = $this->rule->safeParse('abc def ghi jkl mno');
13+
14+
expect($validationResult->isValid())->toBeTrue();
15+
expect($validationResult->getErrors())->toBeEmpty();
16+
});
17+
18+
it('Should return \'minLength\' error on safeParse', function () {
19+
$validationResult = $this->rule->safeParse('abc');
20+
21+
expect($validationResult->isValid())->toBeFalse();
22+
expect($validationResult->getErrors())->toHaveKey('minLength');
23+
});
24+
25+
it('Should throw exception on parse', function () {
26+
$this->rule->parse('abc');
27+
})->throws(PvalidateException::class);
28+
});

0 commit comments

Comments
 (0)