@@ -4,26 +4,33 @@ from collections import Counter
44import time
55
66import falcon
7- from falcon.media.validators.jsonschema import validate
8-
7+ from falcon.media import validators
8+ try :
9+ import jsonschema
10+ except ImportError :
11+ jsonschema = None
12+ try :
13+ import jsonschema_rs
14+ except ImportError :
15+ jsonschema_rs = None
916
1017_MESSAGE_SCHEMA = {
11- ' definitions' : {},
12- ' $schema' : ' http://json-schema.org/draft-07/schema#' ,
13- ' $id' : ' http://example.com/root.json' ,
14- ' type' : ' object' ,
15- ' title' : ' The Root Schema' ,
16- ' required' : [' message' ],
17- ' properties' : {
18- ' message' : {
19- ' $id' : ' #/properties/message' ,
20- ' type' : ' string' ,
21- ' title' : ' The Message Schema' ,
22- ' default' : ' ' ,
23- ' examples' : [' hello world' ],
24- ' pattern' : ' ^(.*)$'
25- }
26- }
18+ ' definitions' : {},
19+ ' $schema' : ' http://json-schema.org/draft-07/schema#' ,
20+ ' $id' : ' http://example.com/root.json' ,
21+ ' type' : ' object' ,
22+ ' title' : ' The Root Schema' ,
23+ ' required' : [' message' ],
24+ ' properties' : {
25+ ' message' : {
26+ ' $id' : ' #/properties/message' ,
27+ ' type' : ' string' ,
28+ ' title' : ' The Message Schema' ,
29+ ' default' : ' ' ,
30+ ' examples' : [' hello world' ],
31+ ' pattern' : ' ^(.*)$'
32+ }
33+ }
2734}
2835
2936
@@ -43,20 +50,37 @@ class NOPClass:
4350 pass
4451
4552
46- class TestResourceWithValidation :
47- @ validate (resp_schema = _MESSAGE_SCHEMA, is_async = True )
48- async def on_get(self , req, resp):
49- resp.media = {
50- ' message' : ' hello world'
51- }
53+ if jsonschema:
54+ class TestResourceWithValidation :
55+ @ validators.jsonschema.validate (resp_schema = _MESSAGE_SCHEMA, is_async = True )
56+ async def on_get(self , req, resp):
57+ resp.media = {
58+ ' message' : ' hello world'
59+ }
5260
5361
54- class TestResourceWithValidationNoHint :
55- @ validate (resp_schema = _MESSAGE_SCHEMA)
56- async def on_get(self , req, resp):
57- resp.media = {
58- ' message' : ' hello world'
59- }
62+ class TestResourceWithValidationNoHint :
63+ @ validators.jsonschema.validate (resp_schema = _MESSAGE_SCHEMA)
64+ async def on_get(self , req, resp):
65+ resp.media = {
66+ ' message' : ' hello world'
67+ }
68+
69+ if jsonschema_rs:
70+ class TestResourceWithValidationRs :
71+ @ validators.jsonschema_rs.validate (resp_schema = _MESSAGE_SCHEMA, is_async = True )
72+ async def on_get(self , req, resp):
73+ resp.media = {
74+ ' message' : ' hello world'
75+ }
76+
77+
78+ class TestResourceWithValidationNoHintRs :
79+ @ validators.jsonschema_rs.validate (resp_schema = _MESSAGE_SCHEMA)
80+ async def on_get(self , req, resp):
81+ resp.media = {
82+ ' message' : ' hello world'
83+ }
6084
6185
6286class TestResourceWithScheduledJobs :
@@ -85,7 +109,7 @@ class TestResourceWithScheduledJobsAsyncRequired:
85109 pass
86110
87111 # NOTE(kgriffs): This will fail later since we can't detect
88- # up front that it isn't a coroutine function.
112+ # up front that it isn't a coroutine function.
89113 resp.schedule(background_job_sync)
90114
91115
0 commit comments