瀏覽代碼

stricter range of validity for schema

Guénaël Muller 6 年之前
父節點
當前提交
518592f7cd
共有 1 個文件被更改,包括 70 次插入19 次删除
  1. 70 19
      tracim/views/core_api/schemas.py

+ 70 - 19
tracim/views/core_api/schemas.py 查看文件

2
 import marshmallow
2
 import marshmallow
3
 from marshmallow import post_load
3
 from marshmallow import post_load
4
 from marshmallow.validate import OneOf
4
 from marshmallow.validate import OneOf
5
+from marshmallow.validate import Range
5
 
6
 
6
 from tracim.lib.utils.utils import DATETIME_FORMAT
7
 from tracim.lib.utils.utils import DATETIME_FORMAT
7
 from tracim.models.auth import Profile
8
 from tracim.models.auth import Profile
79
 
80
 
80
 
81
 
81
 class UserIdPathSchema(marshmallow.Schema):
82
 class UserIdPathSchema(marshmallow.Schema):
82
-    user_id = marshmallow.fields.Int(example=3, required=True)
83
+    user_id = marshmallow.fields.Int(
84
+        example=3,
85
+        required=True,
86
+        description='id of a valid user',
87
+        validate=Range(min=1, error="Value must be greater than 0"),
88
+    )
83
 
89
 
84
 
90
 
85
 class WorkspaceIdPathSchema(marshmallow.Schema):
91
 class WorkspaceIdPathSchema(marshmallow.Schema):
86
-    workspace_id = marshmallow.fields.Int(example=4, required=True)
92
+    workspace_id = marshmallow.fields.Int(
93
+        example=4,
94
+        required=True,
95
+        description='id of a valid workspace',
96
+        validate=Range(min=1, error="Value must be greater than 0"),
97
+    )
87
 
98
 
88
 
99
 
89
 class ContentIdPathSchema(marshmallow.Schema):
100
 class ContentIdPathSchema(marshmallow.Schema):
90
-    content_id = marshmallow.fields.Int(example=6, required=True)
101
+    content_id = marshmallow.fields.Int(
102
+        example=6,
103
+        required=True,
104
+        description='id of a valid content',
105
+        validate=Range(min=1, error="Value must be greater than 0"),
106
+    )
91
 
107
 
92
 
108
 
93
 class WorkspaceAndContentIdPathSchema(
109
 class WorkspaceAndContentIdPathSchema(
102
 class CommentsPathSchema(WorkspaceAndContentIdPathSchema):
118
 class CommentsPathSchema(WorkspaceAndContentIdPathSchema):
103
     comment_id = marshmallow.fields.Int(
119
     comment_id = marshmallow.fields.Int(
104
         example=6,
120
         example=6,
105
-        description='id of a comment related to content content_id',
106
-        required=True
121
+        description='id of a valid comment related to content content_id',
122
+        required=True,
123
+        validate=Range(min=1, error="Value must be greater than 0"),
107
     )
124
     )
108
     @post_load
125
     @post_load
109
     def make_path_object(self, data):
126
     def make_path_object(self, data):
118
                     ' If not set, then return all contents.'
135
                     ' If not set, then return all contents.'
119
                     ' If set to 0, then return root contents.'
136
                     ' If set to 0, then return root contents.'
120
                     ' If set to another value, return all contents'
137
                     ' If set to another value, return all contents'
121
-                    ' directly included in the folder parent_id'
138
+                    ' directly included in the folder parent_id',
139
+        validate=Range(min=0, error="Value must be positive or 0"),
122
     )
140
     )
123
     show_archived = marshmallow.fields.Int(
141
     show_archived = marshmallow.fields.Int(
124
         example=0,
142
         example=0,
125
         default=0,
143
         default=0,
126
         description='if set to 1, then show archived contents.'
144
         description='if set to 1, then show archived contents.'
127
-                    ' Default is 0 - hide archived content'
145
+                    ' Default is 0 - hide archived content',
146
+        validate=Range(min=0, max=1, error="Value must be 0 or 1"),
128
     )
147
     )
129
     show_deleted = marshmallow.fields.Int(
148
     show_deleted = marshmallow.fields.Int(
130
         example=0,
149
         example=0,
131
         default=0,
150
         default=0,
132
         description='if set to 1, then show deleted contents.'
151
         description='if set to 1, then show deleted contents.'
133
-                    ' Default is 0 - hide deleted content'
152
+                    ' Default is 0 - hide deleted content',
153
+        validate=Range(min=0, max=1, error="Value must be 0 or 1"),
134
     )
154
     )
135
     show_active = marshmallow.fields.Int(
155
     show_active = marshmallow.fields.Int(
136
         example=1,
156
         example=1,
140
                     ' Note: active content are content '
160
                     ' Note: active content are content '
141
                     'that is neither archived nor deleted. '
161
                     'that is neither archived nor deleted. '
142
                     'The reason for this parameter to exist is for example '
162
                     'The reason for this parameter to exist is for example '
143
-                    'to allow to show only archived documents'
163
+                    'to allow to show only archived documents',
164
+        validate=Range(min=0, max=1, error="Value must be 0 or 1"),
144
     )
165
     )
145
 
166
 
146
     @post_load
167
     @post_load
204
 
225
 
205
 
226
 
206
 class WorkspaceDigestSchema(marshmallow.Schema):
227
 class WorkspaceDigestSchema(marshmallow.Schema):
207
-    workspace_id = marshmallow.fields.Int(example=4)
228
+    workspace_id = marshmallow.fields.Int(
229
+        example=4,
230
+        validate=Range(min=1, error="Value must be greater than 0"),
231
+    )
208
     slug = marshmallow.fields.String(example='intranet')
232
     slug = marshmallow.fields.String(example='intranet')
209
     label = marshmallow.fields.String(example='Intranet')
233
     label = marshmallow.fields.String(example='Intranet')
210
     sidebar_entries = marshmallow.fields.Nested(
234
     sidebar_entries = marshmallow.fields.Nested(
228
         example='contributor',
252
         example='contributor',
229
         validate=OneOf(UserRoleInWorkspace.get_all_role_slug())
253
         validate=OneOf(UserRoleInWorkspace.get_all_role_slug())
230
     )
254
     )
231
-    user_id = marshmallow.fields.Int(example=3)
232
-    workspace_id = marshmallow.fields.Int(example=4)
255
+    user_id = marshmallow.fields.Int(
256
+        example=3,
257
+        validate=Range(min=1, error="Value must be greater than 0"),
258
+    )
259
+    workspace_id = marshmallow.fields.Int(
260
+        example=4,
261
+        validate=Range(min=1, error="Value must be greater than 0"),
262
+    )
233
     user = marshmallow.fields.Nested(
263
     user = marshmallow.fields.Nested(
234
         UserSchema(only=('public_name', 'avatar_url'))
264
         UserSchema(only=('public_name', 'avatar_url'))
235
     )
265
     )
318
         description='id of the new parent content id.',
348
         description='id of the new parent content id.',
319
         allow_none=True,
349
         allow_none=True,
320
         required=True,
350
         required=True,
351
+        validate=Range(min=0, error="Value must be positive or 0"),
321
     )
352
     )
322
     new_workspace_id = marshmallow.fields.Int(
353
     new_workspace_id = marshmallow.fields.Int(
323
         example=2,
354
         example=2,
324
         description='id of the new workspace id.',
355
         description='id of the new workspace id.',
325
-        required=True
356
+        required=True,
357
+        validate=Range(min=1, error="Value must be greater than 0"),
326
     )
358
     )
327
 
359
 
328
     @post_load
360
     @post_load
346
 
378
 
347
 
379
 
348
 class ContentDigestSchema(marshmallow.Schema):
380
 class ContentDigestSchema(marshmallow.Schema):
349
-    content_id = marshmallow.fields.Int(example=6)
381
+    content_id = marshmallow.fields.Int(
382
+        example=6,
383
+        validate=Range(min=1, error="Value must be greater than 0"),
384
+    )
350
     slug = marshmallow.fields.Str(example='intervention-report-12')
385
     slug = marshmallow.fields.Str(example='intervention-report-12')
351
     parent_id = marshmallow.fields.Int(
386
     parent_id = marshmallow.fields.Int(
352
         example=34,
387
         example=34,
353
         allow_none=True,
388
         allow_none=True,
354
-        default=None
389
+        default=None,
390
+        validate=Range(min=0, error="Value must be positive or 0"),
355
     )
391
     )
356
     workspace_id = marshmallow.fields.Int(
392
     workspace_id = marshmallow.fields.Int(
357
         example=19,
393
         example=19,
394
+        validate=Range(min=1, error="Value must be greater than 0"),
358
     )
395
     )
359
     label = marshmallow.fields.Str(example='Intervention Report 12')
396
     label = marshmallow.fields.Str(example='Intervention Report 12')
360
     content_type = marshmallow.fields.Str(
397
     content_type = marshmallow.fields.Str(
421
 
458
 
422
 
459
 
423
 class RevisionSchema(ContentDigestSchema):
460
 class RevisionSchema(ContentDigestSchema):
424
-    comment_ids = marshmallow.fields.List(marshmallow.fields.Int(example=4))
425
-    revision_id = marshmallow.fields.Int(example=12)
461
+    comment_ids = marshmallow.fields.List(
462
+        marshmallow.fields.Int(
463
+            example=4,
464
+            validate=Range(min=1, error="Value must be greater than 0"),
465
+        )
466
+    )
467
+    revision_id = marshmallow.fields.Int(
468
+        example=12,
469
+        validate=Range(min=1, error="Value must be greater than 0"),
470
+    )
426
     created = marshmallow.fields.DateTime(
471
     created = marshmallow.fields.DateTime(
427
         format=DATETIME_FORMAT,
472
         format=DATETIME_FORMAT,
428
         description='Content creation date',
473
         description='Content creation date',
435
 
480
 
436
 
481
 
437
 class CommentSchema(marshmallow.Schema):
482
 class CommentSchema(marshmallow.Schema):
438
-    content_id = marshmallow.fields.Int(example=6)
439
-    parent_id = marshmallow.fields.Int(example=34)
483
+    content_id = marshmallow.fields.Int(
484
+        example=6,
485
+        validate=Range(min=1, error="Value must be greater than 0"),
486
+    )
487
+    parent_id = marshmallow.fields.Int(
488
+        example=34,
489
+        validate=Range(min=0, error="Value must be positive or 0"),
490
+    )
440
     raw_content = marshmallow.fields.String(
491
     raw_content = marshmallow.fields.String(
441
         example='<p>This is just an html comment !</p>'
492
         example='<p>This is just an html comment !</p>'
442
     )
493
     )