Commit d02dc18
committed
string: Use 're.Pattern.search' instead of 're.Pattern.match'
Rust's 'Regex.is_match' behaves like Python's 're.Pattern.search',
here's a snippet from the docs of the former:
Returns true if and only if there is a match for the regex *anywhere*
in the haystack given.
Given the implementation of the regex matching mechanism for the
'validators.string.Pattern', this leads to an inconsistent behavior:
import re
from pydantic import BaseModel, Field
class A(BaseModel):
b: str = Field(pattern=r"[a-z]")
c: str = Field(pattern=re.compile(r"[a-z]"))
A.model_validate({"b": "Abc", "c": "Abc"})
In this snippet od code, 'b' will validate fine, but 'c' won't.
Since the test cases for string already establish the expected behavior
(the Rust's `Regex.is_match` case), let's use `re.Pattern.search`
instead of `re.Pattern.match` to unify the results when a `re.Pattern`
object is passed.
Signed-off-by: Marcin Sobczyk <[email protected]>1 parent 0e6b377 commit d02dc18
2 files changed
+3
-1
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
277 | 277 | | |
278 | 278 | | |
279 | 279 | | |
280 | | - | |
| 280 | + | |
281 | 281 | | |
282 | 282 | | |
283 | 283 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
80 | 80 | | |
81 | 81 | | |
82 | 82 | | |
| 83 | + | |
| 84 | + | |
83 | 85 | | |
84 | 86 | | |
85 | 87 | | |
| |||
0 commit comments