Skip to content

Commit a4c1e40

Browse files
committed
add regexp and domain checks on StringSchema
1 parent d5de7dc commit a4c1e40

3 files changed

Lines changed: 222 additions & 108 deletions

File tree

doc/Schema/StringSchema.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,11 @@ $schema->regexp('/^[a-z]+$/i'); // Must match regex pattern
3636
### Format Validations
3737

3838
```php
39+
$schema->domain(); // Valid domain
3940
$schema->email(); // Valid email address
4041
$schema->ipV4(); // Valid IPv4 address
4142
$schema->ipV6(); // Valid IPv6 address
43+
$schema->mac(); // Valid mac address
4244
$schema->url(); // Valid URL
4345
$schema->uuidV4(); // Valid UUID v4
4446
$schema->uuidV5(); // Valid UUID v5
@@ -122,10 +124,12 @@ $usernameSchema->parse(' John_Doe123 '); // Returns: 'john_doe123'
122124
| `string.includes` | String doesn't contain required substring |
123125
| `string.startsWith` | String doesn't start with required prefix |
124126
| `string.endsWith` | String doesn't end with required suffix |
125-
| `string.regexp` | String doesn't match regex pattern |
127+
| `string.domain` | Invalid domain format |
126128
| `string.email` | Invalid email format |
127129
| `string.ipV4` | Invalid IPv4 format |
128130
| `string.ipV6` | Invalid IPv6 format |
131+
| `string.mac` | Invalid mac format |
132+
| `string.regexp` | String doesn't match regex pattern |
129133
| `string.url` | Invalid URL format |
130134
| `string.uuidV4` | Invalid UUID v4 format |
131135
| `string.uuidV5` | Invalid UUID v5 format |

src/Schema/StringSchema.php

Lines changed: 85 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,24 @@ final class StringSchema extends AbstractSchema implements SchemaInterface
3030
public const string ERROR_ENDSWITH_CODE = 'string.endsWith';
3131
public const string ERROR_ENDSWITH_TEMPLATE = '{{given}} does not ends with {{endsWith}}';
3232

33-
public const string ERROR_MATCH_CODE = 'string.match';
34-
public const string ERROR_MATCH_TEMPLATE = '{{given}} does not match {{match}}';
35-
36-
public const string ERROR_REGEXP_CODE = 'string.regexp';
37-
public const string ERROR_REGEXP_TEMPLATE = '{{given}} does not regexp {{regexp}}';
33+
public const string ERROR_DOMAIN_CODE = 'string.domain';
34+
public const string ERROR_DOMAIN_TEMPLATE = 'Invalid domain {{given}}';
3835

3936
public const string ERROR_EMAIL_CODE = 'string.email';
4037
public const string ERROR_EMAIL_TEMPLATE = 'Invalid email {{given}}';
4138

4239
public const string ERROR_IP_CODE = 'string.ip';
4340
public const string ERROR_IP_TEMPLATE = 'Invalid ip {{version}} {{given}}';
4441

42+
public const string ERROR_MAC_CODE = 'string.mac';
43+
public const string ERROR_MAC_TEMPLATE = 'Invalid mac {{given}}';
44+
45+
public const string ERROR_MATCH_CODE = 'string.match';
46+
public const string ERROR_MATCH_TEMPLATE = '{{given}} does not match {{match}}';
47+
48+
public const string ERROR_REGEXP_CODE = 'string.regexp';
49+
public const string ERROR_REGEXP_TEMPLATE = '{{given}} does not regexp {{regexp}}';
50+
4551
public const string ERROR_URL_CODE = 'string.url';
4652
public const string ERROR_URL_TEMPLATE = 'Invalid url {{given}}';
4753

@@ -200,49 +206,15 @@ public function endsWith(string $endsWith): static
200206
});
201207
}
202208

203-
public function regexp(string $regexp): static
209+
public function domain(): static
204210
{
205-
if (false === @preg_match($regexp, '')) {
206-
throw new \InvalidArgumentException(\sprintf('Invalid regexp "%s" given', $regexp));
207-
}
208-
209-
return $this->postParse(static function (string $string) use ($regexp) {
210-
$doesMatch = filter_var($string, FILTER_VALIDATE_REGEXP, ['options' => ['regexp' => $regexp]]);
211-
212-
if (false === $doesMatch) {
213-
throw new ErrorsException(
214-
new Error(
215-
self::ERROR_REGEXP_CODE,
216-
self::ERROR_REGEXP_TEMPLATE,
217-
['regexp' => $regexp, 'given' => $string]
218-
)
219-
);
220-
}
221-
222-
return $string;
223-
});
224-
}
225-
226-
/**
227-
* @deprecated: use regexp
228-
*/
229-
public function match(string $match): static
230-
{
231-
@trigger_error('Use regexp instead', E_USER_DEPRECATED);
232-
233-
if (false === @preg_match($match, '')) {
234-
throw new \InvalidArgumentException(\sprintf('Invalid match "%s" given', $match));
235-
}
236-
237-
return $this->postParse(static function (string $string) use ($match) {
238-
$doesMatch = filter_var($string, FILTER_VALIDATE_REGEXP, ['options' => ['regexp' => $match]]);
239-
240-
if (false === $doesMatch) {
211+
return $this->postParse(static function (string $string) {
212+
if (!filter_var($string, FILTER_VALIDATE_DOMAIN)) {
241213
throw new ErrorsException(
242214
new Error(
243-
self::ERROR_MATCH_CODE,
244-
self::ERROR_MATCH_TEMPLATE,
245-
['match' => $match, 'given' => $string]
215+
self::ERROR_DOMAIN_CODE,
216+
self::ERROR_DOMAIN_TEMPLATE,
217+
['given' => $string]
246218
)
247219
);
248220
}
@@ -302,6 +274,74 @@ public function ipV6(): static
302274
});
303275
}
304276

277+
public function mac(): static
278+
{
279+
return $this->postParse(static function (string $string) {
280+
if (!filter_var($string, FILTER_VALIDATE_MAC)) {
281+
throw new ErrorsException(
282+
new Error(
283+
self::ERROR_MAC_CODE,
284+
self::ERROR_MAC_TEMPLATE,
285+
['given' => $string]
286+
)
287+
);
288+
}
289+
290+
return $string;
291+
});
292+
}
293+
294+
/**
295+
* @deprecated: use regexp
296+
*/
297+
public function match(string $match): static
298+
{
299+
@trigger_error('Use regexp instead', E_USER_DEPRECATED);
300+
301+
if (false === @preg_match($match, '')) {
302+
throw new \InvalidArgumentException(\sprintf('Invalid match "%s" given', $match));
303+
}
304+
305+
return $this->postParse(static function (string $string) use ($match) {
306+
$doesMatch = filter_var($string, FILTER_VALIDATE_REGEXP, ['options' => ['regexp' => $match]]);
307+
308+
if (false === $doesMatch) {
309+
throw new ErrorsException(
310+
new Error(
311+
self::ERROR_MATCH_CODE,
312+
self::ERROR_MATCH_TEMPLATE,
313+
['match' => $match, 'given' => $string]
314+
)
315+
);
316+
}
317+
318+
return $string;
319+
});
320+
}
321+
322+
public function regexp(string $regexp): static
323+
{
324+
if (false === @preg_match($regexp, '')) {
325+
throw new \InvalidArgumentException(\sprintf('Invalid regexp "%s" given', $regexp));
326+
}
327+
328+
return $this->postParse(static function (string $string) use ($regexp) {
329+
$doesMatch = filter_var($string, FILTER_VALIDATE_REGEXP, ['options' => ['regexp' => $regexp]]);
330+
331+
if (false === $doesMatch) {
332+
throw new ErrorsException(
333+
new Error(
334+
self::ERROR_REGEXP_CODE,
335+
self::ERROR_REGEXP_TEMPLATE,
336+
['regexp' => $regexp, 'given' => $string]
337+
)
338+
);
339+
}
340+
341+
return $string;
342+
});
343+
}
344+
305345
public function url(): static
306346
{
307347
return $this->postParse(static function (string $string) {

0 commit comments

Comments
 (0)