Browse Source

stricter range of validity for schema

Guénaël Muller 6 years ago
parent
commit
518592f7cd
1 changed files with 70 additions and 19 deletions
  1. 70 19
      tracim/views/core_api/schemas.py

+ 70 - 19
tracim/views/core_api/schemas.py View File

@@ -2,6 +2,7 @@
2 2
 import marshmallow
3 3
 from marshmallow import post_load
4 4
 from marshmallow.validate import OneOf
5
+from marshmallow.validate import Range
5 6
 
6 7
 from tracim.lib.utils.utils import DATETIME_FORMAT
7 8
 from tracim.models.auth import Profile
@@ -79,15 +80,30 @@ class UserSchema(UserDigestSchema):
79 80
 
80 81
 
81 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 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 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 109
 class WorkspaceAndContentIdPathSchema(
@@ -102,8 +118,9 @@ class WorkspaceAndContentIdPathSchema(
102 118
 class CommentsPathSchema(WorkspaceAndContentIdPathSchema):
103 119
     comment_id = marshmallow.fields.Int(
104 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 125
     @post_load
109 126
     def make_path_object(self, data):
@@ -118,19 +135,22 @@ class FilterContentQuerySchema(marshmallow.Schema):
118 135
                     ' If not set, then return all contents.'
119 136
                     ' If set to 0, then return root contents.'
120 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 141
     show_archived = marshmallow.fields.Int(
124 142
         example=0,
125 143
         default=0,
126 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 148
     show_deleted = marshmallow.fields.Int(
130 149
         example=0,
131 150
         default=0,
132 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 155
     show_active = marshmallow.fields.Int(
136 156
         example=1,
@@ -140,7 +160,8 @@ class FilterContentQuerySchema(marshmallow.Schema):
140 160
                     ' Note: active content are content '
141 161
                     'that is neither archived nor deleted. '
142 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 167
     @post_load
@@ -204,7 +225,10 @@ class WorkspaceMenuEntrySchema(marshmallow.Schema):
204 225
 
205 226
 
206 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 232
     slug = marshmallow.fields.String(example='intranet')
209 233
     label = marshmallow.fields.String(example='Intranet')
210 234
     sidebar_entries = marshmallow.fields.Nested(
@@ -228,8 +252,14 @@ class WorkspaceMemberSchema(marshmallow.Schema):
228 252
         example='contributor',
229 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 263
     user = marshmallow.fields.Nested(
234 264
         UserSchema(only=('public_name', 'avatar_url'))
235 265
     )
@@ -318,11 +348,13 @@ class ContentMoveSchema(marshmallow.Schema):
318 348
         description='id of the new parent content id.',
319 349
         allow_none=True,
320 350
         required=True,
351
+        validate=Range(min=0, error="Value must be positive or 0"),
321 352
     )
322 353
     new_workspace_id = marshmallow.fields.Int(
323 354
         example=2,
324 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 360
     @post_load
@@ -346,15 +378,20 @@ class ContentCreationSchema(marshmallow.Schema):
346 378
 
347 379
 
348 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 385
     slug = marshmallow.fields.Str(example='intervention-report-12')
351 386
     parent_id = marshmallow.fields.Int(
352 387
         example=34,
353 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 392
     workspace_id = marshmallow.fields.Int(
357 393
         example=19,
394
+        validate=Range(min=1, error="Value must be greater than 0"),
358 395
     )
359 396
     label = marshmallow.fields.Str(example='Intervention Report 12')
360 397
     content_type = marshmallow.fields.Str(
@@ -421,8 +458,16 @@ class TextBasedContentSchema(ContentSchema, TextBasedDataAbstractSchema):
421 458
 
422 459
 
423 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 471
     created = marshmallow.fields.DateTime(
427 472
         format=DATETIME_FORMAT,
428 473
         description='Content creation date',
@@ -435,8 +480,14 @@ class TextBasedRevisionSchema(RevisionSchema, TextBasedDataAbstractSchema):
435 480
 
436 481
 
437 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 491
     raw_content = marshmallow.fields.String(
441 492
         example='<p>This is just an html comment !</p>'
442 493
     )