Просмотр исходного кода

work in progress -- validation

Afshin Darian 8 лет назад
Родитель
Сommit
df8dfd83f7
2 измененных файлов с 27 добавлено и 2 удалено
  1. 2 0
      packages/coreutils/package.json
  2. 25 2
      packages/coreutils/src/settingregistry.ts

+ 2 - 0
packages/coreutils/package.json

@@ -17,12 +17,14 @@
     "@phosphor/disposable": "^1.1.1",
     "@phosphor/messaging": "^1.2.1",
     "@phosphor/signaling": "^1.2.1",
+    "ajv": "^5.1.5",
     "minimist": "^1.2.0",
     "moment": "^2.17.1",
     "path-posix": "^1.0.0",
     "url-parse": "^1.1.8"
   },
   "devDependencies": {
+    "@types/ajv": "^1.0.0",
     "@types/minimist": "^1.2.0",
     "rimraf": "^2.5.2",
     "typescript": "~2.3.1"

+ 25 - 2
packages/coreutils/src/settingregistry.ts

@@ -1,6 +1,8 @@
 // Copyright (c) Jupyter Development Team.
 // Distributed under the terms of the Modified BSD License.
 
+import * as Ajv from 'ajv';
+
 import {
   find
 } from '@phosphor/algorithm';
@@ -33,6 +35,13 @@ const LEVEL: ISettingRegistry.Level = 'user';
 const copy = JSONExt.deepCopy;
 
 
+/**
+ * An implementation of a schema validator.
+ */
+export
+interface ISchemaValidator {}
+
+
 /* tslint:disable */
 /**
  * The setting registry token.
@@ -199,6 +208,13 @@ export
 interface ISettingRegistry extends SettingRegistry {}
 
 
+class DefaultSchemaValidator implements ISchemaValidator {
+  constructor() {
+    const validator = new Ajv();
+    console.log('validator', validator);
+  }
+}
+
 /**
  * The default concrete implementation of a setting registry.
  */
@@ -207,9 +223,10 @@ class SettingRegistry {
   /**
    * Create a new setting registry.
    */
-  constructor(options: SettingRegistry.IOptions = { datastore: null }) {
+  constructor(options: SettingRegistry.IOptions = { }) {
     const namespace = 'jupyter.db.settings';
     this._datastore = options.datastore || new StateDB({ namespace });
+    this._validator = options.validator || new DefaultSchemaValidator();
   }
 
   /**
@@ -431,6 +448,7 @@ class SettingRegistry {
   private _datastore: IDatastore<ISettingRegistry.IPlugin, ISettingRegistry.IPlugin> | null = null;
   private _pluginChanged = new Signal<this, string>(this);
   private _plugins: { [name: string]: ISettingRegistry.IPlugin } = Object.create(null);
+  private _validator: ISchemaValidator | null = null;
 }
 
 
@@ -599,7 +617,12 @@ namespace SettingRegistry {
     /**
      * The datastore used by the setting registry.
      */
-    datastore: IDatastore<ISettingRegistry.IPlugin, ISettingRegistry.IPlugin>;
+    datastore?: IDatastore<ISettingRegistry.IPlugin, ISettingRegistry.IPlugin>;
+
+    /**
+     * The validator used to enforce the settings JSON schema.
+     */
+    validator?: ISchemaValidator;
   }
 }