--field-constraints add decimal points for integers constraints
See original GitHub issueDescribe the bug An option --field-constraints add decimal points for integers constraints
To Reproduce
Example schema:
{
"type": "object",
"properties": {
"dataSize": {
"type": "integer",
"minimum": 10,
"maximum": 64
}
},
}
from __future__ import annotations
from typing import Optional
from pydantic import BaseModel, Field
class Model(BaseModel):
dataSize: Optional[int] = Field(None, ge=10.0, le=64.0)
Used commandline:
$ datamodel-codegen --input model.json --input-file-type jsonschema --field-constraints > model.py
Expected behavior Expected ge=10 not ge=10.0, same for le dataSize: Optional[int] = Field(None, ge=10, le=64)
Issue Analytics
- State:
- Created 2 years ago
- Reactions:3
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Definitive Way to Constrain Response to X Decimal Places
I'm at my wit's end and need some help coming up with a definitive solution to this problem that I've seen pop up...
Read more >Can you enter a constraint value with decimal in IB?
As of Xcode 6.3, constraint constant values seem to be restricted to integers in the storyboard editor (unlike multiplier values, which can be...
Read more >11.1.3 Fixed-Point Types (Exact Value) - DECIMAL, NUMERIC
When such a column is assigned a value with more digits following the decimal point than are permitted by the specified scale, the...
Read more >Understanding the SQL Decimal data type - SQLShack
One of the easiest workarounds is to increase the precision level of the columns to store bigger numbers. We can alter the data...
Read more >Number fields | Blockly - Google Developers
On this page · Creation · Serialization · Constraints. Minimum value; Maximum value; Rounding · Common constraints. Positive numbers; Integers.
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
@koxudaxi Thanks for looking into it. I also found that section of code when investigating this issue. I considered that maybe we can change the type annotations there to
But this idea doesn’t work because of an upstream Pydantic issue, samuelcolvin/pydantic#1423. And even if that worked, it would cause some questionable behavior, e.g.:
would result in:
where instead, one would maybe expect:
I think ideally the type of
gt
,ge
,lt
, andle
should match the structural type of the value being constrained. That is, if the schema specifies a value as an integer, its range bounds should be integers, and if the schema specifies a value as a float, its range bounds should be floats.But this would be difficult because the class
Constraints
is defined independently from the specific value being constrained.Also, it is somewhat unclear how to handle this in the case where the schema specifies a union type that contains int and/or float. Right now, we have the following behavior, but I don’t know that it really makes sense, nor am I sure how other consumers of the jsonschema format treat this kind of schema:
becomes
I.e. should 10.0 and 64.0 be used here, or 10 and 64? I don’t know. 😅
I hope the problem was fixed.