Sfoglia il codice sorgente

Change verb to patch and add schema validation

Steven Silvester 7 anni fa
parent
commit
e302c28407
1 ha cambiato i file con 17 aggiunte e 1 eliminazioni
  1. 17 1
      jupyterlab/settings_handler.py

+ 17 - 1
jupyterlab/settings_handler.py

@@ -8,6 +8,12 @@ from tornado import web
 
 from notebook.base.handlers import APIHandler, json_errors
 
+try:
+    from jsonschema import ValidationError
+    from jsonschema import Draft4Validator as Validator
+except ImportError:
+    Validator = None
+
 
 class SettingsHandler(APIHandler):
 
@@ -36,7 +42,7 @@ class SettingsHandler(APIHandler):
 
     @json_errors
     @web.authenticated
-    def put(self, section_name):
+    def patch(self, section_name):
         if not self.settings_dir:
             raise web.HTTPError(404, "No current settings directory")
 
@@ -47,6 +53,16 @@ class SettingsHandler(APIHandler):
 
         data = self.get_json_body()  # Will raise 400 if content is not valid JSON
 
+        # Validate the data against the schema.
+        if Validator is not None:
+            with open(path) as fid:
+                schema = json.load(fid)
+            validator = Validator(schema)
+            try:
+                validator.validate(data)
+            except ValidationError as e:
+                raise web.HTTPError(400, str(e))
+
         path = os.path.join(self.settings_dir, section_name + '.json')
         with open(path, 'w') as fid:
             json.dump(fid, data)