From 8417d199b74de795681b439420bf5be0ef1e4839 Mon Sep 17 00:00:00 2001 From: Yuri Khan Date: Sun, 21 Jun 2020 19:49:41 +0700 Subject: [PATCH] Document default `regex` anchoring semantics (#1631) --- changes/1648-yurikhan.md | 1 + docs/usage/schema.md | 17 +++++++++++++++++ 2 files changed, 18 insertions(+) create mode 100644 changes/1648-yurikhan.md diff --git a/changes/1648-yurikhan.md b/changes/1648-yurikhan.md new file mode 100644 index 0000000000..e5ed04818f --- /dev/null +++ b/changes/1648-yurikhan.md @@ -0,0 +1 @@ +Document default `regex` anchoring semantics diff --git a/docs/usage/schema.md b/docs/usage/schema.md index 028e7731f2..08916661b4 100644 --- a/docs/usage/schema.md +++ b/docs/usage/schema.md @@ -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