fix: added strict validation to all ingress ints

This commit is contained in:
ITQ
2025-01-24 18:09:37 +03:00
parent ead34fd867
commit fe2bc038a9
3 changed files with 15 additions and 16 deletions
+6 -8
View File
@@ -3,7 +3,7 @@ import uuid
from typing import ClassVar, Literal from typing import ClassVar, Literal
from ninja import ModelSchema, Schema from ninja import ModelSchema, Schema
from pydantic import Field from pydantic import Field, StrictInt
from pydantic_extra_types.country import CountryAlpha2 from pydantic_extra_types.country import CountryAlpha2
from apps.business.models import Business from apps.business.models import Business
@@ -40,26 +40,24 @@ class BusinessSignInOut(Schema):
class PromocodeTarget(ModelSchema): class PromocodeTarget(ModelSchema):
categories: list[str] | None = None categories: list[str] | None = None
country: str | None = None age_from: StrictInt | None = None
age_until: StrictInt | None = None
class Meta: class Meta:
model = PromocodeTarget model = PromocodeTarget
fields: ClassVar[list[str]] = [ fields: ClassVar[list[str]] = [PromocodeTarget.country.field.name]
PromocodeTarget.age_from.field.name,
PromocodeTarget.age_until.field.name,
]
class CreatePromocodeIn(ModelSchema): class CreatePromocodeIn(ModelSchema):
target: PromocodeTarget target: PromocodeTarget
promo_unique: list[str] | None = None promo_unique: list[str] | None = None
max_count: StrictInt
class Meta: class Meta:
model = Promocode model = Promocode
fields: ClassVar[list[str]] = [ fields: ClassVar[list[str]] = [
Promocode.description.field.name, Promocode.description.field.name,
Promocode.image_url.field.name, Promocode.image_url.field.name,
Promocode.max_count.field.name,
Promocode.active_from.field.name, Promocode.active_from.field.name,
Promocode.active_until.field.name, Promocode.active_until.field.name,
Promocode.mode.field.name, Promocode.mode.field.name,
@@ -109,7 +107,7 @@ class PatchPromocodeIn(Schema):
description: str | None = None description: str | None = None
image_url: str | None = None image_url: str | None = None
target: PromocodeTarget | None = None target: PromocodeTarget | None = None
max_count: int | None = None max_count: StrictInt | None = None
active_from: datetime.date | None = None active_from: datetime.date | None = None
active_until: datetime.date | None = None active_until: datetime.date | None = None
+6 -6
View File
@@ -176,7 +176,7 @@ def list_promocode(
promocodes = promocodes[filters.offset : filters.offset + filters.limit] promocodes = promocodes[filters.offset : filters.offset + filters.limit]
return [ return status.OK, [
utils.map_promocode_to_schema(promocode) for promocode in promocodes utils.map_promocode_to_schema(promocode) for promocode in promocodes
] ]
@@ -192,7 +192,7 @@ def list_promocode(
) )
def get_promocode( def get_promocode(
request: HttpRequest, promocode_id: str request: HttpRequest, promocode_id: str
) -> schemas.PromocodeViewOut: ) -> tuple[int, schemas.PromocodeViewOut]:
business = request.auth business = request.auth
promocodes = Promocode.objects.filter(id=promocode_id) promocodes = Promocode.objects.filter(id=promocode_id)
@@ -218,7 +218,7 @@ def get_promocode(
promocode = promocodes.first() promocode = promocodes.first()
return utils.map_promocode_to_schema(promocode) return status.OK, utils.map_promocode_to_schema(promocode)
@router.patch( @router.patch(
@@ -234,7 +234,7 @@ def patch_promocode(
request: HttpRequest, request: HttpRequest,
promocode_id: str, promocode_id: str,
patched_fields: schemas.PatchPromocodeIn, patched_fields: schemas.PatchPromocodeIn,
) -> schemas.PromocodeViewOut: ) -> tuple[status.OK, schemas.PromocodeViewOut]:
business = request.auth business = request.auth
promocodes = Promocode.objects.filter(id=promocode_id) promocodes = Promocode.objects.filter(id=promocode_id)
@@ -275,7 +275,7 @@ def patch_promocode(
promocode.save() promocode.save()
return utils.map_promocode_to_schema(promocode) return status.OK, utils.map_promocode_to_schema(promocode)
@router.get( @router.get(
@@ -289,7 +289,7 @@ def patch_promocode(
) )
def promocode_stat( def promocode_stat(
request: HttpRequest, promocode_id: str request: HttpRequest, promocode_id: str
) -> schemas.PromocodeStats: ) -> tuple[int, schemas.PromocodeStats]:
business = request.auth business = request.auth
promocodes = Promocode.objects.filter(id=promocode_id) promocodes = Promocode.objects.filter(id=promocode_id)
+3 -2
View File
@@ -2,16 +2,17 @@ import uuid
from typing import ClassVar from typing import ClassVar
from ninja import ModelSchema, Schema from ninja import ModelSchema, Schema
from pydantic import Field from pydantic import Field, StrictInt
from apps.user.models import User from apps.user.models import User
class UserTarget(ModelSchema): class UserTarget(ModelSchema):
age: StrictInt
class Meta: class Meta:
model = User model = User
fields: ClassVar[list[str]] = [ fields: ClassVar[list[str]] = [
User.age.field.name,
User.country.field.name, User.country.field.name,
] ]