Skip to content

Commit c5f95af

Browse files
committed
Update API.md with string.regex invalidate config
1 parent 83b1aa7 commit c5f95af

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

API.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1542,14 +1542,26 @@ const schema = Joi.object({
15421542
});
15431543
```
15441544
1545-
#### `string.regex(pattern, [name])`
1545+
#### `string.regex(pattern, [name | config])`
15461546
15471547
Defines a regular expression rule where:
15481548
- `pattern` - a regular expression object the string value must match against.
15491549
- `name` - optional name for patterns (useful with multiple patterns). Defaults to 'required'.
1550+
- `config` - an optional configuration object with the following supported properties:
1551+
- `name` - optional pattern name. Defaults to `required`.
1552+
- `invalidate` - optional boolean flag. Defaults to `false` behavior. If specified as `true`, the provided pattern will be disallowed instead of required.
15501553
15511554
```js
15521555
const schema = Joi.string().regex(/^[abc]+$/);
1556+
1557+
const namedSchema = Joi.string().regex(/[0-9]/, { name: 'numbers'});
1558+
namedSchema.validate('alpha'); // ValidationError: "value" with value "alpha" fails to match the numbers pattern
1559+
1560+
const invalidatedSchema = Joi.string().regex(/[a-z]/, { invalidate: true });
1561+
invalidatedSchema.validate('lowercase'); // ValidationError: "value" with value "lowercase" matches the invalidated pattern: [a-z]
1562+
1563+
const invalidatedNamedSchema = Joi.string().regex(/[a-z]/, { name: 'alpha', invalidate: true });
1564+
invalidatedNamedSchema.validate('lowercase'); // ValidationError: "value" with value "lowercase" matches the invalidated alpha pattern
15531565
```
15541566
15551567
#### `string.replace(pattern, replacement)`

0 commit comments

Comments
 (0)