Skip to content

Commit 91e81b8

Browse files
committed
feat: start add schemas support
1 parent 6c22c14 commit 91e81b8

File tree

9 files changed

+99
-13
lines changed

9 files changed

+99
-13
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
commit 6c22c1440b0454a8295c637fe1d07d7848d198c8
2+
Author: Alexeev Bronislav <[email protected]>
3+
Date: Sat May 3 13:11:31 2025 +0700
4+
5+
fix: fix bugs, docstrings
6+
17
commit 8e25c30dcb41d13a213c0e40904f76d0dc015bb2
28
Author: Alexeev Bronislav <[email protected]>
39
Date: Sat May 3 00:26:30 2025 +0700

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ Welcome to **EchoNext**, where innovation meets simplicity! Are you tired of the
5959

6060
**Imagine** a lightweight framework that empowers you to create modern web applications with lightning speed and flexibility. With EchoNext, you're not just coding; you're building a masterpiece!
6161

62-
> Last stable version: 0.7.16 alpha
62+
> Last stable version: 0.7.17 alpha
6363
6464
> Next Big Update: ASYNC & unicorn support
6565
@@ -117,6 +117,7 @@ Welcome to **EchoNext**, where innovation meets simplicity! Are you tired of the
117117
+ performance
118118
+ slugger
119119
+ permissions
120+
+ pyEchoNext Schemas (`pyechonext.schemas`) and pydantic support for validating schemas
120121

121122
<p align="right">(<a href="#readme-top">back to top</a>)</p>
122123

SECURITY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ currently being supported with security updates.
77

88
| Version | Supported |
99
|---------| ------------------ |
10+
| 0.7.17 | :white_check_mark: |
1011
| 0.7.16 | :white_check_mark: |
1112
| 0.7.15 | :white_check_mark: |
1213
| 0.7.14 | :white_check_mark: |

docs/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
project = "pyEchoNext"
1111
author = "name"
12-
version = "0.7.16"
12+
version = "0.7.17"
1313
release = "0.7"
1414
project_copyright = "2025, Alexeev Bronislaw"
1515

pyechonext/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
from rich import print
2323
from rich.traceback import install
2424

25-
__version__ = "0.7.15"
25+
__version__ = "0.7.17"
2626
install(show_locals=True)
2727

2828

pyechonext/schemas/__init__.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +0,0 @@
1-
from functools import wraps
2-
3-
4-
def field_validation(handler, cls, value):
5-
@wraps(handler)
6-
def wrapper():
7-
result = handler(cls, value)
8-
return result
9-
10-
return wrapper

pyechonext/schemas/fields.py

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# дай угадаю, кто то украл твой сладкий рулет?
2+
from typing import Any, Optional, Dict, Optional, Callable, Union, Type, List
3+
4+
5+
class BaseField:
6+
"""This class describes the Base Field.
7+
8+
Field is a one input data. Field must be have a:
9+
1. Datatype - any Type
10+
2. Default value (only if this field is optional)
11+
3. max_length - max length of value for field
12+
4. desc - description text of field
13+
5. exclude - exclude this field from model json
14+
6. transformer - a transformer (convert value datatype) callable object
15+
7. dependencies - fields-dependencies
16+
8. name - name of field
17+
"""
18+
19+
def __init__(self,
20+
name: str,
21+
datatype: Type,
22+
nullable: bool = False,
23+
default: Optional[Any] = None,
24+
max_length: Optional[int] = None,
25+
desc: Optional[str] = None,
26+
exclude: bool = False,
27+
transformer: Optional[Callable[[Any], Any]] = None,
28+
dependencies: Optional[List[str]] = None) -> None:
29+
self.name = name
30+
self.nullable = nullable
31+
self.datatype: Type = datatype
32+
self.default: Any | None = default
33+
self.max_length: int | None = max_length
34+
self.desc: str | None = desc
35+
self.exclude: bool = exclude
36+
self.transformer: Callable[[Any], Any] | None = transformer
37+
self.dependencies: List[str] | None = dependencies
38+
39+
def fast_validate(self, value: Any) -> bool:
40+
"""Fast validation of field value
41+
42+
Args:
43+
value (Any): value of field.
44+
45+
Returns:
46+
bool: True if all conditions is right, otherwise False
47+
"""
48+
conditions = [
49+
isinstance(value, self.datatype),
50+
len(value) <= max_length
51+
]
52+
53+
return all(conditions)
54+
55+
def validate(self,
56+
value: Any,
57+
context: Optional[Dict[str, Any]] = None) -> Any:
58+
"""Validate field value
59+
60+
Args:
61+
value (Any): value for validating
62+
context (Dict[str, Any]): context with data for validate dependenc fields. Defaults to None.
63+
64+
Raises:
65+
ValueError: is field is None (and not nullable) or field have invalid dependencies
66+
67+
Returns:
68+
Any: validated and transformed value
69+
"""
70+
if value is None and self.default is None and not nullable:
71+
raise ValueError(f'Field "{self.name}" is required and must not be none')
72+
73+
if not isinstance(value, self.datatype):
74+
value = self._type_case(value)
75+
76+
self._check_type(value)
77+
self._check_length(value)
78+
79+
if self.transformer:
80+
value = self.transformer(value)
81+
82+
if context:
83+
for dependency in self.dependencies:
84+
if dependency in context and not self._validate_dependency(value, context[dependency]):
85+
raise ValueError(f"Field '{self.name}' is invalid based on dependency {dependency}.")
86+
87+
return value
88+

pyechonext/schemas/models.py

Whitespace-only changes.

pyechonext/schemas/validators.py

Whitespace-only changes.

0 commit comments

Comments
 (0)