Skip to content

Commit 15fc8fd

Browse files
author
Adrian Dankiv
committed
Add add_schema example
1 parent 041ad50 commit 15fc8fd

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

example1_simple.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class Config:
1616
bulk_size: int = 20
1717

1818

19-
Config.Schema = class_schema(Config)
19+
Config.Schema = class_schema(Config) # <--------------
2020

2121
json_data = {
2222
'file_path': '/validators-example/file',

example1_simple_add_schema.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from dataclasses import dataclass, field
2+
from enum import Enum
3+
4+
from marshmallow_dataclass import add_schema
5+
6+
7+
class Command(Enum):
8+
CREATE = 'create'
9+
DELETE = 'delete'
10+
11+
12+
@add_schema # <--------------
13+
@dataclass
14+
class Config:
15+
file_path: str
16+
command: Command = field(metadata=dict(by_value=True))
17+
bulk_size: int = 20
18+
19+
20+
json_data = {
21+
'file_path': '/validators-example/file',
22+
'command': 'create',
23+
}
24+
25+
config = Config.Schema().load(json_data)
26+
print(config)
27+
print(Config.Schema().dump(config))
28+
29+
assert config.file_path == '/validators-example/file'
30+
assert config.command is Command.CREATE
31+
assert config.bulk_size == 20

0 commit comments

Comments
 (0)