Multiple pattern properties are not handled correctly
See original GitHub issueDescribe the bug
JSON schema pattern properties can provide multiple patterns where any object property key must match one of the patterns. This is probably not handled correctly by models generated from such a schema.
To Reproduce
Example schema:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Foo",
"type": "object",
"properties": {
"bar": {
"type": "object",
"patternProperties": {
"^A_\\d$": {
"type": "string"
},
"^B_\\d$": {
"type": "string"
}
}
}
}
}
Used commandline:
$ datamodel-codegen --input tests/data/jsonschema/pattern_properties.json
Expected behavior The given command should generate a model that handles this example data:
{
"bar": {
"A_1": "a",
"B_2": "b"
}
}
However, it will fail with the following error message:
pydantic.error_wrappers.ValidationError: 2 validation errors for Foo
bar -> __key__
string does not match regex "^A_\d$" (type=value_error.str.regex; pattern=^A_\d$)
bar -> __key__
string does not match regex "^B_\d$" (type=value_error.str.regex; pattern=^B_\d$)
Version:
- OS: Ubuntu 20.04
- Python version: 3.8.10
- datamodel-code-generator version: 0.11.15
Additional context Add any other context about the problem here.
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (1 by maintainers)
Top Results From Across the Web
Correctly handling multiple views in a MVC javascript game
I think the ViewModel method is the way to go. I now have the following: UniverseDomainObject (and other miscellaneous Domain objects like ...
Read more >Pattern matching overview - C# guide | Microsoft Learn
The two relational patterns are surrounded by parentheses, which you can use around any pattern for clarity. The final two switch arms handle...
Read more >Everything you need to know about Regular Expressions
Most characters in a regex pattern do not have a special meaning, ... In many regex engines — such as Java, JavaScript, Python, ......
Read more >Python's property(): Add Managed Attributes to Your Classes
In this example, you create Point with two non-public attributes ._x and ._y to hold the Cartesian coordinates of the point at hand....
Read more >Pattern (Java Platform SE 7 ) - Oracle Help Center
Instances of this class are immutable and are safe for use by multiple concurrent threads. Instances of the Matcher class are not safe...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
So the generator comes up with something like
However, the correct way is
So
Union
insideDict
, not the other way around. I’ll try to come up with a fix for that.@georgms Thank you for creating PR and the issue.
We should discuss the best way in the pydantic project.