You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@brooklyn.apache.org by he...@apache.org on 2022/08/22 20:37:17 UTC

[brooklyn-ui] 03/05: better validation and handling of complex types

This is an automated email from the ASF dual-hosted git repository.

heneveld pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/brooklyn-ui.git

commit f87fba3df704666c13e664500652da1e45275ab7
Author: Alex Heneveld <al...@cloudsoft.io>
AuthorDate: Mon Aug 22 20:14:17 2022 +0100

    better validation and handling of complex types
---
 .../spec-editor/spec-editor.directive.js           | 66 +++++++++++++---------
 .../addon/schemas/blueprint-entity.json            |  4 +-
 .../utils/yaml-editor/addon/schemas/blueprint.json | 44 +++++++++++++++
 3 files changed, 86 insertions(+), 28 deletions(-)

diff --git a/ui-modules/blueprint-composer/app/components/spec-editor/spec-editor.directive.js b/ui-modules/blueprint-composer/app/components/spec-editor/spec-editor.directive.js
index adc1dbfb..1b74dafd 100644
--- a/ui-modules/blueprint-composer/app/components/spec-editor/spec-editor.directive.js
+++ b/ui-modules/blueprint-composer/app/components/spec-editor/spec-editor.directive.js
@@ -591,6 +591,7 @@ export function specEditorDirective($rootScope, $templateCache, $injector, $sani
                     if (!Array.isArray(val)) return type + '-manual';  // causes default string editor
                 }
             }
+
             if (scope.state.config.codeModeActive[item.name]) {
                 // code mode forces manual editor
                 return type + '-manual';
@@ -991,27 +992,33 @@ export function specEditorDirective($rootScope, $templateCache, $injector, $sani
 
             try {
                 if (definition.widgetMode === 'array') {
-                    return value.map(item => {
-                        if (item instanceof Dsl) {
-                            return item.toString();
-                        } else if (item instanceof Array || item instanceof Object) {
-                            throw 'not simple json in array';
-                        } else {
-                            return item;
-                        }
-                    });
+                    if (Array.isArray(value)) {
+                        return value.map(item => {
+                            if (item instanceof Dsl) {
+                                return item.toString();
+                            } else if (item instanceof Array || item instanceof Object) {
+                                throw 'not simple json in array';
+                            } else {
+                                return item;
+                            }
+                        });
+                    }
+                    // fall through to return toString below
                 } else if (definition.widgetMode === 'map') {
-                    let object = {};
-                    for (let keyObject in value) {
-                        if (value[keyObject] instanceof Dsl) {
-                            object[keyObject] = value[keyObject].toString();
-                        } else if (value[keyObject] instanceof Array || value[keyObject] instanceof Object) {
-                            throw 'not simple json in map';
-                        } else {
-                            object[keyObject] = value[keyObject];
+                    if (typeof value === "object") {
+                        let object = {};
+                        for (let keyObject in value) {
+                            if (value[keyObject] instanceof Dsl) {
+                                object[keyObject] = value[keyObject].toString();
+                            } else if (value[keyObject] instanceof Array || value[keyObject] instanceof Object) {
+                                throw 'not simple json in map';
+                            } else {
+                                object[keyObject] = value[keyObject];
+                            }
                         }
+                        return object;
                     }
-                    return object;
+                    // fall through to return toString below
                 } else if (!(value instanceof Dsl) && (value instanceof Object || value instanceof Array)) {
                     throw 'must use code editor';
                 }
@@ -1065,6 +1072,7 @@ export function specEditorDirective($rootScope, $templateCache, $injector, $sani
                 if (angular.isUndefined(localConfig[keyRef]) || localConfig[keyRef] === null || localConfig[keyRef].length < 1) {
                     continue;
                 }
+
                 if (localConfig[keyRef].hasOwnProperty(REPLACED_DSL_ENTITYSPEC)) {
                     result[keyRef] = {};
                     result[keyRef][DSL_ENTITY_SPEC] = localConfig[keyRef][REPLACED_DSL_ENTITYSPEC];
@@ -1077,11 +1085,13 @@ export function specEditorDirective($rootScope, $templateCache, $injector, $sani
                     scope.getConfigWidgetMode(definition, localConfig[keyRef])
                 }
 
+                let v = localConfig[keyRef];
+
                 // if JSON mode then parse
                 scope.state.config.codeModeError[keyRef] = null;
                 if (scope.state.config.codeModeActive && scope.state.config.codeModeActive[keyRef]) {
                     try {
-                        result[keyRef] = JSON.parse(localConfig[keyRef]);
+                        result[keyRef] = JSON.parse(v);
                     } catch (ex) {
                         scope.state.config.codeModeError[keyRef] = "Invalid JSON";
                         result[keyRef] = localConfig[keyRef];
@@ -1092,18 +1102,22 @@ export function specEditorDirective($rootScope, $templateCache, $injector, $sani
                 // else return as is, or introspect for array/map
 
                 if (definition.widgetMode === 'array') {
-                    result[keyRef] = localConfig[keyRef].map(getModelValueFromString);
-                    continue;
+                    if (Array.isArray(v)) {
+                        result[keyRef] = v.map(getModelValueFromString);
+                        continue;
+                    }
                 }
                 if (definition.widgetMode === 'map') {
-                    result[keyRef] = {};
-                    for (let keyObject in localConfig[keyRef]) {
-                        result[keyRef][keyObject] = getModelValueFromString(localConfig[keyRef][keyObject]);
+                    if (typeof v === "object") {
+                        result[keyRef] = {};
+                        for (let keyObject in v) {
+                            result[keyRef][keyObject] = getModelValueFromString(v[keyObject]);
+                        }
+                        continue;
                     }
-                    continue;
                 }
 
-                result[keyRef] = getModelValueFromString(localConfig[keyRef]);
+                result[keyRef] = getModelValueFromString(v);
             }
 
             scope.model.setConfigFromJson(result);
diff --git a/ui-modules/utils/yaml-editor/addon/schemas/blueprint-entity.json b/ui-modules/utils/yaml-editor/addon/schemas/blueprint-entity.json
index 811d1d8d..c0df9a28 100644
--- a/ui-modules/utils/yaml-editor/addon/schemas/blueprint-entity.json
+++ b/ui-modules/utils/yaml-editor/addon/schemas/blueprint-entity.json
@@ -120,9 +120,9 @@
         },
         "type": {
           "title": "Parameter type",
-          "description": "The type of the parameter, one of string, boolean, integer, long, float, double, timestamp, duration, port, or a fully qualified Java type name; the default is string",
+          "description": "The type of the parameter, one of string, boolean, integer, long, float, double, timestamp, duration, port, or a registered type name; the default is string",
           "type": "string",
-          "pattern": "^(string|boolean|integer|long|float|double|timestamp|duration|port|[a-z]+(\\.[a-zA-Z]+)+)$"
+          "pattern": "^(string|[^ ]+)$"
         },
         "default": {
           "title": "Parameter default value",
diff --git a/ui-modules/utils/yaml-editor/addon/schemas/blueprint.json b/ui-modules/utils/yaml-editor/addon/schemas/blueprint.json
index fcc8ebc3..b5688436 100644
--- a/ui-modules/utils/yaml-editor/addon/schemas/blueprint.json
+++ b/ui-modules/utils/yaml-editor/addon/schemas/blueprint.json
@@ -39,6 +39,50 @@
     },
     "location": {
       "$ref": "/Blueprint/Location"
+    },
+    "brooklyn.config": {
+      "title": "Entity configuration",
+      "description": "Configuration key-value pairs passed to the service entity being created",
+      "type": "object"
+    },
+    "brooklyn.policies": {
+      "title": "Policies",
+      "description": "A list of policies, each as a map described with their type and their brooklyn.config as keys",
+      "type": "array",
+      "items": {
+        "title": "Policy",
+        "$ref": "/Blueprint/Entity"
+      },
+      "minItems": 1
+    },
+    "brooklyn.enrichers": {
+      "title": "Enrichers",
+      "description": "A list of enrichers, each as a map described with their type and their brooklyn.config as keys",
+      "type": "array",
+      "items": {
+        "title": "Enricher",
+        "$ref": "/Blueprint/Entity"
+      },
+      "minItems": 1
+    },
+    "brooklyn.initializers": {
+      "title": "Initializers",
+      "description": "A list of initializers instances to be constructed and run against the entity, each as a map described with their type and their brooklyn.config as keys",
+      "type": "array",
+      "items": {
+        "title": "Initializer",
+        "$ref": "/Blueprint/Entity"
+      },
+      "minItems": 1
+    },
+    "brooklyn.parameters": {
+      "title": "Parameters",
+      "description": "A list of typed parameters the entity accepts",
+      "type": "array",
+      "items": {
+        "$ref": "/Blueprint/Entity#/definitions/brooklynParameter"
+      },
+      "minItems": 1
     }
   }
 }
\ No newline at end of file