-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathSolutionsUtils.js
More file actions
107 lines (103 loc) · 3.62 KB
/
SolutionsUtils.js
File metadata and controls
107 lines (103 loc) · 3.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
* GPII Solutions Utils - Independent helper functions and utilities for working with
* solutions and their schemas.
*
* Copyright 2019 Raising the Floor - US
*
* Licensed under the New BSD license. You may not use this file except in
* compliance with this License.
*
* The research leading to these results has received funding from the European Union's
* Seventh Framework Programme (FP7/2007-2013)
* under grant agreement no. 289016.
*
* You may obtain a copy of the License at
* https://github.com/GPII/universal/blob/master/LICENSE.txt
*/
"use strict";
var fluid = require("infusion");
var gpii = fluid.registerNamespace("gpii");
fluid.registerNamespace("gpii.universal.solutionsRegistry");
/**
* This function will take a schema from a supported setting in the solutions
* registry and return metadata on it's default value, including the type of
* the default value. The function will always return a block of json in order
* to avoid any ambiguities for a situation where the default value is perhaps
* `null`. A key `hasDefault` will be `true` or `false` to communicate this.
* If the schema does have a default value, then the properties `type` and
* `default` can also be included. Note that there may not always be a `type`
* that can be included, the inclusion of this in the return if optional and
* will be included if a `type` is declared.
*
* This is NOT meant to be an exhaustive coverage of all the possible ways
* default values can be set for any JSON Schema. We merely want to cover the
* cases in use in the solutions registry, focussing on single primitive values,
* and situations where we next one level deep due to a requirement of having a
* `properties` entry in the schema.
*
* @param {Object} schema - Schema to search for a default value. This is what
* would be in the solutions registry under `supportedSettings` and a
* configuration. An example from the Windows Mouse Keys MaxSpeed setting is:
* ```json
* {
* "title": "Mouse keys speed",
* "description": "Speed of mouse keys",
* "type": "number",
* "multipleOf": 10,
* "default": 80
* }
* ```
* @return {Object} Always returns a json block. For the example above the
* return value would be:
* ```json
* {
* "hasDefault": true,
* "type": "number",
* "default": 80
* }
* ```
* If a default value cannot be detected the return payload will be:
* ```json
* {
* "hasDefault": false
* }
* ```
*/
gpii.universal.solutionsRegistry.findDefaultValue = function (schema) {
var togo;
// 1. The most simple, and hopefull case is that there is a top level
// default value.
if (schema["default"] !== undefined) {
togo = {
hasDefault: true,
default: schema["default"]
};
if (schema.type) {
togo.type = schema.type;
}
}
// 2. Sometimes we have a properties structure that can be interogated, example:
// "properties": {
// "path": { "type": "string"},
// "value": {
// "type": "string",
// "default": "%SystemRoot%\\Web\\Wallpaper\\Windows\\img0.jpg"
// }
// }
else if (schema.properties && schema.properties.value && schema.properties.value["default"] !== undefined) {
togo = {
hasDefault: true,
default: schema.properties.value["default"]
};
if (schema.properties.value.type) {
togo.type = schema.properties.value.type;
}
}
// We cannot find a default value for our simplified usage of json schema...
else {
togo = {
hasDefault: false
};
}
return togo;
};