10000 Document default `regex` anchoring semantics (#1631) (#1648) · ag-python/pydantic@908f6ed · GitHub
[go: up one dir, main page]

Skip to content

Commit 908f6ed

Browse files
authored
Document default regex anchoring semantics (pydantic#1631) (pydantic#1648)
1 parent d5e1c88 commit 908f6ed

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

changes/1648-yurikhan.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Document default `regex` anchoring semantics

docs/usage/schema.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,23 @@ It has the following arguments:
6969
JSON Schema
7070
* `regex`: for string values, this adds a Regular Expression validation generated from the passed string and an
7171
annotation of `pattern` to the JSON Schema
72+
73+
!!! note
74+
*pydantic* validates strings using `re.match`,
75+
which treats regular expressions as implicitly anchored at the beginning.
76+
On the contrary,
77+
JSON Schema validators treat the `pattern` keyword as implicitly unanchored,
78+
more like what `re.search` does.
79+
80+
For interoperability, depending on your desired behavior,
81+
either explicitly anchor your regular expressions with `^`
82+
(e.g. `^foo` to match any string starting with `foo`),
83+
or explicitly allow an arbitrary prefix with `.*?`
84+
(e.g. `.*?foo` to match any string containig the substring `foo`).
85+
86+
See [#1631](https://github.com/samuelcolvin/pydantic/issues/1631)
87+
for a discussion of possible changes to *pydantic* behavior in **v2**.
88+
7289
* `**` any other keyword arguments (e.g. `examples`) will be added verbatim to the field's schema
7390

7491
Instead of using `Field`, the `fields` property of [the Config class](model_config.md) can be used

0 commit comments

Comments
 (0)
0