Exemplo n.º 1
0
class Model(BaseModel):
    lower_bytes: conbytes(to_lower=True)
    short_bytes: conbytes(min_length=2, max_length=10)
    strip_bytes: conbytes(strip_whitespace=True)

    lower_str: constr(to_lower=True)
    short_str: constr(min_length=2, max_length=10)
    regex_str: constr(regex=r'^apple (pie|tart|sandwich)$')
    strip_str: constr(strip_whitespace=True)

    big_int: conint(gt=1000, lt=1024)
    mod_int: conint(multiple_of=5)
    pos_int: PositiveInt
    neg_int: NegativeInt
    non_neg_int: NonNegativeInt
    non_pos_int: NonPositiveInt

    big_float: confloat(gt=1000, lt=1024)
    unit_interval: confloat(ge=0, le=1)
    mod_float: confloat(multiple_of=0.5)
    pos_float: PositiveFloat
    neg_float: NegativeFloat
    non_neg_float: NonNegativeFloat
    non_pos_float: NonPositiveFloat

    short_list: conlist(int, min_items=1, max_items=4)
    short_set: conset(int, min_items=1, max_items=4)

    decimal_positive: condecimal(gt=0)
    decimal_negative: condecimal(lt=0)
    decimal_max_digits_and_places: condecimal(max_digits=2, decimal_places=2)
    mod_decimal: condecimal(multiple_of=Decimal('0.25'))

    bigger_int: int = Field(..., gt=10000)
class ProductCreateRequest(ProductUpdateRequest):
    """
    Defines schema and validation for creation request.
    Mostly makes some fields required.
    """
    name: Name
    rating: Rating

    brand: BrandID
    categories: conset(CategoryID, min_items=1, max_items=5)

    items_in_stock: PositiveInt
class ProductUpdateRequest(BaseModel):
    """
    Defines schema and validation for update request.
    """
    name: Optional[Name]
    rating: Optional[Rating]
    featured: Optional[bool]

    receipt_date: Optional[datetime]
    expiration_date: Optional[datetime]

    brand: Optional[BrandID]
    categories: Optional[conset(CategoryID, min_items=1, max_items=5)]

    items_in_stock: Optional[PositiveInt]

    @validator("receipt_date", "expiration_date", pre=True)
    def parse_rfc_1123_datetime(cls, date: Any):
        """
        Parse string value as date in RFC 1123 format.
        @param date: date with unknown type, will be processed if string. Otherwise validator is skipped.
        @return: datetime object or string (if unable to parse as RFC 1123 formatted string)
        """
        if isinstance(date, str):
            parsed_datetime = email_utils.parsedate_to_datetime(date)
            if parsed_datetime is None:
                return date
            return parsed_datetime
        return date

    @validator("expiration_date")
    def validate_expiration_date(cls, expiration_date: datetime):
        """
        Makes sure expiration date is valid (according to acceptance criteria 3).
        Throws ValueError if expiration date in less then defined by MINIMAL_EXPIRATION.

        @param expiration_date: expiration date
        @return: expiration date if it is valid
        """

        if isinstance(expiration_date, datetime):
            today = datetime.utcnow()
            time_to_expire = expiration_date - today
            if time_to_expire < MINIMAL_EXPIRATION:
                raise ValueError(
                    f"can't set expiration in less then {MINIMAL_EXPIRATION} days"
                )

        return expiration_date
Exemplo n.º 4
0
    def test_pydantic_conset_str_field(self, normalize_sdl):
        not_implemented = str(pydantic.VERSION)[:3] in {
            "1.5",
            "1.4",
            "1.3",
            "1.2",
            "1.1",
            "1.0",
        }
        if not_implemented:
            # AttributeError: module 'pydantic' has no attribute 'conset'
            # Pydantic versions < 1.6 return error when using conset
            return

        value = pydantic2graphene.to_graphene(
            to_pydantic_class(pydantic.conset(str, min_items=1, max_items=4)))
        expected_value = """
            type FakeGql {
                field: [String!]!
            }
        """
        assert normalize_sdl(value) == normalize_sdl(expected_value)
Exemplo n.º 5
0
class QuizForm(BaseModel):
    grade_name: int
    name: constr(min_length=1)
    cafe_name: conset(int, min_items=1, max_items=2)
    feedback: Optional[str]