Fastapi: Is it possible to define a value within a response model that contains at least 2 decimal digits [QUESTION]

Created on 10 Dec 2019  路  7Comments  路  Source: tiangolo/fastapi

Description

Is it possible to define a value within a response model that contains at least 2 decimal digits.?

Additional context

The response model I am talking about looks like this:

class WindData(BaseModel):
    speed: Union[List[float], List[None]] = Schema(
        None,
        title="Wind Speed [m/s]")
    dir: Union[List[float], List[None]] = Schema(
        None,
        title="Wind Direction [掳]")
    gust: Union[List[float], List[None]] = Schema(
        None,
        title="Wind Speed [m/s]")

And the values should have maximal 2 decimal digits. I hope you can help me

Thanks a lot !

question

Most helpful comment

Yeah, there is also another challenge with decimal that pydantic json-encodes it to a float rather than a string (which can come with some float rounding issues if dumped and re-encoded).

All 7 comments

This could probably be done by using a validator that rounds inputs to the nearest 0.01? If you need to preserve the original value then things would get more complicated.

If actual decimal values matter, you should probably be using Decimal instead of float. As for precision, If you want to require a certain amount of decimals so that 12 != 12.00 without doing that sort of validation on the incoming strings by hand, Decimal has a same_quantum test that validates that both numbers have the same exponent value.

import Decimal from decimal

Decimal("2.2").same_quantum(Decimal("1.00")) # False
Decimal("2.20").same_quantum(Decimal("1.00")) # True
Decimal("2.200").same_quantum(Decimal("1.00")) # False

You would still need to write a custom validator for this, but you don't risk accidentally adding significant figures to the input data.

If you just want to limit allowed precision but not forbid values with a different level of precision, you can instead quantize your decimal values to ensure they're rounded at the precision level you want.

Thanks @dmontagu and @sm-Fifteen ! :bowing_man: :cake:

Does that solve your question @meteoDaniel ?

I should probably mention that Pydantic has had max_digits and decimal_places restrictions for its condecimal type since very early versions (though those barely get a mention in the current documentation), which can be used together to have a constraint similar to NUMERIC(precision, scale) in SQL. There's no way to require a minimum or exact number of significant figures, though, at least not without a custom validator.

Yeah, there is also another challenge with decimal that pydantic json-encodes it to a float rather than a string (which can come with some float rounding issues if dumped and re-encoded).

Yep, good points.

@meteoDaniel if that solved your question, may we close this issue?

Assuming the original issue was solved, it will be automatically closed now. But feel free to add more comments or create new issues.

Was this page helpful?
0 / 5 - 0 ratings