mirror of
https://github.com/Respect/Validation.git
synced 2026-03-15 06:45:44 +01:00
- Remove redundant "valid" prefix: Date, DateTime, DateTimeDiff, Domain, Email, Iban, Imei, Ip, Isbn, Json, LanguageCode, LeapDate, LeapYear, Luhn, MacAddress, NfeAccessKey, Nif, Nip, Pesel, Phone, Pis, PolishIdCard, PostalCode, Roman, Slug, Tld, Url, Uuid, Version. - Remove redundant "value" suffix ArrayVal, BoolVal, Countable, FloatVal, IntVal, IterableVal, NumericVal, ScalarVal, StringVal. - Standardize "consist only of" phrasing Alnum, Alpha, Cntrl, Consonant, Digit, Graph, Lowercase, Printable, Punct, Space, Spaced, Uppercase, Vowel, Xdigit. - Improve file accessibility messages Directory, Executable, File, Image, Readable, SymbolicLink, Writable. - Improve grammar and article usage CreditCard, Extension, Mimetype, Regex, Size.
77 lines
2.4 KiB
PHP
77 lines
2.4 KiB
PHP
<?php
|
|
|
|
/*
|
|
* SPDX-License-Identifier: MIT
|
|
* SPDX-FileCopyrightText: (c) Respect Project Contributors
|
|
* SPDX-FileContributor: Henrique Moody <henriquemoody@gmail.com>
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
test('findByPath with nested keys', function (): void {
|
|
$validator = v::key('user', v::key('email', v::email()))
|
|
->key('items', v::each(v::positive()));
|
|
|
|
$result = $validator->validate([
|
|
'user' => ['email' => 'invalid'],
|
|
'items' => [10, -5, 20],
|
|
]);
|
|
|
|
$emailResult = $result->findByPath('user.email');
|
|
expect()
|
|
->and($emailResult)->not->toBeNull()
|
|
->and($emailResult?->hasFailed())->toBeTrue()
|
|
->and($emailResult?->getMessage())->toBe('`.user.email` must be an email address');
|
|
});
|
|
|
|
test('findByPath with array index', function (): void {
|
|
$validator = v::key('items', v::each(v::positive()));
|
|
|
|
$result = $validator->validate([
|
|
'items' => [10, -5, 20],
|
|
]);
|
|
|
|
$itemResult = $result->findByPath('items.1');
|
|
expect()
|
|
->and($itemResult)->not->toBeNull()
|
|
->and($itemResult?->hasFailed())->toBeTrue()
|
|
->and($itemResult?->getMessage())->toBe('`.items.1` must be a positive number');
|
|
});
|
|
|
|
test('findByName with named validator', function (): void {
|
|
$result = v::named('User Email', v::email())->validate('bad');
|
|
|
|
$namedResult = $result->findByName('User Email');
|
|
expect()
|
|
->and($namedResult)->not->toBeNull()
|
|
->and($namedResult?->hasFailed())->toBeTrue()
|
|
->and($namedResult?->getMessage())->toBe('User Email must be an email address');
|
|
});
|
|
|
|
test('findById with validator id', function (): void {
|
|
$result = v::stringType()->email()->validate(123);
|
|
|
|
$stringResult = $result->findById('stringType');
|
|
expect()
|
|
->and($stringResult)->not->toBeNull()
|
|
->and($stringResult?->hasFailed())->toBeTrue()
|
|
->and($stringResult?->getMessage())->toBe('123 must be a string');
|
|
});
|
|
|
|
test('findByPath returns null when path not found', function (): void {
|
|
$result = v::key('user', v::email())->validate(['user' => 'bad']);
|
|
|
|
expect($result->findByPath('nonexistent'))->toBeNull();
|
|
});
|
|
|
|
test('findByName returns null when name not found', function (): void {
|
|
$result = v::email()->validate('bad');
|
|
|
|
expect($result->findByName('Nonexistent'))->toBeNull();
|
|
});
|
|
|
|
test('findById returns null when id not found', function (): void {
|
|
$result = v::email()->validate('bad');
|
|
|
|
expect($result->findById('nonexistent'))->toBeNull();
|
|
});
|