Skip to content

Commit

Permalink
Run payload validation as part of dataset endpoints (#319)
Browse files Browse the repository at this point in the history
  • Loading branch information
florimondmanca authored Jun 30, 2022
1 parent 199afcb commit 1e1cd58
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 79 deletions.
2 changes: 0 additions & 2 deletions server/api/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from server.config import Settings
from server.config.di import resolve

from . import errors
from .auth.middleware import AuthMiddleware
from .resources import auth_backend
from .routes import router
Expand All @@ -32,7 +31,6 @@ def create_app(settings: Settings = None) -> App:
app = App(
debug=settings.debug,
docs_url=settings.docs_url,
exception_handlers=errors.exception_handlers,
)

app.add_middleware(
Expand Down
8 changes: 6 additions & 2 deletions server/api/datasets/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
from fastapi import Query
from pydantic import BaseModel, EmailStr, Field

from server.application.datasets.validation import (
CreateDatasetValidationMixin,
UpdateDatasetValidationMixin,
)
from server.domain.common.types import ID
from server.domain.datasets.entities import (
DataFormat,
Expand Down Expand Up @@ -34,7 +38,7 @@ def __init__(
self.tag_id = tag_id


class DatasetCreate(BaseModel):
class DatasetCreate(CreateDatasetValidationMixin, BaseModel):
title: str
description: str
service: str
Expand All @@ -49,7 +53,7 @@ class DatasetCreate(BaseModel):
tag_ids: List[ID] = Field(default_factory=list)


class DatasetUpdate(BaseModel):
class DatasetUpdate(UpdateDatasetValidationMixin, BaseModel):
title: str
description: str
service: str
Expand Down
24 changes: 0 additions & 24 deletions server/api/errors.py

This file was deleted.

56 changes: 5 additions & 51 deletions server/application/datasets/commands.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import datetime as dt
from typing import List, Optional

from pydantic import EmailStr, Field, validator
from pydantic import EmailStr, Field

from server.domain.common.types import ID
from server.domain.datasets.entities import (
Expand All @@ -11,8 +11,10 @@
)
from server.seedwork.application.commands import Command

from .validation import CreateDatasetValidationMixin, UpdateDatasetValidationMixin

class CreateDataset(Command[ID]):

class CreateDataset(CreateDatasetValidationMixin, Command[ID]):
title: str
description: str
service: str
Expand All @@ -26,20 +28,8 @@ class CreateDataset(Command[ID]):
published_url: Optional[str] = None
tag_ids: List[ID] = Field(default_factory=list)

@validator("formats")
def check_formats_at_least_one(cls, value: List[DataFormat]) -> List[DataFormat]:
if not value:
raise ValueError("formats must contain at least one item")
return value

@validator("contact_emails")
def check_contact_emails_at_least_one(cls, value: List[str]) -> List[str]:
if not value:
raise ValueError("contact_emails must contain at least one item")
return value


class UpdateDataset(Command[None]):
class UpdateDataset(UpdateDatasetValidationMixin, Command[None]):
id: ID
title: str
description: str
Expand All @@ -54,42 +44,6 @@ class UpdateDataset(Command[None]):
published_url: Optional[str] = Field(...)
tag_ids: List[ID]

@validator("title")
def check_title_not_empty(cls, value: str) -> str:
if not value:
raise ValueError("title must not be empty")
return value

@validator("description")
def check_description_not_empty(cls, value: str) -> str:
if not value:
raise ValueError("description must not be empty")
return value

@validator("service")
def check_service_not_empty(cls, value: str) -> str:
if not value:
raise ValueError("service must not be empty")
return value

@validator("formats")
def check_formats_at_least_one(cls, value: List[DataFormat]) -> List[DataFormat]:
if not value:
raise ValueError("formats must contain at least one item")
return value

@validator("contact_emails")
def check_contact_emails_at_least_one(cls, value: List[str]) -> List[str]:
if not value:
raise ValueError("contact_emails must contain at least one item")
return value

@validator("published_url")
def check_published_url_not_empty(cls, value: Optional[str]) -> Optional[str]:
if value is not None and not value:
raise ValueError("published_url must not be empty")
return value


class DeleteDataset(Command[None]):
id: ID
57 changes: 57 additions & 0 deletions server/application/datasets/validation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
from typing import List, Optional

from pydantic import BaseModel, validator

from server.domain.datasets.entities import DataFormat


class CreateDatasetValidationMixin(BaseModel):
@validator("formats", check_fields=False)
def check_formats_at_least_one(cls, value: List[DataFormat]) -> List[DataFormat]:
if not value:
raise ValueError("formats must contain at least one item")
return value

@validator("contact_emails", check_fields=False)
def check_contact_emails_at_least_one(cls, value: List[str]) -> List[str]:
if not value:
raise ValueError("contact_emails must contain at least one item")
return value


class UpdateDatasetValidationMixin(BaseModel):
@validator("title", check_fields=False)
def check_title_not_empty(cls, value: str) -> str:
if not value:
raise ValueError("title must not be empty")
return value

@validator("description", check_fields=False)
def check_description_not_empty(cls, value: str) -> str:
if not value:
raise ValueError("description must not be empty")
return value

@validator("service", check_fields=False)
def check_service_not_empty(cls, value: str) -> str:
if not value:
raise ValueError("service must not be empty")
return value

@validator("formats", check_fields=False)
def check_formats_at_least_one(cls, value: List[DataFormat]) -> List[DataFormat]:
if not value:
raise ValueError("formats must contain at least one item")
return value

@validator("contact_emails", check_fields=False)
def check_contact_emails_at_least_one(cls, value: List[str]) -> List[str]:
if not value:
raise ValueError("contact_emails must contain at least one item")
return value

@validator("published_url", check_fields=False)
def check_published_url_not_empty(cls, value: Optional[str]) -> Optional[str]:
if value is not None and not value:
raise ValueError("published_url must not be empty")
return value

0 comments on commit 1e1cd58

Please sign in to comment.