Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document default regex anchoring semantics (#1631) #1648

Merged
merged 1 commit into from Jun 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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`).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo here: containig -> containing

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry! Do I open a new PR or what?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess so, only needs to be a tiny fix.


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