mirror of
https://github.com/Respect/Validation.git
synced 2026-03-16 23:35:45 +01:00
The Masked validator decorates other validators to mask sensitive input values in error messages while still validating the original unmasked data. This validator is essential for applications handling sensitive information such as passwords, credit cards, or email addresses. Without it, users would need to implement a custom layer between Validation and the end user to prevent PII from appearing in error messages or logs. With Masked, sensitive data protection is built directly into the validation workflow with no additional abstraction required. Assisted-by: Claude Code (Opus 4.5)
58 lines
1.7 KiB
PHP
58 lines
1.7 KiB
PHP
<?php
|
|
|
|
/*
|
|
* SPDX-License-Identifier: MIT
|
|
* SPDX-FileCopyrightText: (c) Respect Project Contributors
|
|
* SPDX-FileContributor: Henrique Moody <henriquemoody@gmail.com>
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Respect\Validation\Validators;
|
|
|
|
use PHPUnit\Framework\Attributes\CoversClass;
|
|
use PHPUnit\Framework\Attributes\DataProvider;
|
|
use PHPUnit\Framework\Attributes\Test;
|
|
use Respect\StringFormatter\MaskFormatter;
|
|
use Respect\Validation\Exceptions\InvalidValidatorException;
|
|
use Respect\Validation\Test\TestCase;
|
|
use Respect\Validation\Test\Validators\Stub;
|
|
|
|
#[CoversClass(Masked::class)]
|
|
final class MaskedTest extends TestCase
|
|
{
|
|
#[Test]
|
|
public function shouldNotAllowCreatingValidatorWithAnInvalidRange(): void
|
|
{
|
|
$range = '0-3';
|
|
|
|
$this->expectException(InvalidValidatorException::class);
|
|
|
|
new Masked($range, Stub::daze());
|
|
}
|
|
|
|
#[Test]
|
|
#[DataProvider('providerForNonStringValues')]
|
|
public function shouldNotValidateWhenInputIsNotStringValue(mixed $input): void
|
|
{
|
|
$this->assertInvalidInput(new Masked('1-', Stub::any(1)), $input);
|
|
}
|
|
|
|
#[Test]
|
|
#[DataProvider('providerForStringValues')]
|
|
public function shouldMaskTheInputWhenInputIsStringValue(mixed $input): void
|
|
{
|
|
$maskFormatter = new MaskFormatter('1-', '*');
|
|
|
|
$stub = Stub::pass(2);
|
|
$comparableResult = $stub->evaluate($input);
|
|
|
|
$validator = new Masked('1-', $stub);
|
|
|
|
$result = $validator->evaluate($input);
|
|
|
|
self::assertSame($maskFormatter->format((string) $input), $result->input);
|
|
self::assertSame($comparableResult->hasPassed, $result->hasPassed);
|
|
self::assertSame($comparableResult->validator, $result->validator);
|
|
}
|
|
}
|