Skip to content

Customize bad request payload handling with dataclasses #339

Answered by RobertoPrevato
techntools asked this question in Q&A
Discussion options

You must be logged in to vote

@techntools I prepared an example for you, using Marshmallow. Implementing a generic solution to validate input and produce user friendly error messages is not in the scope of BlackSheep, I recommend using Marshmallow. This is currently possible defining a couple of custom binders, a custom exception, and a custom exception handler like in the example below.

from typing import Any, TypeVar

from marshmallow import Schema, ValidationError, fields

from blacksheep import Application
from blacksheep.messages import Request
from blacksheep.server.bindings import Binder, BoundValue
from blacksheep.server.responses import pretty_json

SchemaType = TypeVar("SchemaType", bound=Schema)


class Inv…

Replies: 3 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by techntools
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants