Skip to content

Commit 66a3408

Browse files
committed
Add v::format() to check if data is already in a specific format
This validator is not so much about how we present the input during error messages, but in what format data already is formatted.
1 parent 16148e9 commit 66a3408

File tree

20 files changed

+251
-2
lines changed

20 files changed

+251
-2
lines changed

aliases.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,12 @@
1313
declare(strict_types=1);
1414

1515
use Respect\Validation\ValidatorBuilder;
16+
use Respect\StringFormatter\FormatterBuilder;
1617

1718
if (!class_exists('v')) {
1819
class_alias(ValidatorBuilder::class, 'v');
1920
}
21+
22+
if (!class_exists('f')) {
23+
class_alias(FormatterBuilder::class, 'f');
24+
}

docs/validators.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ In this page you will find a list of validators by their category.
2727

2828
**Date and Time**: [Date][] - [DateTime][] - [DateTimeDiff][] - [LeapDate][] - [LeapYear][] - [Time][]
2929

30-
**Display**: [Masked][] - [Named][] - [Templated][]
30+
**Display**: [Format][] - [Masked][] - [Named][] - [Templated][]
3131

3232
**File system**: [Directory][] - [Executable][] - [Exists][] - [Extension][] - [File][] - [Image][] - [Mimetype][] - [Readable][] - [Size][] - [SymbolicLink][] - [Writable][]
3333

@@ -49,7 +49,7 @@ In this page you will find a list of validators by their category.
4949

5050
**Objects**: [Attributes][] - [Instance][] - [ObjectType][] - [Property][] - [PropertyExists][] - [PropertyOptional][]
5151

52-
**Strings**: [Alnum][] - [Alpha][] - [Base64][] - [Charset][] - [Consonant][] - [Contains][] - [ContainsAny][] - [ContainsCount][] - [Control][] - [Digit][] - [Emoji][] - [EndsWith][] - [Graph][] - [HexRgbColor][] - [In][] - [Json][] - [Lowercase][] - [Phone][] - [PostalCode][] - [Printable][] - [Punct][] - [Regex][] - [Slug][] - [Sorted][] - [Space][] - [Spaced][] - [StartsWith][] - [StringType][] - [StringVal][] - [Uppercase][] - [Uuid][] - [Version][] - [Vowel][] - [Xdigit][]
52+
**Strings**: [Alnum][] - [Alpha][] - [Base64][] - [Charset][] - [Consonant][] - [Contains][] - [ContainsAny][] - [ContainsCount][] - [Control][] - [Digit][] - [Emoji][] - [EndsWith][] - [Format][] - [Graph][] - [HexRgbColor][] - [In][] - [Json][] - [Lowercase][] - [Phone][] - [PostalCode][] - [Printable][] - [Punct][] - [Regex][] - [Slug][] - [Sorted][] - [Space][] - [Spaced][] - [StartsWith][] - [StringType][] - [StringVal][] - [Uppercase][] - [Uuid][] - [Version][] - [Vowel][] - [Xdigit][]
5353

5454
**Structures**: [Attributes][] - [Key][] - [KeyExists][] - [KeyOptional][] - [KeySet][] - [Property][] - [PropertyExists][] - [PropertyOptional][]
5555

@@ -118,6 +118,7 @@ In this page you will find a list of validators by their category.
118118
- [Finite][] - `v::finite()->assert('10');`
119119
- [FloatType][] - `v::floatType()->assert(1.5);`
120120
- [FloatVal][] - `v::floatVal()->assert(1.5);`
121+
- [Format][] - `v::format(f::pattern('00-00'))->assert('42-33');`
121122
- [Graph][] - `v::graph()->assert('LKM@#$%4;');`
122123
- [GreaterThan][] - `v::greaterThan(10)->assert(11);`
123124
- [GreaterThanOrEqual][] - `v::intVal()->greaterThanOrEqual(10)->assert(10);`
@@ -274,6 +275,7 @@ In this page you will find a list of validators by their category.
274275
[Finite]: validators/Finite.md "Validates if the input is a finite number."
275276
[FloatType]: validators/FloatType.md "Validates whether the type of the input is float."
276277
[FloatVal]: validators/FloatVal.md "Validate whether the input value is float."
278+
[Format]: validators/Format.md "Validates whether an input is already formatted as the result of applying a provided"
277279
[Graph]: validators/Graph.md "Validates if all characters in the input are printable and actually creates"
278280
[GreaterThan]: validators/GreaterThan.md "Validates whether the input is greater than a value."
279281
[GreaterThanOrEqual]: validators/GreaterThanOrEqual.md "Validates whether the input is greater than or equal to a value."

docs/validators/Format.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<!--
2+
SPDX-License-Identifier: MIT
3+
SPDX-FileCopyrightText: (c) Respect Project Contributors
4+
SPDX-FileContributor: Alexandre Gomes Gaigalas <alganet@gmail.com>
5+
-->
6+
7+
# Format
8+
9+
- `Format(Formatter $formatter)`
10+
11+
Validates whether an input is already formatted as the result of applying a provided
12+
[Respect\StringFormatter][] `Formatter` to it. You can build formatters using
13+
`Respect\StringFormatter\FormatterBuilder` (commonly aliased as `f`).
14+
15+
```php
16+
v::format(f::pattern('00-00'))->assert('42-33');
17+
// Validation passes successfully
18+
19+
v::format(f::pattern('00-00'))->assert('42.33');
20+
// → "42.33" must be formatted as "42-33"
21+
22+
v::format(f::mask('1-@'))->assert('alganet@gmail.com');
23+
// → "alganet@gmail.com" must be formatted as "*******@gmail.com"
24+
25+
v::not(v::format(f::pattern('00-00')))->assert('42-33');
26+
// → "42-33" must not be formatted as "42-33"
27+
28+
v::named('Vanity plate', v::format(f::pattern('AAA-0000')))->assert('DAD8008');
29+
// → Vanity plate must be formatted as "DAD-8008"
30+
```
31+
32+
This validator is useful when you need to assert that data is already in a desired
33+
presentation (for example, ensuring that a masking tool was already applied or that
34+
stored data follows a required display format).
35+
36+
## Templates
37+
38+
### `Format::TEMPLATE_STANDARD`
39+
40+
| Mode | Template |
41+
| ---------: | :------------------------------------------------- |
42+
| `default` | {{subject}} must be formatted as {{formatted}} |
43+
| `inverted` | {{subject}} must not be formatted as {{formatted}} |
44+
45+
## Template placeholders
46+
47+
| Placeholder | Description |
48+
| ----------- | ---------------------------------------------------------------- |
49+
| `formatted` | The value resulting from applying the provided formatter to the input |
50+
| `subject` | The validated input or the custom validator name (if specified). |
51+
52+
## Categorization
53+
54+
- Display
55+
- Strings
56+
57+
## Changelog
58+
59+
| Version | Description |
60+
| ------: | :---------- |
61+
| 3.0.0 | Created |
62+
63+
## See Also
64+
65+
- [Masked](Masked.md)
66+
- [Templated](Templated.md)
67+
68+
[Respect\StringFormatter]: https://github.com/Respect/StringFormatter

src/Mixins/AllBuilder.php

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Mixins/AllChain.php

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Mixins/Builder.php

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Mixins/Chain.php

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Mixins/KeyBuilder.php

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Mixins/KeyChain.php

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Mixins/NotBuilder.php

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)