Skip to content

Commit 71b2bc8

Browse files
committed
refactor: just renaming things
1 parent e779b85 commit 71b2bc8

File tree

12 files changed

+30
-62
lines changed

12 files changed

+30
-62
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ Ensure your incoming data is valid by defining schemas. If validation fails, Web
160160
Define a schema by inheriting from `Schema` and adding fields.
161161

162162
```python
163-
from webspark.schema import Schema, StringField, IntegerField, EmailField
163+
from webspark.validation import Schema, StringField, IntegerField, EmailField
164164

165165
class UserSchema(Schema):
166166
name = StringField(required=True, max_length=100)
@@ -376,7 +376,7 @@ You can apply the SchemaPlugin using the `@apply` decorator from `webspark.utils
376376
from webspark.contrib.plugins import SchemaPlugin
377377
from webspark.core import View
378378
from webspark.http import Context
379-
from webspark.schema import Schema, StringField, IntegerField
379+
from webspark.validation import Schema, StringField, IntegerField
380380
from webspark.utils import apply
381381

382382
# Define a schema for validation

examples/database_example.py

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
from webspark.core import View, WebSpark, path
1313
from webspark.http import Context
14-
from webspark.schema import IntegerField, Schema, StringField
1514
from webspark.utils import HTTPException
1615

1716
# Database configuration
@@ -45,19 +44,6 @@ def get_db_connection():
4544
conn.close()
4645

4746

48-
# Schema for product creation
49-
class ProductSchema(Schema):
50-
name = StringField(required=True, max_length=100)
51-
description = StringField(max_length=500)
52-
price = IntegerField(min_value=0)
53-
54-
55-
class ProductUpdateSchema(Schema):
56-
name = StringField(max_length=100)
57-
description = StringField(max_length=500)
58-
price = IntegerField(min_value=0)
59-
60-
6147
class ProductListView(View):
6248
"""Handle operations on the collection of products."""
6349

examples/schema_example.py

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,16 @@
88

99
from webspark.core import View, WebSpark, path
1010
from webspark.http import Context
11-
from webspark.schema import (
12-
BooleanField,
13-
EmailField,
14-
IntegerField,
15-
Schema,
16-
StringField,
17-
)
1811
from webspark.utils import HTTPException
12+
from webspark.validation import Schema, fields
1913

2014

2115
# Define a schema for user data
2216
class UserSchema(Schema):
23-
name = StringField(required=True, max_length=100)
24-
age = IntegerField(min_value=1, max_value=120)
25-
email = EmailField(required=True)
26-
is_active = BooleanField(default=True)
17+
name = fields.StringField(required=True, max_length=100)
18+
age = fields.IntegerField(min_value=1, max_value=120)
19+
email = fields.EmailField(required=True)
20+
is_active = fields.BooleanField(default=True)
2721

2822

2923
# In-memory storage
@@ -34,8 +28,6 @@ class UserSchema(Schema):
3428
class UserView(View):
3529
"""Handle user operations with schema validation."""
3630

37-
body_schema = UserSchema # Attach the schema for automatic validation
38-
3931
def handle_get(self, ctx: Context):
4032
"""Return all users."""
4133
ctx.json({"users": users})
@@ -44,9 +36,11 @@ def handle_post(self, ctx: Context):
4436
"""Create a new user with validation."""
4537
global next_id
4638

47-
# When body_schema is defined, WebSpark automatically validates the request body
48-
# and makes the validated data available through self.validated_body()
49-
validated_data, errors = self.validated_body(raise_=True)
39+
schema_instance = UserSchema(data=ctx.body)
40+
if not schema_instance.is_valid():
41+
raise HTTPException(schema_instance.errors, status_code=400)
42+
43+
validated_data = schema_instance.validated_data
5044

5145
# Create new user with validated data
5246
new_user = {
@@ -89,6 +83,6 @@ def handle_get(self, ctx: Context):
8983

9084
if __name__ == "__main__":
9185
# For development purposes, you can run this with a WSGI server like:
92-
# gunicorn examples.schema_example:app
86+
# gunicorn examples.validation_example:app
9387
print("Schema Validation Example")
94-
print("Run with: gunicorn examples.schema_example:app")
88+
print("Run with: gunicorn examples.validation_example:app")

examples/schema_plugin_example.py

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,17 @@
99
from webspark.contrib.plugins.schema import SchemaPlugin
1010
from webspark.core import View, WebSpark, path
1111
from webspark.http import Context
12-
from webspark.schema import (
13-
BooleanField,
14-
EmailField,
15-
IntegerField,
16-
Schema,
17-
StringField,
18-
)
1912
from webspark.utils import HTTPException
2013
from webspark.utils.decorators import apply
14+
from webspark.validation import Schema, fields
2115

2216

2317
# Define a schema for user data
2418
class UserSchema(Schema):
25-
name = StringField(required=True, max_length=100)
26-
email = EmailField(required=True)
27-
age = IntegerField(min_value=1, max_value=120)
28-
is_active = BooleanField(default=True)
19+
name = fields.StringField(required=True, max_length=100)
20+
email = fields.EmailField(required=True)
21+
age = fields.IntegerField(min_value=1, max_value=120)
22+
is_active = fields.BooleanField(default=True)
2923

3024

3125
# In-memory storage
@@ -36,23 +30,17 @@ class UserSchema(Schema):
3630
class UserView(View):
3731
"""Handle user operations with schema validation."""
3832

39-
body_schema = UserSchema # Attach the schema for automatic validation
40-
4133
def handle_get(self, ctx: Context):
4234
"""Return all users."""
4335
ctx.json({"users": users})
4436

4537
@apply(
46-
SchemaPlugin(UserSchema, prop="body", kw="body"),
38+
SchemaPlugin(UserSchema, prop="body"),
4739
)
4840
def handle_post(self, ctx: Context, body: dict):
4941
"""Create a new user with validation."""
5042
global next_id
5143

52-
# When body_schema is defined, WebSpark automatically validates the request body
53-
# and makes the validated data available through self.validated_body()
54-
# validated_data, errors = self.validated_body(raise_=True)
55-
5644
# Create new user with validated data
5745
new_user = {
5846
"id": next_id,
@@ -95,5 +83,5 @@ def handle_get(self, ctx: Context):
9583
if __name__ == "__main__":
9684
# For development purposes, you can run this with a WSGI server like:
9785
# gunicorn examples.schema_example:app
98-
print("Schema Validation Example")
86+
print("Schema Plugin Example")
9987
print("Run with: gunicorn examples.schema_plugin_example:app")

tests/contrib/plugins/test_schema.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
from webspark.contrib.plugins.schema import SchemaPlugin
66
from webspark.core.views import View
77
from webspark.http.context import Context
8-
from webspark.schema.fields import IntegerField, StringField
9-
from webspark.schema.schema import Schema
108
from webspark.utils import HTTPException
9+
from webspark.validation import Schema
10+
from webspark.validation.fields import IntegerField, StringField
1111

1212

1313
# Sample schema for testing

tests/schema/test_schema.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66

77
import pytest
88

9-
from webspark.schema.fields import (
9+
from webspark.utils import HTTPException
10+
from webspark.validation import Schema
11+
from webspark.validation.fields import (
1012
UNDEFINED,
1113
BaseField,
1214
BooleanField,
@@ -24,8 +26,6 @@
2426
URLField,
2527
UUIDField,
2628
)
27-
from webspark.schema.schema import Schema
28-
from webspark.utils import HTTPException
2929

3030

3131
def test_base_field_initialization():

tests/utils/test_apply.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
from webspark.contrib.plugins.schema import SchemaPlugin
66
from webspark.core.views import View
7-
from webspark.schema.fields import IntegerField, StringField
8-
from webspark.schema.schema import Schema
97
from webspark.utils import HTTPException
108
from webspark.utils.decorators import apply
9+
from webspark.validation import Schema
10+
from webspark.validation.fields import IntegerField, StringField
1111

1212

1313
# Sample schema for testing

webspark/contrib/plugins/schema.py

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

1010
from ...core.views import View
1111
from ...http.context import Context
12-
from ...schema.schema import Schema
12+
from ...validation.schema import Schema
1313

1414
from ...core.plugin import Plugin
1515
from ...utils.exceptions import HTTPException
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from collections.abc import Callable
1313
from typing import Any
1414

15-
from .schema import Schema
15+
from .validation import Schema
1616

1717
from ..constants import UNDEFINED
1818
from ..utils import HTTPException

0 commit comments

Comments
 (0)