From 20dcd56b373666fe5543bc6406cec9c56fcde0b4 Mon Sep 17 00:00:00 2001 From: FallegaHQ Date: Sat, 12 Apr 2025 02:15:09 +0100 Subject: [PATCH] chore: rename JsonValidator methods --- CHANGELOG.md | 7 +- README.md | 10 +- VERSION | 2 +- examples/AdvancedExamples.php | 16 +- src/JsonAssertions.php | 16 +- src/JsonValidator.php | 172 ++++----- src/JsonValidatorAssertion.php | 24 +- tests/JsonAssertionsTest.php | 8 +- tests/JsonValidatorTest.php | 669 ++++++++++++++++----------------- 9 files changed, 439 insertions(+), 485 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e7dab76..8ac3327 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,12 @@ # Change Log All notable changes to this project will be documented in this file. -## [0.0.1] - 12/04/2025 +## [0.0.1alpha5] - 12/04/2025 + +### Additions +- Renamed some methods + +## [0.0.1alpha1] - 12/04/2025 ### Additions - Initial release diff --git a/README.md b/README.md index 80f04a1..ec807c8 100644 --- a/README.md +++ b/README.md @@ -258,14 +258,14 @@ use FallegaHQ\JsonTestUtils\JsonValidator; $validator = new JsonValidator($json); $validator->has('data') - ->whereType('data', 'array') - ->whereNotEmpty('data') - ->whereEach('data.items', function($item) { + ->isType('data', 'array') + ->isNotEmpty('data') + ->passesEach('data.items', function($item) { return isset($item['id']) ? true : 'Item must have an ID'; }); -if ($validator->fails()) { - var_dump($validator->errors()); +if ($validator->failed()) { + var_dump($validator->getErrors()); } ``` diff --git a/VERSION b/VERSION index 548cef6..6a331f7 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.0.1alpha4 +0.0.1alpha5 diff --git a/examples/AdvancedExamples.php b/examples/AdvancedExamples.php index 776ff48..492b1ab 100644 --- a/examples/AdvancedExamples.php +++ b/examples/AdvancedExamples.php @@ -180,21 +180,21 @@ public function testDirectValidatorUsage(): void { // First level validation $validator->has('settings'); - $validator->whereType('settings', 'array'); + $validator->isType('settings', 'array'); // Validate notification settings $validator->has('settings.notifications'); - $validator->whereType('settings.notifications.email', 'boolean'); - $validator->whereType('settings.notifications.push', 'boolean'); - $validator->whereType('settings.notifications.sms', 'boolean'); + $validator->isType('settings.notifications.email', 'boolean'); + $validator->isType('settings.notifications.push', 'boolean'); + $validator->isType('settings.notifications.sms', 'boolean'); // Validate privacy settings $validator->has('settings.privacy'); - $validator->whereType('settings.privacy.share_data', 'boolean'); - $validator->whereType('settings.privacy.public_profile', 'boolean'); + $validator->isType('settings.privacy.share_data', 'boolean'); + $validator->isType('settings.privacy.public_profile', 'boolean'); // Advanced: custom logic check across multiple values - $validator->whereIs('settings', function ($settings) { + $validator->passes('settings', function ($settings) { // Example: at least one notification type must be enabled $notifications = $settings['notifications'] ?? []; $hasOneEnabled = false; @@ -209,6 +209,6 @@ public function testDirectValidatorUsage(): void { return $hasOneEnabled ? true : 'At least one notification must be enabled'; }); - self::assertTrue($validator->passes(), 'Validation failed: '.json_encode($validator->errors(), JSON_THROW_ON_ERROR)); + self::assertTrue($validator->validated(), 'Validation failed: '.json_encode($validator->getErrors(), JSON_THROW_ON_ERROR)); } } diff --git a/src/JsonAssertions.php b/src/JsonAssertions.php index dd67661..c867659 100644 --- a/src/JsonAssertions.php +++ b/src/JsonAssertions.php @@ -36,7 +36,7 @@ protected function assertJsonHasKey(array|string $json, string $key, ?string $me $validator = new JsonValidator($json); $validator->has($key); - Assert::assertTrue($validator->passes(), $message ?? $this->formatValidatorErrors($validator->errors())); + Assert::assertTrue($validator->validated(), $message ?? $this->formatValidatorErrors($validator->getErrors())); } /** @@ -50,7 +50,7 @@ protected function assertJsonNotHasKey(array|string $json, string $key, ?string $validator = new JsonValidator($json); $validator->hasNot($key); - Assert::assertTrue($validator->passes(), $message ?? $this->formatValidatorErrors($validator->errors())); + Assert::assertTrue($validator->validated(), $message ?? $this->formatValidatorErrors($validator->getErrors())); } /** @@ -63,9 +63,9 @@ protected function assertJsonNotHasKey(array|string $json, string $key, ?string */ protected function assertJsonEquals(array|string $json, string $key, mixed $expectedValue, ?string $message = null): void { $validator = new JsonValidator($json); - $validator->where($key, $expectedValue); + $validator->hasWithValue($key, $expectedValue); - Assert::assertTrue($validator->passes(), $message ?? $this->formatValidatorErrors($validator->errors())); + Assert::assertTrue($validator->validated(), $message ?? $this->formatValidatorErrors($validator->getErrors())); } /** @@ -78,9 +78,9 @@ protected function assertJsonEquals(array|string $json, string $key, mixed $expe */ protected function assertJsonType(array|string $json, string $key, string $expectedType, ?string $message = null): void { $validator = new JsonValidator($json); - $validator->whereType($key, $expectedType); + $validator->isType($key, $expectedType); - Assert::assertTrue($validator->passes(), $message ?? $this->formatValidatorErrors($validator->errors())); + Assert::assertTrue($validator->validated(), $message ?? $this->formatValidatorErrors($validator->getErrors())); } /** @@ -93,9 +93,9 @@ protected function assertJsonType(array|string $json, string $key, string $expec */ protected function assertJsonCondition(array|string $json, string $key, callable $condition, ?string $message = null): void { $validator = new JsonValidator($json); - $validator->whereIs($key, $condition); + $validator->passes($key, $condition); - Assert::assertTrue($validator->passes(), $message ?? $this->formatValidatorErrors($validator->errors())); + Assert::assertTrue($validator->validated(), $message ?? $this->formatValidatorErrors($validator->getErrors())); } /** diff --git a/src/JsonValidator.php b/src/JsonValidator.php index 764fc53..54ab70f 100644 --- a/src/JsonValidator.php +++ b/src/JsonValidator.php @@ -39,23 +39,6 @@ public static function validator(array|string $data): self { return new self($data); } - /** - * Validate that a key matches a custom condition - * - * @param string $key The key to check (dot notation supported) - * @param callable $callback Function that returns true if valid - * @param string|null $message Optional custom error message - * - * @return $this - */ - public function whereIs(string $key, callable $callback, ?string $message = null): static { - if (! $this->hasKey($key) || ! $callback($this->getValue($key))) { - $this->addError($key, $message ?? "The '{$key}' doesn't match the required condition"); - } - - return $this; - } - /** * Validate that multiple keys are of specific types * @@ -63,9 +46,9 @@ public function whereIs(string $key, callable $callback, ?string $message = null * * @return $this */ - public function whereAllTypes(array $typeMap): static { + public function hasTypedItems(array $typeMap): static { foreach ($typeMap as $key => $type) { - $this->whereType($key, $type); + $this->isType($key, $type); } return $this; @@ -79,7 +62,7 @@ public function whereAllTypes(array $typeMap): static { * * @return $this */ - public function whereType(string $key, string $type): static { + public function isType(string $key, string $type): static { if (! $this->hasKey($key)) { $this->addError($key, "The '{$key}' is required"); @@ -109,17 +92,18 @@ public function whereType(string $key, string $type): static { /** * Validate that a key exists, but is optional * - * @param string $key The key to check (dot notation supported) + * @param string $key The key to check (dot notation supported) + * @param mixed $value Value to validate against, if the key is present * * @return $this */ - public function whereOptional(string $key, mixed $value): static { + public function optional(string $key, mixed $value): static { if (! $this->hasKey($key)) { return $this; } // Check the value - return $this->where($key, $value); + return $this->hasWithValue($key, $value); } /** @@ -130,11 +114,11 @@ public function whereOptional(string $key, mixed $value): static { * * @return $this */ - public function where(string $key, mixed $value): static { + public function hasWithValue(string $key, mixed $value): static { /** * @phpstan-ignore-next-line */ - if (! $this->hasKey($key) || $this->getValue($key) != $value) { + if (! $this->hasKey($key) || $value != $this->getValue($key)) { $this->addError($key, "The '{$key}' must be exactly: ".$this->valueToString($value)); } @@ -149,13 +133,13 @@ public function where(string $key, mixed $value): static { * * @return $this */ - public function whereOptionalType(string $key, string $type): static { + public function optionalWithType(string $key, string $type): static { if (! $this->hasKey($key)) { return $this; } // Check the type - return $this->whereType($key, $type); + return $this->isType($key, $type); } /** @@ -195,7 +179,7 @@ public function has(string $key): static { * * @return $this */ - public function hasNone(array $keys): static { + public function hasNoneOf(array $keys): static { foreach ($keys as $key) { $this->hasNot($key); } @@ -211,17 +195,6 @@ public function hasNone(array $keys): static { * @return $this */ public function hasNot(string $key): static { - return $this->whereNot($key); - } - - /** - * Validate that a key doesn't exist - * - * @param string $key The key that should not exist (dot notation supported) - * - * @return $this - */ - public function whereNot(string $key): static { if ($this->hasKey($key)) { $this->addError($key, "The '{$key}' must not be present"); } @@ -261,7 +234,7 @@ public function hasAnyOf(string ...$keys): static { * * @return $this */ - public function whereIsFile(string $key, bool $mustExist = true): static { + public function isFile(string $key, bool $mustExist = true): static { if (! $this->hasKey($key)) { $this->addError($key, "The '{$key}' is required"); @@ -291,7 +264,7 @@ public function whereIsFile(string $key, bool $mustExist = true): static { * * @return $this */ - public function whereDate(string $key, string $format = 'Y-m-d'): static { + public function isDate(string $key, string $format = 'Y-m-d'): static { if (! $this->hasKey($key)) { $this->addError($key, "The '{$key}' is required"); @@ -322,37 +295,24 @@ public function whereDate(string $key, string $format = 'Y-m-d'): static { * * @return $this */ - public function whereEmail(string $key): static { - return $this->whereIsValid($key, function ($value) use ($key) { - if (! is_string($value)) { - return $key.' must be a string'; - } - - return false !== filter_var($value, FILTER_VALIDATE_EMAIL) ? true : $key.' must be a valid email address'; - }, ); + public function isEmail(string $key): static { + return $this->passes($key, function ($value) { + return is_string($value) && false !== filter_var($value, FILTER_VALIDATE_EMAIL); + }, $key.' must be a valid email address'); } /** - * Validate that a key passes custom validation + * Validate that a key matches a custom condition * - * @param string $key The key to validate (dot notation supported) - * @param callable $validator Function returning true if valid, or an error string if invalid + * @param string $key The key to check (dot notation supported) + * @param callable $check Function that returns true if valid + * @param string|null $message Optional custom error message * * @return $this */ - public function whereIsValid(string $key, callable $validator): static { - if (! $this->hasKey($key)) { - $this->addError($key, "The '{$key}' is required"); - - return $this; - } - - $value = $this->getValue($key); - $result = $validator($value); - - if (true !== $result) { - $message = is_string($result) ? $result : "The '{$key}' failed validation"; - $this->addError($key, $message); + public function passes(string $key, callable $check, ?string $message = null): static { + if (! $this->hasKey($key) || ! $check($this->getValue($key))) { + $this->addError($key, $message ?? "The '{$key}' doesn't match the required condition"); } return $this; @@ -365,14 +325,10 @@ public function whereIsValid(string $key, callable $validator): static { * * @return $this */ - public function whereUrl(string $key): static { - return $this->whereIsValid($key, function ($value) use ($key) { - if (! is_string($value)) { - return $key.' must be a string'; - } - - return false !== filter_var($value, FILTER_VALIDATE_URL) ? true : $key.' must be a valid URL'; - }, ); + public function isURL(string $key): static { + return $this->passes($key, function ($value) { + return is_string($value) && false !== filter_var($value, FILTER_VALIDATE_URL); + }, $key.' must be a valid URL'); } /** @@ -383,7 +339,7 @@ public function whereUrl(string $key): static { * * @return $this */ - public function whereIp(string $key, ?int $flags = null): static { + public function isIP(string $key, ?int $flags = null): static { if (! in_array($flags, [ null, @@ -395,16 +351,13 @@ public function whereIp(string $key, ?int $flags = null): static { return $this; } - return $this->whereIsValid($key, function ($value) use ($flags, $key) { - if (! is_string($value)) { - return $key.' must be a string'; - } - $options = null !== $flags ? [ + return $this->passes($key, function ($value) use ($flags) { + $options = null !== $flags ? [ 'flags' => $flags, ] : []; - return false !== filter_var($value, FILTER_VALIDATE_IP, $options) ? true : $key.' must be a valid IP address'; - }, ); + return is_string($value) && false !== filter_var($value, FILTER_VALIDATE_IP, $options); + }, $key.' must be a valid IP address'); } /** @@ -416,7 +369,7 @@ public function whereIp(string $key, ?int $flags = null): static { * * @return $this */ - public function whereContains(string $key, string $needle, bool $caseSensitive = true): static { + public function contains(string $key, string $needle, bool $caseSensitive = true): static { if (! $this->hasKey($key)) { $this->addError($key, "The '{$key}' is required"); @@ -448,7 +401,7 @@ public function whereContains(string $key, string $needle, bool $caseSensitive = * * @return $this */ - public function whereContainsType(string $key, string $type): static { + public function arrayOfType(string $key, string $type): static { if (! $this->hasKey($key)) { $this->addError($key, "The '{$key}' is required"); @@ -464,7 +417,7 @@ public function whereContainsType(string $key, string $type): static { } foreach ($value as $index => $item) { - $this->whereType($key.'.'.$index, $type); + $this->isType($key.'.'.$index, $type); } return $this; @@ -478,7 +431,7 @@ public function whereContainsType(string $key, string $type): static { * * @return $this */ - public function whereEach(string $key, callable $callback): static { + public function passesEach(string $key, callable $callback): static { if (! $this->hasKey($key)) { $this->addError($key, "The '{$key}' is required"); @@ -512,7 +465,7 @@ public function whereEach(string $key, callable $callback): static { * * @return $this */ - public function whereNotEmpty(string $key): static { + public function isNotEmpty(string $key): static { if (! $this->hasKey($key)) { $this->addError($key, "The '{$key}' is required"); @@ -537,7 +490,7 @@ public function whereNotEmpty(string $key): static { * * @return $this */ - public function whereSchema(string $key, array $schema): static { + public function passesSchema(string $key, array $schema): static { $data = '' === $key ? $this->data : $this->getValue($key); if ('' !== $key && ! $this->hasKey($key)) { @@ -567,12 +520,12 @@ public function whereSchema(string $key, array $schema): static { } else { // This is a nested schema - $this->whereSchema($fullKey, $validationRules); + $this->passesSchema($fullKey, $validationRules); } } elseif (is_string($validationRules)) { // Simple type validation - $this->whereType($fullKey, $validationRules); + $this->isType($fullKey, $validationRules); } } @@ -587,7 +540,7 @@ public function whereSchema(string $key, array $schema): static { * * @return $this */ - public function whereIn(string $key, array|string $allowedValues): static { + public function isIn(string $key, array|string $allowedValues): static { if (! $this->hasKey($key)) { $this->addError($key, "The '{$key}' is required"); @@ -632,7 +585,7 @@ public function whereIn(string $key, array|string $allowedValues): static { * * @return $this */ - public function whereBetween(string $key, float|int $min, float|int $max): static { + public function isBetween(string $key, float|int $min, float|int $max): static { if (! $this->hasKey($key)) { $this->addError($key, "The '{$key}' is required"); @@ -664,7 +617,7 @@ public function whereBetween(string $key, float|int $min, float|int $max): stati * * @return $this */ - public function whereLength(string $key, ?int $exact = null, ?int $min = null, ?int $max = null): static { + public function hasLength(string $key, ?int $exact = null, ?int $min = null, ?int $max = null): static { if (! $this->hasKey($key)) { $this->addError($key, "The '{$key}' is required"); @@ -706,7 +659,7 @@ public function whereLength(string $key, ?int $exact = null, ?int $min = null, ? * * @return $this */ - public function whereRegexMatch(string $key, string $pattern, bool $matchAll = false): static { + public function matchesRegex(string $key, string $pattern, bool $matchAll = false): static { if (! $this->hasKey($key)) { $this->addError($key, "The '{$key}' is required"); @@ -735,9 +688,9 @@ public function whereRegexMatch(string $key, string $pattern, bool $matchAll = f * * @return array Validation errors */ - public function errors(): array { + public function getErrors(): array { if (! $this->validated) { - $this->passes(); + $this->validated(); } return $this->errors; @@ -748,7 +701,7 @@ public function errors(): array { * * @return bool Whether validation passed */ - public function passes(): bool { + public function validated(): bool { $this->validated = true; return empty($this->errors); @@ -760,21 +713,22 @@ public function passes(): bool { * @return array|null The validated data or null if validation failed */ public function getValidData(): ?array { - return $this->passes() ? $this->data : null; + return $this->validated() ? $this->data : null; } /** * Execute the validation and throw an exception if invalid * + * @throws \JsonException * @throws JsonValidationException If validation fails - * @return array The validated data + * @return bool Whether the validation was successful */ - public function validate(): array { - if ($this->fails()) { - throw new JsonValidationException('Validation failed: '.json_encode($this->errors)); + public function validatedStrict(): bool { + if ($this->failed()) { + throw new JsonValidationException('Validation failed: '.json_encode($this->errors, JSON_THROW_ON_ERROR)); } - return $this->data; + return true; } /** @@ -782,8 +736,8 @@ public function validate(): array { * * @return bool Whether validation failed */ - public function fails(): bool { - return ! $this->passes(); + public function failed(): bool { + return ! $this->validated(); } /** @@ -898,29 +852,29 @@ private function applySchemaRules(string $key, array $rules): void { // Check type if (isset($rules['type'])) { - $this->whereType($key, $rules['type']); + $this->isType($key, $rules['type']); } // Check enum values if (isset($rules['enum'])) { - $this->whereIn($key, $rules['enum']); + $this->isIn($key, $rules['enum']); } // Check min/max for numbers if (isset($rules['min']) || isset($rules['max'])) { $min = $rules['min'] ?? PHP_FLOAT_MIN; $max = $rules['max'] ?? PHP_FLOAT_MAX; - $this->whereBetween($key, $min, $max); + $this->isBetween($key, $min, $max); } // Check string length if (isset($rules['minLength']) || isset($rules['maxLength'])) { - $this->whereLength($key, null, $rules['minLength'] ?? null, $rules['maxLength'] ?? null); + $this->hasLength($key, null, $rules['minLength'] ?? null, $rules['maxLength'] ?? null); } // Check pattern if (isset($rules['pattern'])) { - $this->whereRegexMatch($key, $rules['pattern']); + $this->matchesRegex($key, $rules['pattern']); } } } diff --git a/src/JsonValidatorAssertion.php b/src/JsonValidatorAssertion.php index e73a1db..c01132b 100644 --- a/src/JsonValidatorAssertion.php +++ b/src/JsonValidatorAssertion.php @@ -82,7 +82,7 @@ public function hasAnyKey(string ...$keys): self { * @return $this For method chaining */ public function equals(string $key, mixed $value): self { - $this->validator->where($key, $value); + $this->validator->hasWithValue($key, $value); return $this; } @@ -96,7 +96,7 @@ public function equals(string $key, mixed $value): self { * @return $this For method chaining */ public function isType(string $key, string $type): self { - $this->validator->whereType($key, $type); + $this->validator->isType($key, $type); return $this; } @@ -110,7 +110,7 @@ public function isType(string $key, string $type): self { * @return $this For method chaining */ public function in(string $key, array|string $allowedValues): self { - $this->validator->whereIn($key, $allowedValues); + $this->validator->isIn($key, $allowedValues); return $this; } @@ -124,7 +124,7 @@ public function in(string $key, array|string $allowedValues): self { * @return $this For method chaining */ public function matches(string $key, string $pattern): self { - $this->validator->whereRegexMatch($key, $pattern); + $this->validator->matchesRegex($key, $pattern); return $this; } @@ -137,7 +137,7 @@ public function matches(string $key, string $pattern): self { * @return $this For method chaining */ public function isEmail(string $key): self { - $this->validator->whereEmail($key); + $this->validator->isEmail($key); return $this; } @@ -150,7 +150,7 @@ public function isEmail(string $key): self { * @return $this For method chaining */ public function isUrl(string $key): self { - $this->validator->whereUrl($key); + $this->validator->isURL($key); return $this; } @@ -163,7 +163,7 @@ public function isUrl(string $key): self { * @return $this For method chaining */ public function notEmpty(string $key): self { - $this->validator->whereNotEmpty($key); + $this->validator->isNotEmpty($key); return $this; } @@ -179,7 +179,7 @@ public function notEmpty(string $key): self { * @return $this For method chaining */ public function hasLength(string $key, ?int $exact = null, ?int $min = null, ?int $max = null): self { - $this->validator->whereLength($key, $exact, $min, $max); + $this->validator->hasLength($key, $exact, $min, $max); return $this; } @@ -192,7 +192,7 @@ public function hasLength(string $key, ?int $exact = null, ?int $min = null, ?in * @return $this For method chaining */ public function matchesSchema(array $schema): self { - $this->validator->whereSchema('', $schema); + $this->validator->passesSchema('', $schema); return $this; } @@ -205,8 +205,8 @@ public function matchesSchema(array $schema): self { * @throws AssertionFailedError if validation fails */ public function assert(?string $message = null): void { - $validationPassed = $this->validator->passes(); - $errors = $this->validator->errors(); + $validationPassed = $this->validator->validated(); + $errors = $this->validator->getErrors(); $errorMsg = $message ?? $this->formatErrors($errors); @@ -223,7 +223,7 @@ public function assert(?string $message = null): void { * @return $this For method chaining */ public function passes(string $key, callable $callback, ?string $message = null): self { - $this->validator->whereIs($key, $callback, $message); + $this->validator->passes($key, $callback, $message); return $this; } diff --git a/tests/JsonAssertionsTest.php b/tests/JsonAssertionsTest.php index f323444..ca9847e 100644 --- a/tests/JsonAssertionsTest.php +++ b/tests/JsonAssertionsTest.php @@ -133,8 +133,8 @@ public function testJsonSchemaValidation(): void { $schema = [ 'id' => 'integer', 'name' => 'string', - 'email' => function ($validator, $key) { - $validator->whereEmail($key); + 'email' => function (JsonValidator $validator, string $key) { + $validator->isEmail($key); }, 'active' => 'boolean', 'tags' => [ @@ -144,8 +144,8 @@ public function testJsonSchemaValidation(): void { 'profile' => [ 'age' => 'integer', 'city' => 'string', - 'website' => function ($validator, $key) { - $validator->whereUrl($key); + 'website' => function (JsonValidator $validator, string $key) { + $validator->isURL($key); }, ], 'created_at' => [ diff --git a/tests/JsonValidatorTest.php b/tests/JsonValidatorTest.php index c6c0ec1..f8559c9 100644 --- a/tests/JsonValidatorTest.php +++ b/tests/JsonValidatorTest.php @@ -29,6 +29,11 @@ enum UserRole: string { case Subscriber = 'subscriber'; } +enum Status: string { + case Active = 'active'; + case Inactive = 'inactive'; +} + #[CoversClass(Validator::class)] #[CoversClass(JsonValidationException::class)] class JsonValidatorTest extends TestCase { @@ -110,7 +115,7 @@ class JsonValidatorTest extends TestCase { */ protected function setUp(): void { parent::setUp(); - $this->validArray = json_decode($this->validJson, true, flags: JSON_THROW_ON_ERROR); + $this->validArray = json_decode($this->validJson, true, 512, JSON_THROW_ON_ERROR); } public function testValidateJsonString(): void { @@ -120,7 +125,7 @@ public function testValidateJsonString(): void { Validator::validator($this->validJson); } catch (Throwable) { - self::fail('Case invalid has no email.'); + self::fail('A exception has been thrown.'); } } @@ -130,190 +135,190 @@ public function testValidateInvalidJsonThrowsException(): void { Validator::validator('{invalid:json}'); } - public function testWhereIsWithValidCondition(): void { + public function testPassesWithValidCondition(): void { $result = Validator::validator($this->validArray) - ->whereIs('age', fn ($age) => $age >= 18) - ->passes(); + ->passes('age', fn ($age) => $age >= 18) + ->validated(); static::assertTrue($result); } - public function testWhereIsWithInvalidCondition(): void { - $result = Validator::validator($this->validArray) - ->whereIs('age', fn ($age) => $age > 100) - ->passes(); + public function testPassesWithInvalidCondition(): void { + $validator = Validator::validator($this->validArray); + $result = $validator->passes('age', fn ($age) => $age > 100) + ->validated(); static::assertFalse($result); } - public function testWhereIsWithCustomMessage(): void { + public function testPassesWithCustomMessage(): void { $validator = Validator::validator($this->validArray) - ->whereIs('age', fn ($age) => $age > 100, 'Age must be greater than 100'); + ->passes('age', fn ($age) => $age > 100, 'Age must be greater than 100'); - $errors = $validator->errors(); + $errors = $validator->getErrors(); static::assertArrayHasKey('age', $errors); static::assertContains('Age must be greater than 100', $errors['age']); } - public function testWhereWithValidValue(): void { + public function testHasWithValueWithValidValue(): void { $result = Validator::validator($this->validArray) - ->where('name', 'John') - ->passes(); + ->hasWithValue('name', 'John') + ->validated(); static::assertTrue($result); } - public function testWhereWithInvalidValue(): void { + public function testHasWithValueWithInvalidValue(): void { $result = Validator::validator($this->validArray) - ->where('name', 'Jane') - ->passes(); + ->hasWithValue('name', 'Jane') + ->validated(); static::assertFalse($result); } - public function testWhereTypeWithValidType(): void { + public function testIsTypeWithValidType(): void { $result = Validator::validator($this->validArray) - ->whereType('name', 'string') - ->whereType('age', 'integer') - ->whereType('roles', 'array') - ->whereType('settings', 'array') - ->passes(); + ->isType('name', 'string') + ->isType('age', 'integer') + ->isType('roles', 'array') + ->isType('settings', 'array') + ->validated(); static::assertTrue($result); } - public function testWhereTypeWithInvalidType(): void { + public function testIsTypeWithInvalidType(): void { $result = Validator::validator($this->validArray) - ->whereType('name', 'integer') - ->passes(); + ->isType('name', 'integer') + ->validated(); static::assertFalse($result); } - public function testWhereTypeWithValidNullAndObjectType(): void { + public function testIsTypeWithValidNullAndObjectType(): void { $validator = new Validator([ 'obj' => new stdClass(), 'none' => null, ], ); - $result = $validator->whereType('obj', 'object') - ->whereType('obj', stdClass::class) - ->whereType('none', 'null') - ->passes(); + $result = $validator->isType('obj', 'object') + ->isType('obj', stdClass::class) + ->isType('none', 'null') + ->validated(); static::assertTrue($result); } - public function testWhereTypeWithMissingKey(): void { + public function testIsTypeWithMissingKey(): void { $result = Validator::validator($this->validArray) - ->whereType('nonexistent', 'string') - ->passes(); + ->isType('nonexistent', 'string') + ->validated(); static::assertFalse($result); } - public function testWhereAllTypes(): void { + public function testhasTypedItems(): void { $result = Validator::validator($this->validArray) - ->whereAllTypes([ + ->hasTypedItems([ 'name' => 'string', 'age' => 'integer', 'roles' => 'array', ]) - ->passes(); + ->validated(); static::assertTrue($result); } - public function testWhereAllTypesWithInvalidType(): void { + public function testhasTypedItemsWithInvalidType(): void { $result = Validator::validator($this->validArray) - ->whereAllTypes([ + ->hasTypedItems([ 'name' => 'string', 'age' => 'string', ]) - ->passes(); + ->validated(); static::assertFalse($result); } - public function testWhereOptional(): void { + public function testOptional(): void { $result = Validator::validator($this->validArray) - ->whereOptional('nonexistent', 'doesnt-exist') - ->passes(); + ->optional('nonexistent', 'doesnt-exist') + ->validated(); static::assertTrue($result); $validator = Validator::validator($this->validArray); - $result = $validator->whereOptional('age', 30) - ->passes(); + $result = $validator->optional('age', 30) + ->validated(); static::assertTrue($result); } - public function testWhereOptionalType(): void { + public function testOptionalWithType(): void { $result = Validator::validator($this->validArray) - ->whereOptionalType('nonexistent', 'string') - ->passes(); + ->optionalWithType('nonexistent', 'string') + ->validated(); static::assertTrue($result); $validator = Validator::validator($this->validArray); - $result = $validator->whereOptionalType('age', 'int') - ->passes(); + $result = $validator->optionalWithType('age', 'int') + ->validated(); static::assertTrue($result); } - public function testWhereNot(): void { + public function testHasNot(): void { $result = Validator::validator($this->validArray) - ->whereNot('nonexistent') - ->passes(); + ->hasNot('nonexistent') + ->validated(); static::assertTrue($result); } - public function testWhereNotWithExistingKey(): void { + public function testHasNotWithExistingKey(): void { $result = Validator::validator($this->validArray) - ->whereNot('name') - ->passes(); + ->hasNot('name') + ->validated(); static::assertFalse($result); } - public function testWhereInWithValidValue(): void { + public function testIsInWithValidValue(): void { $result = Validator::validator($this->validArray) - ->whereIn('name', [ + ->isIn('name', [ 'John', 'Jane', 'Alice', ], ) - ->passes(); + ->validated(); static::assertTrue($result); } - public function testWhereInWithInvalidValue(): void { + public function testIsInWithInvalidValue(): void { $result = Validator::validator($this->validArray) - ->whereIn('name', [ + ->isIn('name', [ 'Jane', 'Alice', ], ) - ->passes(); + ->validated(); static::assertFalse($result); } - public function testWhereInWithMissingKey(): void { + public function testIsInWithMissingKey(): void { $result = Validator::validator($this->validArray) - ->whereIn('nonexistent', [ + ->isIn('nonexistent', [ 'value1', 'value2', ], ) - ->passes(); + ->validated(); static::assertFalse($result); } - public function testWhereInWithEnumClass(): void { + public function testIsInWithEnumClass(): void { $mockEnum = new class() { public const VALUE1 = 'John'; public const VALUE2 = 'Jane'; @@ -337,8 +342,8 @@ public static function cases(): array { $validator = Validator::validator($this->validArray); - $result = $validator->whereIn('name', $mockEnumClass) - ->passes(); + $result = $validator->isIn('name', $mockEnumClass) + ->validated(); static::assertTrue($result); } @@ -346,7 +351,7 @@ public static function cases(): array { public function testHas(): void { $result = Validator::validator($this->validArray) ->has('name') - ->passes(); + ->validated(); static::assertTrue($result); } @@ -354,7 +359,7 @@ public function testHas(): void { public function testHasWithMissingKey(): void { $result = Validator::validator($this->validArray) ->has('nonexistent') - ->passes(); + ->validated(); static::assertFalse($result); } @@ -366,56 +371,40 @@ public function testHasAll(): void { 'age', 'email', ], ) - ->passes(); + ->validated(); static::assertTrue($result); } public function testHasAllWithMissingKey(): void { - $result = Validator::validator($this->validArray) - ->hasAll([ - 'name', - 'nonexistent', - ], ) - ->passes(); - - static::assertFalse($result); - } - - public function testHasNot(): void { - $result = Validator::validator($this->validArray) - ->hasNot('nonexistent') - ->passes(); - - static::assertTrue($result); - } - - public function testHasNotWithExistingKey(): void { - $result = Validator::validator($this->validArray) - ->hasNot('name') - ->passes(); + $validator = Validator::validator($this->validArray); + $result = $validator->hasAll([ + 'name', + 'nonexistent', + ], ) + ->validated(); static::assertFalse($result); } - public function testHasNone(): void { + public function testHasNoneOf(): void { $result = Validator::validator($this->validArray) - ->hasNone([ + ->hasNoneOf([ 'nonexistent1', 'nonexistent2', ], ) - ->passes(); + ->validated(); static::assertTrue($result); } - public function testHasNoneWithExistingKey(): void { + public function testHasNoneOofWithExistingKey(): void { $result = Validator::validator($this->validArray) - ->hasNone([ + ->hasNoneOf([ 'nonexistent', 'name', ], ) - ->passes(); + ->validated(); static::assertFalse($result); } @@ -423,7 +412,7 @@ public function testHasNoneWithExistingKey(): void { public function testHasAnyOf(): void { $result = Validator::validator($this->validArray) ->hasAnyOf('name', 'nonexistent') - ->passes(); + ->validated(); static::assertTrue($result); } @@ -431,158 +420,158 @@ public function testHasAnyOf(): void { public function testHasAnyOfWithAllMissingKeys(): void { $result = Validator::validator($this->validArray) ->hasAnyOf('nonexistent1', 'nonexistent2') - ->passes(); + ->validated(); static::assertFalse($result); } - public function testWhereIsFile(): void { + public function testIsFile(): void { $tempFile = tempnam(sys_get_temp_dir(), 'test'); $data = [ 'file_path' => $tempFile, ]; $result = Validator::validator($data) - ->whereIsFile('file_path') - ->passes(); + ->isFile('file_path') + ->validated(); static::assertTrue($result); unlink($tempFile); } - public function testWhereIsFileWithNonexistentFile(): void { + public function testIsFileWithNonexistentFile(): void { $data = [ 'file_path' => '/path/to/nonexistent/file.txt', ]; $result = Validator::validator($data) - ->whereIsFile('file_path') - ->passes(); + ->isFile('file_path') + ->validated(); static::assertFalse($result); } - public function testWhereIsFileWithNonStringValue(): void { + public function testIsFileWithNonStringValue(): void { $data = [ 'file_path' => 123, ]; $result = Validator::validator($data) - ->whereIsFile('file_path') - ->passes(); + ->isFile('file_path') + ->validated(); static::assertFalse($result); } - public function testWhereIsFileWithMissingKey(): void { + public function testIsFileWithMissingKey(): void { $result = Validator::validator($this->validArray) - ->whereIsFile('nonexistent') - ->passes(); + ->isFile('nonexistent') + ->validated(); static::assertFalse($result); } - public function testWhereIsFileWithExistenceFlagOff(): void { + public function testIsFileWithExistenceFlagOff(): void { $data = [ 'file_path' => '/path/to/nonexistent/file.txt', ]; $result = Validator::validator($data) - ->whereIsFile('file_path', false) - ->passes(); + ->isFile('file_path', false) + ->validated(); static::assertTrue($result); } - public function testWhereIsValid(): void { + public function tesPasses(): void { $result = Validator::validator($this->validArray) - ->whereIsValid('email', function ($email) { + ->passes('email', function ($email) { return false !== filter_var($email, FILTER_VALIDATE_EMAIL) ? true : 'Invalid email format'; }, ) - ->passes(); + ->validated(); static::assertTrue($result); } - public function testWhereIsValidWithInvalidValue(): void { + public function tesPassesWithInvalidValue(): void { $data = [ 'email' => 'not-an-email', ]; $result = Validator::validator($data) - ->whereIsValid('email', function ($email) { + ->passes('email', function ($email) { return false !== filter_var($email, FILTER_VALIDATE_EMAIL) ? true : 'Invalid email format'; }, ) - ->passes(); + ->validated(); static::assertFalse($result); } - public function testWhereIsValidWithMissingKey(): void { + public function tesPassesWithMissingKey(): void { $result = Validator::validator($this->validArray) - ->whereIsValid('nonexistent', function () { + ->passes('nonexistent', function () { return true; }, ) - ->passes(); + ->validated(); static::assertFalse($result); } - public function testWhereRegexMatch(): void { + public function testMatchesRegex(): void { $result = Validator::validator($this->validArray) - ->whereRegexMatch('email', '/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/') - ->passes(); + ->matchesRegex('email', '/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/') + ->validated(); static::assertTrue($result); } - public function testWhereRegexMatchWithInvalidValue(): void { + public function testMatchesRegexWithInvalidValue(): void { $data = [ 'email' => 'not-an-email', ]; $result = Validator::validator($data) - ->whereRegexMatch('email', '/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/') - ->passes(); + ->matchesRegex('email', '/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/') + ->validated(); static::assertFalse($result); } - public function testWhereRegexMatchWithMatchAll(): void { + public function testMatchesRegexWithMatchAll(): void { $data = [ 'text' => 'one two three', ]; $result = Validator::validator($data) - ->whereRegexMatch('text', '/\w+/', true) - ->passes(); + ->matchesRegex('text', '/\w+/', true) + ->validated(); static::assertTrue($result); } - public function testWhereRegexMatchWithMissingKey(): void { + public function testMatchesRegexWithMissingKey(): void { $result = Validator::validator($this->validArray) - ->whereRegexMatch('nonexistent', '/pattern/') - ->passes(); + ->matchesRegex('nonexistent', '/pattern/') + ->validated(); static::assertFalse($result); } - public function testWhereRegexMatchWithNonStringValue(): void { + public function testMatchesRegexWithNonStringValue(): void { $result = Validator::validator($this->validArray) - ->whereRegexMatch('age', '/pattern/') - ->passes(); + ->matchesRegex('age', '/pattern/') + ->validated(); static::assertFalse($result); } - public function testPasses(): void { + public function testValidated(): void { $result = Validator::validator($this->validArray) ->has('name') - ->passes(); + ->validated(); static::assertTrue($result); } - public function testFails(): void { + public function testFailed(): void { $result = Validator::validator($this->validArray) ->has('nonexistent') - ->fails(); + ->failed(); static::assertTrue($result); } @@ -590,10 +579,10 @@ public function testFails(): void { public function testErrors(): void { $validator = Validator::validator($this->validArray) ->has('nonexistent') - ->where('name', 'Jane'); + ->hasWithValue('name', 'Jane'); - $validator->passes(); - $errors = $validator->errors(); + $validator->validated(); + $errors = $validator->getErrors(); static::assertArrayHasKey('nonexistent', $errors); static::assertArrayHasKey('name', $errors); @@ -617,74 +606,78 @@ public function testGetValidDataWithFailedValidation(): void { static::assertNull($data); } - public function testValidate(): void { + /** + * @throws JsonException + */ + public function testValidatedStrict(): void { $validator = Validator::validator($this->validArray) ->has('name'); - $data = $validator->validate(); - - static::assertEquals($this->validArray, $data); + static::assertTrue($validator->validatedStrict()); } - public function testValidateWithFailedValidationThrowsException(): void { + /** + * @throws JsonException + */ + public function testValidateStrictThrowsException(): void { $this->expectException(JsonValidationException::class); $validator = Validator::validator($this->validArray) ->has('nonexistent'); - $validator->validate(); + $validator->validatedStrict(); } public function testChainedValidation(): void { $result = Validator::validator($this->validArray) ->has('name') - ->where('name', 'John') - ->whereType('age', 'integer') - ->whereIs('age', fn ($age) => $age >= 18) - ->whereIn('email', [ + ->hasWithValue('name', 'John') + ->isType('age', 'integer') + ->passes('age', fn ($age) => $age >= 18) + ->isIn('email', [ 'john@example.com', 'jane@example.com', ], ) - ->whereIsValid('email', fn ($email) => false !== filter_var($email, FILTER_VALIDATE_EMAIL)) - ->whereRegexMatch('email', '/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/') + ->passes('email', fn ($email) => false !== filter_var($email, FILTER_VALIDATE_EMAIL)) + ->matchesRegex('email', '/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/') ->hasAll([ 'name', 'age', 'email', ], ) - ->hasNone([ + ->hasNoneOf([ 'nonexistent1', 'nonexistent2', ], ) ->hasAnyOf('name', 'nonexistent') - ->passes(); + ->validated(); static::assertTrue($result); } - public function testWhereInWithPureEnum(): void { + public function testIsInWithPureEnum(): void { $data = $this->testData; $data['user']['status'] = UserStatus::Active->name; $validator = Validator::validator($data); - $result = $validator->whereIn('user.status', UserStatus::class) - ->passes(); + $result = $validator->isIn('user.status', UserStatus::class) + ->validated(); static::assertTrue($result); $data['user']['status'] = 'Unknown'; $result = Validator::validator($data) - ->whereIn('user.status', UserStatus::class) - ->passes(); + ->isIn('user.status', UserStatus::class) + ->validated(); static::assertFalse($result); } - public function testWhereInWithBackedEnum(): void { + public function testIsInWithBackedEnum(): void { $validator = Validator::validator($this->testData); - $result = $validator->whereIn('user.role', UserRole::class) - ->passes(); + $result = $validator->isIn('user.role', UserRole::class) + ->validated(); static::assertTrue($result); @@ -692,8 +685,8 @@ public function testWhereInWithBackedEnum(): void { $data['user']['role'] = 'guest'; $result = Validator::validator($data) - ->whereIn('user.role', UserRole::class) - ->passes(); + ->isIn('user.role', UserRole::class) + ->validated(); static::assertFalse($result); } @@ -701,17 +694,21 @@ public function testWhereInWithBackedEnum(): void { /** * @throws JsonException */ - public function testWhereTypeWithEnum(): void { + public function testIsTypeWithEnum(): void { $json = '{"status":"active"}'; $validator = Validator::validator($json); - $result = $validator->whereIsValid('status', function ($value) { - return in_array($value, [ - 'active', - 'inactive', - ], true, ) ? true : 'Status must be either active or inactive'; - }) - ->passes(); + $result = $validator->isType('status', 'string')->validated(); + + static::assertTrue($result); + + $json = json_encode([ + 'status' => 'inactive', + ], flags: JSON_THROW_ON_ERROR); + $validator = Validator::validator($json); + + $result = $validator->isIn('status', Status::class) + ->validated(); static::assertTrue($result); @@ -720,18 +717,13 @@ public function testWhereTypeWithEnum(): void { ], flags: JSON_THROW_ON_ERROR); $validator = Validator::validator($json); - $result = $validator->whereIsValid('status', function ($value) { - return in_array($value, [ - 'active', - 'inactive', - ], true, ) ? true : 'Status must be either active or inactive'; - }) - ->passes(); + $result = $validator->isIn('status', Status::class) + ->validated(); static::assertFalse($result); - $errors = $validator->errors(); + $errors = $validator->getErrors(); static::assertArrayHasKey('status', $errors); - static::assertContains('Status must be either active or inactive', $errors['status']); + static::assertContains("The 'status' must be one of: active, inactive", $errors['status']); } public function testComplexEnumValidation(): void { @@ -747,12 +739,12 @@ public function testComplexEnumValidation(): void { $validator = Validator::validator($data); - $result = $validator->whereType('user.id', 'integer') - ->whereType('user.name', 'string') - ->whereIn('user.status', UserStatus::class) - ->whereIn('user.role', UserRole::class) - ->whereType('user.verified', 'boolean') - ->passes(); + $result = $validator->isType('user.id', 'integer') + ->isType('user.name', 'string') + ->isIn('user.status', UserStatus::class) + ->isIn('user.role', UserRole::class) + ->isType('user.verified', 'boolean') + ->validated(); static::assertTrue($result); } @@ -760,7 +752,7 @@ public function testComplexEnumValidation(): void { public function testHasWithDotNotation(): void { $validator = Validator::validator($this->nestedData); $result = $validator->has('user.profile.name') - ->passes(); + ->validated(); static::assertTrue($result); } @@ -768,7 +760,7 @@ public function testHasWithDotNotation(): void { public function testHasWithDeepNestedDotNotation(): void { $validator = Validator::validator($this->nestedData); $result = $validator->has('user.profile.contact.address.country') - ->passes(); + ->validated(); static::assertTrue($result); } @@ -776,64 +768,64 @@ public function testHasWithDeepNestedDotNotation(): void { public function testHasWithNonExistentNestedKey(): void { $validator = Validator::validator($this->nestedData); $result = $validator->has('user.profile.contact.address.zipcode') - ->passes(); + ->validated(); static::assertFalse($result); } - public function testWhereWithDotNotation(): void { + public function testHasWithValueWithDotNotation(): void { $validator = Validator::validator($this->nestedData); - $result = $validator->where('user.profile.name', 'John Doe') - ->passes(); + $result = $validator->hasWithValue('user.profile.name', 'John Doe') + ->validated(); static::assertTrue($result); } - public function testWhereTypeWithDotNotation(): void { + public function testIsTypeWithDotNotation(): void { $validator = Validator::validator($this->nestedData); - $result = $validator->whereType('user.id', 'integer') - ->whereType('user.profile.name', 'string') - ->whereType('user.settings.notifications', 'boolean') - ->whereType('metadata.version', 'float') - ->passes(); + $result = $validator->isType('user.id', 'integer') + ->isType('user.profile.name', 'string') + ->isType('user.settings.notifications', 'boolean') + ->isType('metadata.version', 'float') + ->validated(); static::assertTrue($result); } - public function testWhereInWithDotNotation(): void { + public function testIsInWithDotNotation(): void { $validator = Validator::validator($this->nestedData); - $result = $validator->whereIn('user.settings.theme', [ + $result = $validator->isIn('user.settings.theme', [ 'light', 'dark', 'auto', ], ) - ->passes(); + ->validated(); static::assertTrue($result); } - public function testWhereIsWithDotNotation(): void { + public function testPassesWithDotNotation(): void { $validator = Validator::validator($this->nestedData); - $result = $validator->whereIs('user.profile.name', function ($value) { + $result = $validator->passes('user.profile.name', function ($value) { return str_starts_with($value, 'John'); }, ) - ->passes(); + ->validated(); static::assertTrue($result); } - public function testWhereRegexMatchWithDotNotation(): void { + public function testMatchesRegexWithDotNotation(): void { $validator = Validator::validator($this->nestedData); - $result = $validator->whereRegexMatch('user.profile.email', '/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/') - ->passes(); + $result = $validator->matchesRegex('user.profile.email', '/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/') + ->validated(); static::assertTrue($result); } - public function testWhereNotWithDotNotation(): void { + public function testHasNotWithDotNotation(): void { $validator = Validator::validator($this->nestedData); - $result = $validator->whereNot('user.profile.contact.address.zipcode') - ->passes(); + $result = $validator->hasNot('user.profile.contact.address.zipcode') + ->validated(); static::assertTrue($result); } @@ -845,7 +837,7 @@ public function testHasAllWithDotNotation(): void { 'user.profile.email', 'user.settings.theme', ]) - ->passes(); + ->validated(); static::assertTrue($result); } @@ -853,17 +845,17 @@ public function testHasAllWithDotNotation(): void { public function testHasAnyOfWithDotNotation(): void { $validator = Validator::validator($this->nestedData); $result = $validator->hasAnyOf('user.profile.nonexistent', 'user.settings.theme', 'user.unknown') - ->passes(); + ->validated(); static::assertTrue($result); } - public function testWhereIsValidWithDotNotation(): void { + public function tesPassesWithDotNotation(): void { $validator = Validator::validator($this->nestedData); - $result = $validator->whereIsValid('user.profile.email', function ($email) { + $result = $validator->passes('user.profile.email', function ($email) { return false !== filter_var($email, FILTER_VALIDATE_EMAIL) ? true : 'Invalid email format'; }, ) - ->passes(); + ->validated(); static::assertTrue($result); } @@ -871,18 +863,18 @@ public function testWhereIsValidWithDotNotation(): void { public function testComplexValidationWithDotNotation(): void { $validator = Validator::validator($this->nestedData); $result = $validator->has('user.profile') - ->whereType('user.profile', 'array') - ->whereType('user.id', 'integer') - ->where('user.profile.contact.address.country', 'USA') - ->whereRegexMatch('user.profile.contact.phone', '/^\d{3}-\d{3}-\d{4}$/') - ->whereIn('user.settings.theme', [ + ->isType('user.profile', 'array') + ->isType('user.id', 'integer') + ->hasWithValue('user.profile.contact.address.country', 'USA') + ->matchesRegex('user.profile.contact.phone', '/^\d{3}-\d{3}-\d{4}$/') + ->isIn('user.settings.theme', [ 'light', 'dark', ], ) - ->whereIs('metadata.created_at', function ($date) { + ->passes('metadata.created_at', function ($date) { return false !== strtotime($date); }, ) - ->passes(); + ->validated(); static::assertTrue($result); } @@ -890,8 +882,8 @@ public function testComplexValidationWithDotNotation(): void { public function testValidationWithArrayElements(): void { $validator = Validator::validator($this->nestedData); $result = $validator->has('posts.0.title') - ->where('posts.1.title', 'Second Post') - ->passes(); + ->hasWithValue('posts.1.title', 'Second Post') + ->validated(); static::assertTrue($result); } @@ -904,8 +896,8 @@ public function testNestedPartialValidation(): void { ]); $result = $validator->has('profile.name') ->has('profile.contact.address.city') - ->where('profile.contact.address.country', 'USA') - ->passes(); + ->hasWithValue('profile.contact.address.country', 'USA') + ->validated(); static::assertTrue($result); } @@ -913,7 +905,7 @@ public function testNestedPartialValidation(): void { /** * @throws JsonException */ - public function testWhereContainsType(): void { + public function testArrayOfType(): void { $json = json_encode([ 'array' => [ 1, @@ -922,8 +914,8 @@ public function testWhereContainsType(): void { ], ], flags: JSON_THROW_ON_ERROR, ); $validator = Validator::validator($json); - static::assertTrue($validator->whereContainsType('array', 'integer') - ->passes(), ); + static::assertTrue($validator->arrayOfType('array', 'integer') + ->validated(), ); $json = json_encode([ 'array' => [ @@ -933,15 +925,15 @@ public function testWhereContainsType(): void { ], ], flags: JSON_THROW_ON_ERROR, ); $validator = Validator::validator($json); - static::assertFalse($validator->whereContainsType('array', 'integer') - ->passes(), ); + static::assertFalse($validator->arrayOfType('array', 'integer') + ->validated(), ); $json = json_encode([ 'array' => 'string', ], flags: JSON_THROW_ON_ERROR); $validator = Validator::validator($json); - static::assertFalse($validator->whereContainsType('array', 'integer') - ->passes(), ); + static::assertFalse($validator->arrayOfType('array', 'integer') + ->validated(), ); $json = json_encode([ 'a' => [ @@ -951,8 +943,8 @@ public function testWhereContainsType(): void { ], ], flags: JSON_THROW_ON_ERROR); $validator = Validator::validator($json); - $result = $validator->whereContainsType('a.b', 'string') - ->passes(); + $result = $validator->arrayOfType('a.b', 'string') + ->validated(); static::assertTrue($result); $json = json_encode([ @@ -963,168 +955,171 @@ public function testWhereContainsType(): void { ], ], flags: JSON_THROW_ON_ERROR); $validator = Validator::validator($json); - $result = $validator->whereContainsType('a.b', 'int') - ->passes(); + $result = $validator->arrayOfType('a.b', 'int') + ->validated(); static::assertFalse($result); } /** * @throws JsonException */ - public function testWhereDate(): void { + public function testIDate(): void { $json = json_encode([ 'date' => '2023-05-15', ], flags: JSON_THROW_ON_ERROR); $validator = Validator::validator($json); - static::assertTrue($validator->whereDate('date') - ->passes(), ); + static::assertTrue($validator->isDate('date') + ->validated(), ); $json = json_encode([ 'date' => '15/05/2023', ], flags: JSON_THROW_ON_ERROR); $validator = Validator::validator($json); - static::assertTrue($validator->whereDate('date', 'd/m/Y') - ->passes(), ); + static::assertTrue($validator->isDate('date', 'd/m/Y') + ->validated(), ); $json = json_encode([ 'date' => 'not-a-date', ], flags: JSON_THROW_ON_ERROR); $validator = Validator::validator($json); - static::assertFalse($validator->whereDate('date') - ->passes(), ); - static::assertArrayHasKey('date', $validator->errors()); + static::assertFalse($validator->isDate('date') + ->validated(), ); + static::assertArrayHasKey('date', $validator->getErrors()); } /** * @throws JsonException */ - public function testWhereEmail(): void { + public function testIsEmail(): void { $json = json_encode([ 'email' => 'test@example.com', ], flags: JSON_THROW_ON_ERROR); $validator = Validator::validator($json); - static::assertTrue($validator->whereEmail('email') - ->passes(), ); + static::assertTrue($validator->isEmail('email') + ->validated(), ); $json = json_encode([ 'email' => 'not-an-email', ], flags: JSON_THROW_ON_ERROR); $validator = Validator::validator($json); - static::assertFalse($validator->whereEmail('email') - ->passes(), ); - static::assertArrayHasKey('email', $validator->errors()); + + $result = $validator->isEmail('email') + ->validated(); + static::assertFalse($result); + static::assertArrayHasKey('email', $validator->getErrors()); + static::assertEmpty($validator->getValidData()); } /** * @throws JsonException */ - public function testWhereUrl(): void { + public function testIsUrl(): void { $json = json_encode([ 'url' => 'https://example.com', ], flags: JSON_THROW_ON_ERROR); $validator = Validator::validator($json); - static::assertTrue($validator->whereUrl('url') - ->passes(), ); + static::assertTrue($validator->isURL('url') + ->validated(), ); $json = json_encode([ 'url' => 'not-a-url', ], flags: JSON_THROW_ON_ERROR); $validator = Validator::validator($json); - static::assertFalse($validator->whereUrl('url') - ->passes(), ); - static::assertArrayHasKey('url', $validator->errors()); + static::assertFalse($validator->isURL('url') + ->validated(), ); + static::assertArrayHasKey('url', $validator->getErrors()); } /** * @throws JsonException */ - public function testWhereIp(): void { + public function testIsIp(): void { $json = json_encode([ 'ip' => '192.168.1.1', ], flags: JSON_THROW_ON_ERROR); $validator = Validator::validator($json); - static::assertTrue($validator->whereIp('ip') - ->passes(), ); + static::assertTrue($validator->isIP('ip') + ->validated(), ); $json = json_encode([ 'ip' => '2001:0db8:85a3:0000:0000:8a2e:0370:7334', ], flags: JSON_THROW_ON_ERROR); $validator = Validator::validator($json); - static::assertTrue($validator->whereIp('ip') - ->passes(), ); + static::assertTrue($validator->isIP('ip') + ->validated(), ); $json = json_encode([ 'ip' => 'not-an-ip', ], flags: JSON_THROW_ON_ERROR); $validator = Validator::validator($json); - static::assertFalse($validator->whereIp('ip') - ->passes(), ); - static::assertArrayHasKey('ip', $validator->errors()); + static::assertFalse($validator->isIP('ip') + ->validated(), ); + static::assertArrayHasKey('ip', $validator->getErrors()); $json = json_encode([ 'ip' => '2001:0db8:85a3:0000:0000:8a2e:0370:7334', ], flags: JSON_THROW_ON_ERROR); $validator = Validator::validator($json); - static::assertFalse($validator->whereIp('ip', FILTER_FLAG_IPV4) - ->passes(), ); + static::assertFalse($validator->isIP('ip', FILTER_FLAG_IPV4) + ->validated(), ); } /** * @throws JsonException */ - public function testWhereBetween(): void { + public function testIsBetween(): void { $json = json_encode([ 'number' => 50, ], flags: JSON_THROW_ON_ERROR); $validator = Validator::validator($json); - static::assertTrue($validator->whereBetween('number', 1, 100) - ->passes(), ); + static::assertTrue($validator->isBetween('number', 1, 100) + ->validated(), ); $json = json_encode([ 'number' => 1, ], flags: JSON_THROW_ON_ERROR); $validator = Validator::validator($json); - static::assertTrue($validator->whereBetween('number', 1, 100) - ->passes(), ); + static::assertTrue($validator->isBetween('number', 1, 100) + ->validated(), ); $json = json_encode([ 'number' => 101, ], flags: JSON_THROW_ON_ERROR); $validator = Validator::validator($json); - static::assertFalse($validator->whereBetween('number', 1, 100) - ->passes(), ); - static::assertArrayHasKey('number', $validator->errors()); + static::assertFalse($validator->isBetween('number', 1, 100) + ->validated(), ); + static::assertArrayHasKey('number', $validator->getErrors()); $json = json_encode([ 'number' => 'string', ], flags: JSON_THROW_ON_ERROR); $validator = Validator::validator($json); - static::assertFalse($validator->whereBetween('number', 1, 100) - ->passes(), ); + static::assertFalse($validator->isBetween('number', 1, 100) + ->validated(), ); } /** * @throws JsonException */ - public function testWhereLength(): void { + public function testhasLength(): void { $json = json_encode([ 'string' => 'hello', ], flags: JSON_THROW_ON_ERROR); $validator = Validator::validator($json); - static::assertTrue($validator->whereLength('string', 5) - ->passes(), ); + static::assertTrue($validator->hasLength('string', 5) + ->validated(), ); $validator = Validator::validator($json); - static::assertTrue($validator->whereLength('string', null, 3, 10) - ->passes(), ); + static::assertTrue($validator->hasLength('string', null, 3, 10) + ->validated(), ); $validator = Validator::validator($json); - static::assertFalse($validator->whereLength('string', null, 10) - ->passes(), ); + static::assertFalse($validator->hasLength('string', null, 10) + ->validated(), ); $validator = Validator::validator($json); - static::assertFalse($validator->whereLength('string', null, null, 3) - ->passes(), ); + static::assertFalse($validator->hasLength('string', null, null, 3) + ->validated(), ); $json = json_encode([ 'array' => [ @@ -1134,48 +1129,48 @@ public function testWhereLength(): void { ], ], flags: JSON_THROW_ON_ERROR, ); $validator = Validator::validator($json); - static::assertTrue($validator->whereLength('array', 3) - ->passes(), ); + static::assertTrue($validator->hasLength('array', 3) + ->validated(), ); $json = json_encode([ 'value' => 123, ], flags: JSON_THROW_ON_ERROR); $validator = Validator::validator($json); - static::assertFalse($validator->whereLength('value') - ->passes(), ); + static::assertFalse($validator->hasLength('value') + ->validated(), ); } /** * @throws JsonException */ - public function testWhereContains(): void { + public function testContains(): void { $json = json_encode([ 'string' => 'hello world', ], flags: JSON_THROW_ON_ERROR); $validator = Validator::validator($json); - static::assertTrue($validator->whereContains('string', 'world') - ->passes(), ); + static::assertTrue($validator->contains('string', 'world') + ->validated(), ); $validator = Validator::validator($json); - static::assertTrue($validator->whereContains('string', 'WORLD', false) - ->passes(), ); + static::assertTrue($validator->contains('string', 'WORLD', false) + ->validated(), ); $validator = Validator::validator($json); - static::assertFalse($validator->whereContains('string', 'missing') - ->passes(), ); + static::assertFalse($validator->contains('string', 'missing') + ->validated(), ); $json = json_encode([ 'string' => 123, ], flags: JSON_THROW_ON_ERROR); $validator = Validator::validator($json); - static::assertFalse($validator->whereContains('string', 'world') - ->passes(), ); + static::assertFalse($validator->contains('string', 'world') + ->validated(), ); } /** * @throws JsonException */ - public function testWhereEach(): void { + public function testPassesEach(): void { $json = json_encode([ 'array' => [ 1, @@ -1184,10 +1179,10 @@ public function testWhereEach(): void { ], ], flags: JSON_THROW_ON_ERROR, ); $validator = Validator::validator($json); - $result = $validator->whereEach('array', function ($item) { + $result = $validator->passesEach('array', function ($item) { return $item > 0 ? true : 'Must be positive'; }) - ->passes(); + ->validated(); static::assertTrue($result); $json = json_encode([ @@ -1198,41 +1193,41 @@ public function testWhereEach(): void { ], ], flags: JSON_THROW_ON_ERROR, ); $validator = Validator::validator($json); - $result = $validator->whereEach('array', function ($item) { + $result = $validator->passesEach('array', function ($item) { return $item > 0 ? true : 'Must be positive'; }) - ->passes(); + ->validated(); static::assertFalse($result); - static::assertArrayHasKey('array.1', $validator->errors()); + static::assertArrayHasKey('array.1', $validator->getErrors()); $json = json_encode([ 'array' => 'string', ], flags: JSON_THROW_ON_ERROR); $validator = Validator::validator($json); - $result = $validator->whereEach('array', function () { + $result = $validator->passesEach('array', function () { return true; }) - ->passes(); + ->validated(); static::assertFalse($result); } /** * @throws JsonException */ - public function testWhereNotEmpty(): void { + public function testIsNotEmpty(): void { $json = json_encode([ 'string' => 'hello', ], flags: JSON_THROW_ON_ERROR); $validator = Validator::validator($json); - static::assertTrue($validator->whereNotEmpty('string') - ->passes(), ); + static::assertTrue($validator->isNotEmpty('string') + ->validated(), ); $json = json_encode([ 'string' => '', ], flags: JSON_THROW_ON_ERROR); $validator = Validator::validator($json); - static::assertFalse($validator->whereNotEmpty('string') - ->passes(), ); + static::assertFalse($validator->isNotEmpty('string') + ->validated(), ); $json = json_encode([ 'array' => [ @@ -1242,21 +1237,21 @@ public function testWhereNotEmpty(): void { ], ], flags: JSON_THROW_ON_ERROR, ); $validator = Validator::validator($json); - static::assertTrue($validator->whereNotEmpty('array') - ->passes(), ); + static::assertTrue($validator->isNotEmpty('array') + ->validated(), ); $json = json_encode([ 'array' => [], ], flags: JSON_THROW_ON_ERROR); $validator = Validator::validator($json); - static::assertFalse($validator->whereNotEmpty('array') - ->passes(), ); + static::assertFalse($validator->isNotEmpty('array') + ->validated(), ); } /** * @throws JsonException */ - public function testWhereSchema(): void { + public function testPassesSchema(): void { $jsonData = json_encode([ 'name' => 'John', 'email' => 'john@example.com', @@ -1278,8 +1273,8 @@ public function testWhereSchema(): void { ]; $validator = Validator::validator($jsonData); - static::assertTrue($validator->whereSchema('', $schema) - ->passes(), ); + static::assertTrue($validator->passesSchema('', $schema) + ->validated(), ); $complexSchema = [ 'name' => [ @@ -1312,19 +1307,19 @@ public function testWhereSchema(): void { ]; $validator = Validator::validator($jsonData); - static::assertTrue($validator->whereSchema('', $complexSchema) - ->passes(), ); + static::assertTrue($validator->passesSchema('', $complexSchema) + ->validated(), ); $schemaWithCallback = [ 'name' => 'string', - 'email' => function ($validator, $key) { - $validator->whereEmail($key); + 'email' => function (Validator $validator, string $key) { + $validator->isEmail($key); }, ]; $validator = Validator::validator($jsonData); - static::assertTrue($validator->whereSchema('', $schemaWithCallback) - ->passes(), ); + static::assertTrue($validator->passesSchema('', $schemaWithCallback) + ->validated(), ); $jsonData = json_encode([ 'name' => 123, @@ -1333,8 +1328,8 @@ public function testWhereSchema(): void { ], flags: JSON_THROW_ON_ERROR); $validator = Validator::validator($jsonData); - static::assertFalse($validator->whereSchema('', $schema) - ->passes(), ); - static::assertCount(3, $validator->errors()); + static::assertFalse($validator->passesSchema('', $schema) + ->validated(), ); + static::assertCount(3, $validator->getErrors()); } }