Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /tyvekzhang_fastlib /fastlib /utils /validate_util.py
| # SPDX-License-Identifier: MIT | |
| """ | |
| Validation service module for Pydantic models. | |
| """ | |
| from pydantic import BaseModel, ValidationError | |
| class ValidateService: | |
| """A service class providing validation utilities for Pydantic models.""" | |
| def validate(obj: BaseModel) -> str | None: | |
| """Validate a Pydantic model instance. | |
| Args: | |
| obj: The Pydantic BaseModel instance to validate. | |
| Returns: | |
| Optional[str]: None if validation passes, otherwise a comma-separated string | |
| of validation error messages. | |
| """ | |
| try: | |
| # Call Pydantic's validation logic (for Pydantic v2.x) | |
| obj.model_validate() # For v2.x Pydantic | |
| return None | |
| except ValidationError as e: | |
| # Concatenate error messages | |
| errors = ", ".join( | |
| [ | |
| f"{'->'.join(map(str, error['loc']))}: {error['msg']}" | |
| for error in e.errors() | |
| ] | |
| ) | |
| return errors | |
| def get_validate_err_msg(e: ValidationError) -> str | None: | |
| """Format validation error messages from a ValidationError exception. | |
| Args: | |
| e: The ValidationError exception containing validation errors. | |
| Returns: | |
| Optional[str]: A comma-separated string of formatted error messages, | |
| or None if no errors exist. | |
| """ | |
| err_msg = [] | |
| for error in e.errors(): | |
| field = " -> ".join(map(str, error["loc"])) # Field path | |
| message = error["message"] # Error description | |
| err_msg.append(f"Error in field '{field}': {message}") | |
| return ",".join(err_msg) | |
Xet Storage Details
- Size:
- 1.74 kB
- Xet hash:
- 27dc943732ce232bd023e8d5c97d52eede9bc4bf281febb903c84e0b64e7e7d6
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.