No project description provided
Project description
Core-Validator
ErrorSchema
Use core_validator.ErrorSchema if you don't know which format you're gonna use.
Quick Start
DTO
class CommentDto(BaseModel):
comment: str
post_id: int
owner_id: int
Add error source (error context)
class Source(BaseModel):
local: str
DTO Validator
@dataclasses.dataclass()
class CommentValidator(Validator[CommentDto, ErrorSchema[Source]]):
@validate
async def test1(self):
post_ids = list(range(1, 10))
if self.data.post_id not in post_ids:
self.context.add_error(
ErrorSchema(
code=ErrorCodeEnum.not_found.value,
message="Id doen't not exists",
detail=f"Post with id={self.data.post_id} not found",
source=Source(
local="data/post_id",
),
)
)
@validate
@pre_state(lambda self: self.data.owner_id > 0, negative_id_error)
async def test2(self):
owner_ids = list(range(1, 10))
if self.data.owner_id not in owner_ids:
self.context.add_error(
ErrorSchema(
code=ErrorCodeEnum.not_found.value,
message="Id doen't not exists",
detail=f"User with id={self.data.post_id} not found",
source=Source(
local="data/owner_id",
),
)
)
Use
async def function():
validator = CommentValidator()
errors = await validator.errors(comment)
# or
await validator.validate() # raise ValidationError
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
core_validator-0.1.0.tar.gz
(5.6 kB
view hashes)
Built Distribution
Close
Hashes for core_validator-0.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0d00992696087215a0e45de59dd2bb942d20dffece7c256e85e588632c60f98e |
|
MD5 | 361ce5c3d0574e0d7429e2492d5859f0 |
|
BLAKE2b-256 | c415dfa0186b4ad768dcf555fc454edbbeab70544f8e026a3ab9ccd04d66de84 |