Skip to content

Commit

Permalink
Document default regex anchoring semantics (#1631)
Browse files Browse the repository at this point in the history
  • Loading branch information
yurikhan committed Jun 21, 2020
1 parent 0cee311 commit 8417d19
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
1 change: 1 addition & 0 deletions changes/1648-yurikhan.md
@@ -0,0 +1 @@
Document default `regex` anchoring semantics
17 changes: 17 additions & 0 deletions docs/usage/schema.md
Expand Up @@ -69,6 +69,23 @@ It has the following arguments:
JSON Schema
* `regex`: for string values, this adds a Regular Expression validation generated from the passed string and an
annotation of `pattern` to the JSON Schema

!!! note
*pydantic* validates strings using `re.match`,
which treats regular expressions as implicitly anchored at the beginning.
On the contrary,
JSON Schema validators treat the `pattern` keyword as implicitly unanchored,
more like what `re.search` does.

For interoperability, depending on your desired behavior,
either explicitly anchor your regular expressions with `^`
(e.g. `^foo` to match any string starting with `foo`),
or explicitly allow an arbitrary prefix with `.*?`
(e.g. `.*?foo` to match any string containig the substring `foo`).

See [#1631](https://github.com/samuelcolvin/pydantic/issues/1631)
for a discussion of possible changes to *pydantic* behavior in **v2**.

* `**` any other keyword arguments (e.g. `examples`) will be added verbatim to the field's schema

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

0 comments on commit 8417d19

Please sign in to comment.