bauerji / flask-pydantic

flask extension for integration with the awesome pydantic package
MIT License
352 stars 56 forks source link

pydantic tip... #62

Closed bbartling closed 1 year ago

bbartling commented 1 year ago

Would anyone be able to give me a Pydantic tip at all?

Is it possible on a pydantic model to reference another class? For example below in the ReadRequestModel in point_type I am trying to figure out if its possible reference that only these "types of points" in a string format can be chosen:

# type-of-points
# just for reference
multiStateValue
multiStateInput
multiStateOutput
analogValue
analogInput
analogOutput
binaryValue
binaryInput
binaryOutput

And depending on what point_type is that depics what type of point_id can be chosen that I am trying to reference in the PointType class twice below that is obviously very wrong. Any tips greatly appreciated...

from typing import List, Literal, Optional
from pydantic import BaseModel

BOOLEAN_ACTION_MAPPING = Literal["active", "inactive"]

class ReadRequestModel(BaseModel):
    device_address: str
    point_type: PointType  <--- not correct
    point_id: PointType    <--- not correct

class PointType(BaseModel):
    multiStateValue: Optional[int]
    multiStateInput: Optional[int]
    multiStateOutput: Optional[int]
    analogValue: Optional[int]
    analogInput: Optional[int]
    analogOutput: Optional[int]
    binaryValue: Optional[BOOLEAN_ACTION_MAPPING]
    binaryInput: Optional[BOOLEAN_ACTION_MAPPING]
    binaryOutput: Optional[BOOLEAN_ACTION_MAPPING]

r = ReadRequestModel({'device_address': '12345:5',
                     'point_type': 'analogInput',
                      'point_id': 8})

print(r)