You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The Patterned validator formats input values using a pattern before
displaying them in error messages, while still validating the original
unformatted input. This is the complement to the Masked validator:
- Masked: Hides sensitive parts of the input (e.g., "4111****1111")
- Patterned: Adds formatting to raw input (e.g., "4111 1111 1111 1111")
This validator solves a common UX problem: when users submit data without
formatting characters (like spaces or punctuation), error messages show
the raw input, which can be harder to read and verify. For example:
- Credit card "4111111111111112" → "4111 1111 1111 1112"
- CPF "12345678900" → "123.456.789-00"
- Phone "11987654321" → "(11) 98765-4321"
The validator is particularly useful in scenarios where:
1. Form inputs strip formatting characters before submission
2. Data comes from external systems without formatting
3. Users need to visually verify the value they entered
4. Error messages should match the expected display format
It uses the PatternFormatter from respect/string-formatter, which supports
digit placeholders (0), quantifiers ({n}), and transformations (\u for
uppercase, \l for lowercase, etc.).
Assisted-by: Claude Code (Opus 4.5)
// → "41111XXXXXXX1211" must be a valid credit card number
719
720
```
720
721
722
+
#### Patterned
723
+
724
+
Decorates a validator to format input values using a pattern in error messages while still validating the original unformatted data. This is useful for displaying formatted values when the original input lacks formatting characters:
@@ -324,6 +325,7 @@ In this page you will find a list of validators by their category.
324
325
[ObjectType]: validators/ObjectType.md"Validates whether the input is an object."
325
326
[Odd]: validators/Odd.md"Validates whether the input is an odd number or not."
326
327
[OneOf]: validators/OneOf.md"Will validate if exactly one inner validator passes."
328
+
[Patterned]: validators/Patterned.md"Decorates a validator to format input values using a pattern in error messages while still validating the original unformatted input."
327
329
[Pesel]: validators/Pesel.md"Validates PESEL (Polish human identification number)."
328
330
[Phone]: validators/Phone.md"Validates whether the input is a valid phone number. This validator requires"
329
331
[Pis]: validators/Pis.md"Validates a Brazilian PIS/NIS number ignoring any non-digit char."
// → "(11) 98765-4321" must be a valid telephone number
24
+
```
25
+
26
+
This validator is useful for displaying formatted values in error messages when the original input lacks formatting characters.
27
+
28
+
It uses [respect/string-formatter](https://github.com/Respect/StringFormatter) as the underlying formatting engine. See the documentation of [PatternFormatter](https://github.com/Respect/StringFormatter/blob/main/docs/PatternFormatter.md) for more information about the pattern syntax.
29
+
30
+
## Categorization
31
+
32
+
- Display
33
+
- Miscellaneous
34
+
35
+
## Behavior
36
+
37
+
The validator first ensures the input is a valid string using `StringVal`. If the input passes string validation, it validates the original unformatted input using the inner validator. If validation fails, it applies the pattern formatting to the input value shown in error messages.
0 commit comments