@fgv/ts-json 3.0.1-alpha.5 → 4.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.json +15 -0
- package/CHANGELOG.md +9 -1
- package/dist/ts-json.d.ts +154 -155
- package/dist/tsdoc-metadata.json +1 -1
- package/lib/packlets/context/compositeJsonMap.d.ts +2 -2
- package/lib/packlets/context/compositeJsonMap.js +2 -2
- package/lib/packlets/context/compositeJsonMap.js.map +1 -1
- package/lib/packlets/context/contextHelpers.d.ts +2 -2
- package/lib/packlets/context/contextHelpers.js +2 -2
- package/lib/packlets/context/contextHelpers.js.map +1 -1
- package/lib/packlets/context/jsonContext.d.ts +5 -5
- package/lib/packlets/context/jsonContext.js.map +1 -1
- package/lib/packlets/converters/converters.d.ts +2 -2
- package/lib/packlets/converters/converters.js +2 -2
- package/lib/packlets/converters/converters.js.map +1 -1
- package/lib/packlets/converters/jsonConverter.d.ts +6 -6
- package/lib/packlets/converters/jsonConverter.js +6 -6
- package/lib/packlets/converters/jsonConverter.js.map +1 -1
- package/lib/packlets/editor/common.d.ts +6 -6
- package/lib/packlets/editor/common.js.map +1 -1
- package/lib/packlets/editor/index.d.ts +2 -2
- package/lib/packlets/editor/index.d.ts.map +1 -1
- package/lib/packlets/editor/index.js +3 -1
- package/lib/packlets/editor/index.js.map +1 -1
- package/lib/packlets/editor/jsonEditor.d.ts +24 -24
- package/lib/packlets/editor/jsonEditor.js +21 -21
- package/lib/packlets/editor/jsonEditor.js.map +1 -1
- package/lib/packlets/editor/jsonEditorRule.d.ts +17 -17
- package/lib/packlets/editor/jsonEditorRule.js +4 -4
- package/lib/packlets/editor/jsonEditorRule.js.map +1 -1
- package/lib/packlets/editor/jsonEditorState.d.ts +20 -20
- package/lib/packlets/editor/jsonEditorState.js +17 -17
- package/lib/packlets/editor/jsonEditorState.js.map +1 -1
- package/lib/packlets/editor/jsonReferenceMap.d.ts +12 -13
- package/lib/packlets/editor/jsonReferenceMap.d.ts.map +1 -1
- package/lib/packlets/editor/jsonReferenceMap.js +7 -8
- package/lib/packlets/editor/jsonReferenceMap.js.map +1 -1
- package/lib/packlets/editor/rules/conditional.d.ts +17 -17
- package/lib/packlets/editor/rules/conditional.js +12 -12
- package/lib/packlets/editor/rules/conditional.js.map +1 -1
- package/lib/packlets/editor/rules/multivalue.d.ts +13 -13
- package/lib/packlets/editor/rules/multivalue.js +11 -11
- package/lib/packlets/editor/rules/multivalue.js.map +1 -1
- package/lib/packlets/editor/rules/references.d.ts +12 -12
- package/lib/packlets/editor/rules/references.js +11 -11
- package/lib/packlets/editor/rules/references.js.map +1 -1
- package/lib/packlets/editor/rules/templates.d.ts +14 -14
- package/lib/packlets/editor/rules/templates.js +12 -12
- package/lib/packlets/editor/rules/templates.js.map +1 -1
- package/package.json +15 -15
|
@@ -4,7 +4,7 @@ import { IJsonEditorOptions, JsonEditFailureReason, JsonPropertyEditFailureReaso
|
|
|
4
4
|
import { JsonEditorRuleBase } from '../jsonEditorRule';
|
|
5
5
|
import { JsonEditorState } from '../jsonEditorState';
|
|
6
6
|
/**
|
|
7
|
-
* Returned by {@link
|
|
7
|
+
* Returned by {@link EditorRules.ConditionalJsonEditorRule._tryParseCondition | ConditionalJsonEditorRule._tryParseCondition}
|
|
8
8
|
* to indicate whether a successful match was due to a matching condition or a default value.
|
|
9
9
|
* @public
|
|
10
10
|
*/
|
|
@@ -12,8 +12,8 @@ export interface IConditionalJsonKeyResult extends JsonObject {
|
|
|
12
12
|
matchType: 'default' | 'match' | 'unconditional';
|
|
13
13
|
}
|
|
14
14
|
/**
|
|
15
|
-
* On a successful match, the {@link
|
|
16
|
-
* stores a {@link
|
|
15
|
+
* On a successful match, the {@link EditorRules.ConditionalJsonEditorRule | ConditionalJsonEditorRule}
|
|
16
|
+
* stores a {@link EditorRules.IConditionalJsonDeferredObject | IConditionalJsonDeferredObject} describing the
|
|
17
17
|
* matching result, to be resolved at finalization time.
|
|
18
18
|
* @public
|
|
19
19
|
*/
|
|
@@ -21,7 +21,7 @@ export interface IConditionalJsonDeferredObject extends IConditionalJsonKeyResul
|
|
|
21
21
|
value: JsonValue;
|
|
22
22
|
}
|
|
23
23
|
/**
|
|
24
|
-
* Configuration options for the {@link
|
|
24
|
+
* Configuration options for the {@link EditorRules.ConditionalJsonEditorRule | ConditionalJsonEditorRule}.
|
|
25
25
|
* @public
|
|
26
26
|
*/
|
|
27
27
|
export interface IConditionalJsonRuleOptions extends Partial<IJsonEditorOptions> {
|
|
@@ -32,7 +32,7 @@ export interface IConditionalJsonRuleOptions extends Partial<IJsonEditorOptions>
|
|
|
32
32
|
flattenUnconditionalValues?: boolean;
|
|
33
33
|
}
|
|
34
34
|
/**
|
|
35
|
-
* The {@link
|
|
35
|
+
* The {@link EditorRules.ConditionalJsonEditorRule | ConditionalJsonEditorRule} evaluates
|
|
36
36
|
* properties with conditional keys, omitting non-matching keys and merging keys that match,
|
|
37
37
|
* or default keys only if no other keys match.
|
|
38
38
|
*
|
|
@@ -44,30 +44,30 @@ export interface IConditionalJsonRuleOptions extends Partial<IJsonEditorOptions>
|
|
|
44
44
|
*/
|
|
45
45
|
export declare class ConditionalJsonEditorRule extends JsonEditorRuleBase {
|
|
46
46
|
/**
|
|
47
|
-
* Stored fully-resolved {@link
|
|
47
|
+
* Stored fully-resolved {@link EditorRules.IConditionalJsonRuleOptions | options} for this
|
|
48
48
|
* rule.
|
|
49
49
|
* @public
|
|
50
50
|
*/
|
|
51
51
|
protected _options?: IConditionalJsonRuleOptions;
|
|
52
52
|
/**
|
|
53
|
-
* Creates a new {@link
|
|
54
|
-
* @param options - Optional {@link
|
|
53
|
+
* Creates a new {@link EditorRules.ConditionalJsonEditorRule | ConditionalJsonEditorRule}.
|
|
54
|
+
* @param options - Optional {@link EditorRules.IConditionalJsonRuleOptions | configuration options}
|
|
55
55
|
* used for this rule.
|
|
56
56
|
*/
|
|
57
57
|
constructor(options?: IConditionalJsonRuleOptions);
|
|
58
58
|
/**
|
|
59
|
-
* Creates a new {@link
|
|
60
|
-
* @param options - Optional {@link
|
|
59
|
+
* Creates a new {@link EditorRules.ConditionalJsonEditorRule | ConditionalJsonEditorRule}.
|
|
60
|
+
* @param options - Optional {@link EditorRules.IConditionalJsonRuleOptions | configuration options}
|
|
61
61
|
* used for this rule.
|
|
62
62
|
*/
|
|
63
63
|
static create(options?: IConditionalJsonRuleOptions): Result<ConditionalJsonEditorRule>;
|
|
64
64
|
/**
|
|
65
65
|
* Evaluates a property for conditional application.
|
|
66
66
|
* @param key - The key of the property to be considered
|
|
67
|
-
* @param value - The
|
|
68
|
-
* @param state - The {@link
|
|
67
|
+
* @param value - The `JsonValue` of the property to be considered.
|
|
68
|
+
* @param state - The {@link JsonEditorState | editor state} for the object being edited.
|
|
69
69
|
* @returns Returns `Success` with detail `'deferred'` and a
|
|
70
|
-
* {@link
|
|
70
|
+
* {@link EditorRules.IConditionalJsonDeferredObject | IConditionalJsonDeferredObject}.
|
|
71
71
|
* for a matching, default or unconditional key. Returns `Failure` with detail `'ignore'` for
|
|
72
72
|
* a non-matching conditional, or with detail `'error'` if an error occurs. Otherwise
|
|
73
73
|
* fails with detail `'inapplicable'`.
|
|
@@ -77,17 +77,17 @@ export declare class ConditionalJsonEditorRule extends JsonEditorRuleBase {
|
|
|
77
77
|
* Finalizes any deferred conditional properties. If the only deferred property is
|
|
78
78
|
* default, that property is emitted. Otherwise all matching properties are emitted.
|
|
79
79
|
* @param finalized - The deferred properties to be considered for merge.
|
|
80
|
-
* @param __state - The {@link
|
|
80
|
+
* @param __state - The {@link JsonEditorState | editor state} for the object
|
|
81
81
|
* being edited.
|
|
82
82
|
*/
|
|
83
83
|
finalizeProperties(finalized: JsonObject[], __state: JsonEditorState): DetailedResult<JsonObject[], JsonEditFailureReason>;
|
|
84
84
|
/**
|
|
85
85
|
* Determines if a given property key is conditional. Derived classes can override this
|
|
86
86
|
* method to use a different format for conditional properties.
|
|
87
|
-
* @param value - The
|
|
88
|
-
* @param state - The {@link
|
|
87
|
+
* @param value - The `JsonValue` of the property to be considered.
|
|
88
|
+
* @param state - The {@link JsonEditorState | editor state} for the object being edited.
|
|
89
89
|
* @returns `Success` with detail `'deferred'` and a
|
|
90
|
-
* {@link
|
|
90
|
+
* {@link EditorRules.IConditionalJsonKeyResult | IConditionalJsonKeyResult} describing the
|
|
91
91
|
* match for a default or matching conditional property. Returns `Failure` with detail `'ignore'`
|
|
92
92
|
* for a non-matching conditional property. Fails with detail `'error'` if an error occurs
|
|
93
93
|
* or with detail `'inapplicable'` if the key does not represent a conditional property.
|
|
@@ -26,7 +26,7 @@ const ts_json_base_1 = require("@fgv/ts-json-base");
|
|
|
26
26
|
const ts_utils_1 = require("@fgv/ts-utils");
|
|
27
27
|
const jsonEditorRule_1 = require("../jsonEditorRule");
|
|
28
28
|
/**
|
|
29
|
-
* The {@link
|
|
29
|
+
* The {@link EditorRules.ConditionalJsonEditorRule | ConditionalJsonEditorRule} evaluates
|
|
30
30
|
* properties with conditional keys, omitting non-matching keys and merging keys that match,
|
|
31
31
|
* or default keys only if no other keys match.
|
|
32
32
|
*
|
|
@@ -38,8 +38,8 @@ const jsonEditorRule_1 = require("../jsonEditorRule");
|
|
|
38
38
|
*/
|
|
39
39
|
class ConditionalJsonEditorRule extends jsonEditorRule_1.JsonEditorRuleBase {
|
|
40
40
|
/**
|
|
41
|
-
* Creates a new {@link
|
|
42
|
-
* @param options - Optional {@link
|
|
41
|
+
* Creates a new {@link EditorRules.ConditionalJsonEditorRule | ConditionalJsonEditorRule}.
|
|
42
|
+
* @param options - Optional {@link EditorRules.IConditionalJsonRuleOptions | configuration options}
|
|
43
43
|
* used for this rule.
|
|
44
44
|
*/
|
|
45
45
|
constructor(options) {
|
|
@@ -47,8 +47,8 @@ class ConditionalJsonEditorRule extends jsonEditorRule_1.JsonEditorRuleBase {
|
|
|
47
47
|
this._options = options;
|
|
48
48
|
}
|
|
49
49
|
/**
|
|
50
|
-
* Creates a new {@link
|
|
51
|
-
* @param options - Optional {@link
|
|
50
|
+
* Creates a new {@link EditorRules.ConditionalJsonEditorRule | ConditionalJsonEditorRule}.
|
|
51
|
+
* @param options - Optional {@link EditorRules.IConditionalJsonRuleOptions | configuration options}
|
|
52
52
|
* used for this rule.
|
|
53
53
|
*/
|
|
54
54
|
static create(options) {
|
|
@@ -57,10 +57,10 @@ class ConditionalJsonEditorRule extends jsonEditorRule_1.JsonEditorRuleBase {
|
|
|
57
57
|
/**
|
|
58
58
|
* Evaluates a property for conditional application.
|
|
59
59
|
* @param key - The key of the property to be considered
|
|
60
|
-
* @param value - The
|
|
61
|
-
* @param state - The {@link
|
|
60
|
+
* @param value - The `JsonValue` of the property to be considered.
|
|
61
|
+
* @param state - The {@link JsonEditorState | editor state} for the object being edited.
|
|
62
62
|
* @returns Returns `Success` with detail `'deferred'` and a
|
|
63
|
-
* {@link
|
|
63
|
+
* {@link EditorRules.IConditionalJsonDeferredObject | IConditionalJsonDeferredObject}.
|
|
64
64
|
* for a matching, default or unconditional key. Returns `Failure` with detail `'ignore'` for
|
|
65
65
|
* a non-matching conditional, or with detail `'error'` if an error occurs. Otherwise
|
|
66
66
|
* fails with detail `'inapplicable'`.
|
|
@@ -83,7 +83,7 @@ class ConditionalJsonEditorRule extends jsonEditorRule_1.JsonEditorRuleBase {
|
|
|
83
83
|
* Finalizes any deferred conditional properties. If the only deferred property is
|
|
84
84
|
* default, that property is emitted. Otherwise all matching properties are emitted.
|
|
85
85
|
* @param finalized - The deferred properties to be considered for merge.
|
|
86
|
-
* @param __state - The {@link
|
|
86
|
+
* @param __state - The {@link JsonEditorState | editor state} for the object
|
|
87
87
|
* being edited.
|
|
88
88
|
*/
|
|
89
89
|
finalizeProperties(finalized, __state) {
|
|
@@ -98,10 +98,10 @@ class ConditionalJsonEditorRule extends jsonEditorRule_1.JsonEditorRuleBase {
|
|
|
98
98
|
/**
|
|
99
99
|
* Determines if a given property key is conditional. Derived classes can override this
|
|
100
100
|
* method to use a different format for conditional properties.
|
|
101
|
-
* @param value - The
|
|
102
|
-
* @param state - The {@link
|
|
101
|
+
* @param value - The `JsonValue` of the property to be considered.
|
|
102
|
+
* @param state - The {@link JsonEditorState | editor state} for the object being edited.
|
|
103
103
|
* @returns `Success` with detail `'deferred'` and a
|
|
104
|
-
* {@link
|
|
104
|
+
* {@link EditorRules.IConditionalJsonKeyResult | IConditionalJsonKeyResult} describing the
|
|
105
105
|
* match for a default or matching conditional property. Returns `Failure` with detail `'ignore'`
|
|
106
106
|
* for a non-matching conditional property. Fails with detail `'error'` if an error occurs
|
|
107
107
|
* or with detail `'inapplicable'` if the key does not represent a conditional property.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"conditional.js","sourceRoot":"","sources":["../../../../src/packlets/editor/rules/conditional.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;;;AAEH,oDAAwE;AACxE,4CAAyG;AAEzG,sDAAuD;AAkCvD;;;;;;;;;;GAUG;AACH,MAAa,yBAA0B,SAAQ,mCAAkB;IAQ/D;;;;OAIG;IACH,YAAmB,OAAqC;QACtD,KAAK,EAAE,CAAC;QACR,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAC;IAC1B,CAAC;IAED;;;;OAIG;IACI,MAAM,CAAC,MAAM,CAAC,OAAqC;QACxD,OAAO,IAAA,wBAAa,EAAC,GAAG,EAAE,CAAC,IAAI,yBAAyB,CAAC,OAAO,CAAC,CAAC,CAAC;IACrE,CAAC;IAED;;;;;;;;;;OAUG;IACI,YAAY,CACjB,GAAW,EACX,KAAgB,EAChB,KAAsB;;QAEtB,MAAM,MAAM,GAAG,IAAI,CAAC,kBAAkB,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC,SAAS,CAAC,CAAC,QAAQ,EAAE,EAAE;YACxE,IAAI,IAAA,2BAAY,EAAC,KAAK,CAAC,EAAE,CAAC;gBACxB,MAAM,IAAI,mCAAwC,QAAQ,KAAE,KAAK,GAAE,CAAC;gBACpE,OAAO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AAM,IAAI,CAAA,MAAA,IAAI,CAAC,QAAQ,0CAAE,0BAA0B,MAAK,KAAK,IAAI,GAAG,CAAC,UAAU,CAAC,GAAG,CAAC,EAAE,CAAC;YACtF,OAAO,IAAA,4BAAiB,EAAC,EAAE,SAAS,EAAE,eAAe,EAAE,EAAE,UAAU,CAAC,CAAC;QACvE,CAAC;QACD,OAAO,IAAA,yBAAc,EAAC,cAAc,EAAE,cAAc,CAAC,CAAC;IACxD,CAAC;IAED;;;;;;;OAOG;IACO,QAAQ,CAAC,IAAY,EAAE,KAAa,EAAE,QAAgB;QAC9D,QAAQ,QAAQ,EAAE,CAAC;YACjB,KAAK,GAAG;gBACN,OAAO,IAAI,KAAK,KAAK,CAAC;YACxB,KAAK,GAAG;gBACN,OAAO,IAAI,GAAG,KAAK,CAAC;YACtB,KAAK,GAAG;gBACN,OAAO,IAAI,GAAG,KAAK,CAAC;YACtB,KAAK,IAAI;gBACP,OAAO,IAAI,IAAI,KAAK,CAAC;YACvB,KAAK,IAAI;gBACP,OAAO,IAAI,IAAI,KAAK,CAAC;YACvB,KAAK,IAAI;gBACP,OAAO,IAAI,KAAK,KAAK,CAAC;QAC1B,CAAC;QACD,oCAAoC;QACpC,OAAO,KAAK,CAAC;IACf,CAAC;CACF;AAvJD,8DAuJC","sourcesContent":["/*\n * Copyright (c) 2020 Erik Fortune\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in all\n * copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * SOFTWARE.\n */\n\nimport { JsonObject, JsonValue, isJsonObject } from '@fgv/ts-json-base';\nimport { DetailedResult, Result, captureResult, failWithDetail, succeedWithDetail } from '@fgv/ts-utils';\nimport { IJsonEditorOptions, JsonEditFailureReason, JsonPropertyEditFailureReason } from '../common';\nimport { JsonEditorRuleBase } from '../jsonEditorRule';\nimport { JsonEditorState } from '../jsonEditorState';\n\n/**\n * Returned by {@link Editor.Rules.ConditionalJsonEditorRule._tryParseCondition | ConditionalJsonEditorRule._tryParseCondition}\n * to indicate whether a successful match was due to a matching condition or a default value.\n * @public\n */\nexport interface IConditionalJsonKeyResult extends JsonObject {\n matchType: 'default' | 'match' | 'unconditional';\n}\n\n/**\n * On a successful match, the {@link Editor.Rules.ConditionalJsonEditorRule | ConditionalJsonEditorRule}\n * stores a {@link Editor.Rules.IConditionalJsonDeferredObject | IConditionalJsonDeferredObject} describing the\n * matching result, to be resolved at finalization time.\n * @public\n */\nexport interface IConditionalJsonDeferredObject extends IConditionalJsonKeyResult {\n value: JsonValue;\n}\n\n/**\n * Configuration options for the {@link Editor.Rules.ConditionalJsonEditorRule | ConditionalJsonEditorRule}.\n * @public\n */\nexport interface IConditionalJsonRuleOptions extends Partial<IJsonEditorOptions> {\n /**\n * If true (default) then properties with unconditional names\n * (which start with !) are flattened.\n */\n flattenUnconditionalValues?: boolean;\n}\n\n/**\n * The {@link Editor.Rules.ConditionalJsonEditorRule | ConditionalJsonEditorRule} evaluates\n * properties with conditional keys, omitting non-matching keys and merging keys that match,\n * or default keys only if no other keys match.\n *\n * The default syntax for a conditional key is:\n * \"?value1=value2\" - matches if value1 and value2 are the same, is ignored otherwise.\n * \"?value\" - matches if value is a non-empty, non-whitespace string. Is ignored otherwise.\n * \"?default\" - matches only if no other conditional blocks in the same object were matched.\n * @public\n */\nexport class ConditionalJsonEditorRule extends JsonEditorRuleBase {\n /**\n * Stored fully-resolved {@link Editor.Rules.IConditionalJsonRuleOptions | options} for this\n * rule.\n * @public\n */\n protected _options?: IConditionalJsonRuleOptions;\n\n /**\n * Creates a new {@link Editor.Rules.ConditionalJsonEditorRule | ConditionalJsonEditorRule}.\n * @param options - Optional {@link Editor.Rules.IConditionalJsonRuleOptions | configuration options}\n * used for this rule.\n */\n public constructor(options?: IConditionalJsonRuleOptions) {\n super();\n this._options = options;\n }\n\n /**\n * Creates a new {@link Editor.Rules.ConditionalJsonEditorRule | ConditionalJsonEditorRule}.\n * @param options - Optional {@link Editor.Rules.IConditionalJsonRuleOptions | configuration options}\n * used for this rule.\n */\n public static create(options?: IConditionalJsonRuleOptions): Result<ConditionalJsonEditorRule> {\n return captureResult(() => new ConditionalJsonEditorRule(options));\n }\n\n /**\n * Evaluates a property for conditional application.\n * @param key - The key of the property to be considered\n * @param value - The {@link JsonValue | value} of the property to be considered.\n * @param state - The {@link Editor.JsonEditorState | editor state} for the object being edited.\n * @returns Returns `Success` with detail `'deferred'` and a\n * {@link Editor.Rules.IConditionalJsonDeferredObject | IConditionalJsonDeferredObject}.\n * for a matching, default or unconditional key. Returns `Failure` with detail `'ignore'` for\n * a non-matching conditional, or with detail `'error'` if an error occurs. Otherwise\n * fails with detail `'inapplicable'`.\n */\n public editProperty(\n key: string,\n value: JsonValue,\n state: JsonEditorState\n ): DetailedResult<JsonObject, JsonPropertyEditFailureReason> {\n const result = this._tryParseCondition(key, state).onSuccess((deferred) => {\n if (isJsonObject(value)) {\n const rtrn: IConditionalJsonDeferredObject = { ...deferred, value };\n return succeedWithDetail(rtrn, 'deferred');\n }\n return failWithDetail<JsonObject, JsonPropertyEditFailureReason>(\n `${key}: conditional body must be object`,\n 'error'\n );\n });\n\n if (result.isFailure() && result.detail === 'error') {\n return state.failValidation('invalidPropertyName', result.message, this._options?.validation);\n }\n\n return result;\n }\n\n /**\n * Finalizes any deferred conditional properties. If the only deferred property is\n * default, that property is emitted. Otherwise all matching properties are emitted.\n * @param finalized - The deferred properties to be considered for merge.\n * @param __state - The {@link Editor.JsonEditorState | editor state} for the object\n * being edited.\n */\n public finalizeProperties(\n finalized: JsonObject[],\n __state: JsonEditorState\n ): DetailedResult<JsonObject[], JsonEditFailureReason> {\n let toMerge = finalized;\n if (finalized.length > 1) {\n if (finalized.find((o) => o.matchType === 'match') !== undefined) {\n toMerge = finalized.filter((o) => o.matchType === 'match' || o.matchType === 'unconditional');\n }\n }\n return succeedWithDetail(toMerge.map((o) => o.value).filter(isJsonObject), 'edited');\n }\n\n /**\n * Determines if a given property key is conditional. Derived classes can override this\n * method to use a different format for conditional properties.\n * @param value - The {@link JsonValue | value} of the property to be considered.\n * @param state - The {@link Editor.JsonEditorState | editor state} for the object being edited.\n * @returns `Success` with detail `'deferred'` and a\n * {@link Editor.Rules.IConditionalJsonKeyResult | IConditionalJsonKeyResult} describing the\n * match for a default or matching conditional property. Returns `Failure` with detail `'ignore'`\n * for a non-matching conditional property. Fails with detail `'error'` if an error occurs\n * or with detail `'inapplicable'` if the key does not represent a conditional property.\n * @public\n */\n protected _tryParseCondition(\n key: string,\n state: JsonEditorState\n ): DetailedResult<IConditionalJsonKeyResult, JsonPropertyEditFailureReason> {\n if (key.startsWith('?')) {\n // ignore everything after any #\n key = key.split('#')[0].trim();\n\n if (key === '?default') {\n return succeedWithDetail({ matchType: 'default' }, 'deferred');\n }\n\n const parts = key.substring(1).split(/(=|>=|<=|>|<|!=)/);\n if (parts.length === 3) {\n if (!this._compare(parts[0].trim(), parts[2].trim(), parts[1])) {\n return failWithDetail(`Condition ${key} does not match`, 'ignore');\n }\n return succeedWithDetail({ matchType: 'match' }, 'deferred');\n } else if (parts.length === 1) {\n if (parts[0].trim().length === 0) {\n return failWithDetail(`Condition ${key} does not match`, 'ignore');\n }\n return succeedWithDetail({ matchType: 'match' }, 'deferred');\n }\n const message = `Malformed condition token ${key}`;\n return state.failValidation('invalidPropertyName', message, this._options?.validation);\n } else if (this._options?.flattenUnconditionalValues !== false && key.startsWith('!')) {\n return succeedWithDetail({ matchType: 'unconditional' }, 'deferred');\n }\n return failWithDetail('inapplicable', 'inapplicable');\n }\n\n /**\n * Compares two strings using a supplied operator.\n * @param left - The first string to be compared.\n * @param right - The second string to be compared.\n * @param operator - The operator to be applied.\n * @returns `true` if the condition is met, `false` otherwise.\n * @internal\n */\n protected _compare(left: string, right: string, operator: string): boolean {\n switch (operator) {\n case '=':\n return left === right;\n case '>':\n return left > right;\n case '<':\n return left < right;\n case '>=':\n return left >= right;\n case '<=':\n return left <= right;\n case '!=':\n return left !== right;\n }\n /* c8 ignore next 2 - unreachable */\n return false;\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"conditional.js","sourceRoot":"","sources":["../../../../src/packlets/editor/rules/conditional.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;;;AAEH,oDAAwE;AACxE,4CAAyG;AAEzG,sDAAuD;AAkCvD;;;;;;;;;;GAUG;AACH,MAAa,yBAA0B,SAAQ,mCAAkB;IAQ/D;;;;OAIG;IACH,YAAmB,OAAqC;QACtD,KAAK,EAAE,CAAC;QACR,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAC;IAC1B,CAAC;IAED;;;;OAIG;IACI,MAAM,CAAC,MAAM,CAAC,OAAqC;QACxD,OAAO,IAAA,wBAAa,EAAC,GAAG,EAAE,CAAC,IAAI,yBAAyB,CAAC,OAAO,CAAC,CAAC,CAAC;IACrE,CAAC;IAED;;;;;;;;;;OAUG;IACI,YAAY,CACjB,GAAW,EACX,KAAgB,EAChB,KAAsB;;QAEtB,MAAM,MAAM,GAAG,IAAI,CAAC,kBAAkB,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC,SAAS,CAAC,C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AA6B,GAAG,EAAE,CAAC;YACnD,OAAO,KAAK,CAAC,cAAc,CAAC,qBAAqB,EAAE,OAAO,EAAE,MAAA,IAAI,CAAC,QAAQ,0CAAE,UAAU,CAAC,CAAC;QACzF,CAAC;aAAM,IAAI,CAAA,MAAA,IAAI,CAAC,QAAQ,0CAAE,0BAA0B,MAAK,KAAK,IAAI,GAAG,CAAC,UAAU,CAAC,GAAG,CAAC,EAAE,CAAC;YACtF,OAAO,IAAA,4BAAiB,EAAC,EAAE,SAAS,EAAE,eAAe,EAAE,EAAE,UAAU,CAAC,CAAC;QACvE,CAAC;QACD,OAAO,IAAA,yBAAc,EAAC,cAAc,EAAE,cAAc,CAAC,CAAC;IACxD,CAAC;IAED;;;;;;;OAOG;IACO,QAAQ,CAAC,IAAY,EAAE,KAAa,EAAE,QAAgB;QAC9D,QAAQ,QAAQ,EAAE,CAAC;YACjB,KAAK,GAAG;gBACN,OAAO,IAAI,KAAK,KAAK,CAAC;YACxB,KAAK,GAAG;gBACN,OAAO,IAAI,GAAG,KAAK,CAAC;YACtB,KAAK,GAAG;gBACN,OAAO,IAAI,GAAG,KAAK,CAAC;YACtB,KAAK,IAAI;gBACP,OAAO,IAAI,IAAI,KAAK,CAAC;YACvB,KAAK,IAAI;gBACP,OAAO,IAAI,IAAI,KAAK,CAAC;YACvB,KAAK,IAAI;gBACP,OAAO,IAAI,KAAK,KAAK,CAAC;QAC1B,CAAC;QACD,oCAAoC;QACpC,OAAO,KAAK,CAAC;IACf,CAAC;CACF;AAvJD,8DAuJC","sourcesContent":["/*\n * Copyright (c) 2020 Erik Fortune\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in all\n * copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * SOFTWARE.\n */\n\nimport { JsonObject, JsonValue, isJsonObject } from '@fgv/ts-json-base';\nimport { DetailedResult, Result, captureResult, failWithDetail, succeedWithDetail } from '@fgv/ts-utils';\nimport { IJsonEditorOptions, JsonEditFailureReason, JsonPropertyEditFailureReason } from '../common';\nimport { JsonEditorRuleBase } from '../jsonEditorRule';\nimport { JsonEditorState } from '../jsonEditorState';\n\n/**\n * Returned by {@link EditorRules.ConditionalJsonEditorRule._tryParseCondition | ConditionalJsonEditorRule._tryParseCondition}\n * to indicate whether a successful match was due to a matching condition or a default value.\n * @public\n */\nexport interface IConditionalJsonKeyResult extends JsonObject {\n matchType: 'default' | 'match' | 'unconditional';\n}\n\n/**\n * On a successful match, the {@link EditorRules.ConditionalJsonEditorRule | ConditionalJsonEditorRule}\n * stores a {@link EditorRules.IConditionalJsonDeferredObject | IConditionalJsonDeferredObject} describing the\n * matching result, to be resolved at finalization time.\n * @public\n */\nexport interface IConditionalJsonDeferredObject extends IConditionalJsonKeyResult {\n value: JsonValue;\n}\n\n/**\n * Configuration options for the {@link EditorRules.ConditionalJsonEditorRule | ConditionalJsonEditorRule}.\n * @public\n */\nexport interface IConditionalJsonRuleOptions extends Partial<IJsonEditorOptions> {\n /**\n * If true (default) then properties with unconditional names\n * (which start with !) are flattened.\n */\n flattenUnconditionalValues?: boolean;\n}\n\n/**\n * The {@link EditorRules.ConditionalJsonEditorRule | ConditionalJsonEditorRule} evaluates\n * properties with conditional keys, omitting non-matching keys and merging keys that match,\n * or default keys only if no other keys match.\n *\n * The default syntax for a conditional key is:\n * \"?value1=value2\" - matches if value1 and value2 are the same, is ignored otherwise.\n * \"?value\" - matches if value is a non-empty, non-whitespace string. Is ignored otherwise.\n * \"?default\" - matches only if no other conditional blocks in the same object were matched.\n * @public\n */\nexport class ConditionalJsonEditorRule extends JsonEditorRuleBase {\n /**\n * Stored fully-resolved {@link EditorRules.IConditionalJsonRuleOptions | options} for this\n * rule.\n * @public\n */\n protected _options?: IConditionalJsonRuleOptions;\n\n /**\n * Creates a new {@link EditorRules.ConditionalJsonEditorRule | ConditionalJsonEditorRule}.\n * @param options - Optional {@link EditorRules.IConditionalJsonRuleOptions | configuration options}\n * used for this rule.\n */\n public constructor(options?: IConditionalJsonRuleOptions) {\n super();\n this._options = options;\n }\n\n /**\n * Creates a new {@link EditorRules.ConditionalJsonEditorRule | ConditionalJsonEditorRule}.\n * @param options - Optional {@link EditorRules.IConditionalJsonRuleOptions | configuration options}\n * used for this rule.\n */\n public static create(options?: IConditionalJsonRuleOptions): Result<ConditionalJsonEditorRule> {\n return captureResult(() => new ConditionalJsonEditorRule(options));\n }\n\n /**\n * Evaluates a property for conditional application.\n * @param key - The key of the property to be considered\n * @param value - The `JsonValue` of the property to be considered.\n * @param state - The {@link JsonEditorState | editor state} for the object being edited.\n * @returns Returns `Success` with detail `'deferred'` and a\n * {@link EditorRules.IConditionalJsonDeferredObject | IConditionalJsonDeferredObject}.\n * for a matching, default or unconditional key. Returns `Failure` with detail `'ignore'` for\n * a non-matching conditional, or with detail `'error'` if an error occurs. Otherwise\n * fails with detail `'inapplicable'`.\n */\n public editProperty(\n key: string,\n value: JsonValue,\n state: JsonEditorState\n ): DetailedResult<JsonObject, JsonPropertyEditFailureReason> {\n const result = this._tryParseCondition(key, state).onSuccess((deferred) => {\n if (isJsonObject(value)) {\n const rtrn: IConditionalJsonDeferredObject = { ...deferred, value };\n return succeedWithDetail(rtrn, 'deferred');\n }\n return failWithDetail<JsonObject, JsonPropertyEditFailureReason>(\n `${key}: conditional body must be object`,\n 'error'\n );\n });\n\n if (result.isFailure() && result.detail === 'error') {\n return state.failValidation('invalidPropertyName', result.message, this._options?.validation);\n }\n\n return result;\n }\n\n /**\n * Finalizes any deferred conditional properties. If the only deferred property is\n * default, that property is emitted. Otherwise all matching properties are emitted.\n * @param finalized - The deferred properties to be considered for merge.\n * @param __state - The {@link JsonEditorState | editor state} for the object\n * being edited.\n */\n public finalizeProperties(\n finalized: JsonObject[],\n __state: JsonEditorState\n ): DetailedResult<JsonObject[], JsonEditFailureReason> {\n let toMerge = finalized;\n if (finalized.length > 1) {\n if (finalized.find((o) => o.matchType === 'match') !== undefined) {\n toMerge = finalized.filter((o) => o.matchType === 'match' || o.matchType === 'unconditional');\n }\n }\n return succeedWithDetail(toMerge.map((o) => o.value).filter(isJsonObject), 'edited');\n }\n\n /**\n * Determines if a given property key is conditional. Derived classes can override this\n * method to use a different format for conditional properties.\n * @param value - The `JsonValue` of the property to be considered.\n * @param state - The {@link JsonEditorState | editor state} for the object being edited.\n * @returns `Success` with detail `'deferred'` and a\n * {@link EditorRules.IConditionalJsonKeyResult | IConditionalJsonKeyResult} describing the\n * match for a default or matching conditional property. Returns `Failure` with detail `'ignore'`\n * for a non-matching conditional property. Fails with detail `'error'` if an error occurs\n * or with detail `'inapplicable'` if the key does not represent a conditional property.\n * @public\n */\n protected _tryParseCondition(\n key: string,\n state: JsonEditorState\n ): DetailedResult<IConditionalJsonKeyResult, JsonPropertyEditFailureReason> {\n if (key.startsWith('?')) {\n // ignore everything after any #\n key = key.split('#')[0].trim();\n\n if (key === '?default') {\n return succeedWithDetail({ matchType: 'default' }, 'deferred');\n }\n\n const parts = key.substring(1).split(/(=|>=|<=|>|<|!=)/);\n if (parts.length === 3) {\n if (!this._compare(parts[0].trim(), parts[2].trim(), parts[1])) {\n return failWithDetail(`Condition ${key} does not match`, 'ignore');\n }\n return succeedWithDetail({ matchType: 'match' }, 'deferred');\n } else if (parts.length === 1) {\n if (parts[0].trim().length === 0) {\n return failWithDetail(`Condition ${key} does not match`, 'ignore');\n }\n return succeedWithDetail({ matchType: 'match' }, 'deferred');\n }\n const message = `Malformed condition token ${key}`;\n return state.failValidation('invalidPropertyName', message, this._options?.validation);\n } else if (this._options?.flattenUnconditionalValues !== false && key.startsWith('!')) {\n return succeedWithDetail({ matchType: 'unconditional' }, 'deferred');\n }\n return failWithDetail('inapplicable', 'inapplicable');\n }\n\n /**\n * Compares two strings using a supplied operator.\n * @param left - The first string to be compared.\n * @param right - The second string to be compared.\n * @param operator - The operator to be applied.\n * @returns `true` if the condition is met, `false` otherwise.\n * @internal\n */\n protected _compare(left: string, right: string, operator: string): boolean {\n switch (operator) {\n case '=':\n return left === right;\n case '>':\n return left > right;\n case '<':\n return left < right;\n case '>=':\n return left >= right;\n case '<=':\n return left <= right;\n case '!=':\n return left !== right;\n }\n /* c8 ignore next 2 - unreachable */\n return false;\n }\n}\n"]}
|
|
@@ -25,13 +25,13 @@ export interface IMultiValuePropertyParts {
|
|
|
25
25
|
readonly propertyValues: string[];
|
|
26
26
|
/**
|
|
27
27
|
* If `true`, the resolved values are added as an array
|
|
28
|
-
* with the name of the {@link
|
|
28
|
+
* with the name of the {@link EditorRules.IMultiValuePropertyParts.propertyVariable | propertyVariable}.
|
|
29
29
|
* If false, values are added as individual properties with names that correspond the value.
|
|
30
30
|
*/
|
|
31
31
|
readonly asArray: boolean;
|
|
32
32
|
}
|
|
33
33
|
/**
|
|
34
|
-
* The {@link
|
|
34
|
+
* The {@link EditorRules.MultiValueJsonEditorRule | Multi-Value JSON editor rule}
|
|
35
35
|
* expands matching keys multiple times, projecting the value into the template
|
|
36
36
|
* context for any child objects rendered by the rule.
|
|
37
37
|
*
|
|
@@ -44,26 +44,26 @@ export interface IMultiValuePropertyParts {
|
|
|
44
44
|
*/
|
|
45
45
|
export declare class MultiValueJsonEditorRule extends JsonEditorRuleBase {
|
|
46
46
|
/**
|
|
47
|
-
* Stored fully-resolved {@link
|
|
47
|
+
* Stored fully-resolved {@link IJsonEditorOptions | editor options}
|
|
48
48
|
* for this rule.
|
|
49
49
|
* @public
|
|
50
50
|
*/
|
|
51
51
|
protected _options?: IJsonEditorOptions;
|
|
52
52
|
/**
|
|
53
|
-
* Creates a new {@link
|
|
54
|
-
* @param options - Optional {@link
|
|
53
|
+
* Creates a new {@link EditorRules.MultiValueJsonEditorRule | MultiValueJsonEditorRule}.
|
|
54
|
+
* @param options - Optional {@link IJsonEditorOptions | configuration options}.
|
|
55
55
|
*/
|
|
56
56
|
constructor(options?: IJsonEditorOptions);
|
|
57
57
|
/**
|
|
58
|
-
* Creates a new {@link
|
|
59
|
-
* @param options - Optional {@link
|
|
58
|
+
* Creates a new {@link EditorRules.MultiValueJsonEditorRule | MultiValueJsonEditorRule}.
|
|
59
|
+
* @param options - Optional {@link IJsonEditorOptions | configuration options}.
|
|
60
60
|
*/
|
|
61
61
|
static create(options?: IJsonEditorOptions): Result<MultiValueJsonEditorRule>;
|
|
62
62
|
/**
|
|
63
63
|
* Evaluates a property for multi-value expansion.
|
|
64
64
|
* @param key - The key of the property to be considered
|
|
65
|
-
* @param value - The
|
|
66
|
-
* @param state - The {@link
|
|
65
|
+
* @param value - The `JsonValue` of the property to be considered.
|
|
66
|
+
* @param state - The {@link JsonEditorState | editor state} for the object being edited.
|
|
67
67
|
* @returns `Success` with an object containing the fully-resolved child values to be merged for
|
|
68
68
|
* matching multi-value property. Returns `Failure` with detail `'error'` if an error occurs or
|
|
69
69
|
* with detail `'inapplicable'` if the property key is not a conditional property.
|
|
@@ -71,7 +71,7 @@ export declare class MultiValueJsonEditorRule extends JsonEditorRuleBase {
|
|
|
71
71
|
editProperty(key: string, value: JsonValue, state: JsonEditorState): DetailedResult<JsonObject, JsonPropertyEditFailureReason>;
|
|
72
72
|
/**
|
|
73
73
|
* Extends the {@link IJsonContext | current context} with a supplied state and values.
|
|
74
|
-
* @param state - The {@link
|
|
74
|
+
* @param state - The {@link JsonEditorState | editor state} for the object being edited.
|
|
75
75
|
* @param values - An array of {@link VariableValue | VariableValue} to be added to the
|
|
76
76
|
* context.
|
|
77
77
|
* @returns The extended {@link IJsonContext | context}.
|
|
@@ -81,10 +81,10 @@ export declare class MultiValueJsonEditorRule extends JsonEditorRuleBase {
|
|
|
81
81
|
/**
|
|
82
82
|
* Determines if a given property key is multi-value. Derived classes can override this
|
|
83
83
|
* method to use a different format for multi-value properties.
|
|
84
|
-
* @param value - The
|
|
85
|
-
* @param state - The {@link
|
|
84
|
+
* @param value - The `JsonValue` of the property to be considered.
|
|
85
|
+
* @param state - The {@link JsonEditorState | editor state} for the object being edited.
|
|
86
86
|
* @returns `Success` with detail `'deferred'` and an
|
|
87
|
-
* {@link
|
|
87
|
+
* {@link EditorRules.IMultiValuePropertyParts | IMultiValuePropertyParts}
|
|
88
88
|
* describing the match for matching multi-value property. Returns `Failure` with detail `'error'` if an error occurs
|
|
89
89
|
* or with detail `'inapplicable'` if the key does not represent a multi-value property.
|
|
90
90
|
* @public
|
|
@@ -25,7 +25,7 @@ exports.MultiValueJsonEditorRule = void 0;
|
|
|
25
25
|
const ts_utils_1 = require("@fgv/ts-utils");
|
|
26
26
|
const jsonEditorRule_1 = require("../jsonEditorRule");
|
|
27
27
|
/**
|
|
28
|
-
* The {@link
|
|
28
|
+
* The {@link EditorRules.MultiValueJsonEditorRule | Multi-Value JSON editor rule}
|
|
29
29
|
* expands matching keys multiple times, projecting the value into the template
|
|
30
30
|
* context for any child objects rendered by the rule.
|
|
31
31
|
*
|
|
@@ -38,16 +38,16 @@ const jsonEditorRule_1 = require("../jsonEditorRule");
|
|
|
38
38
|
*/
|
|
39
39
|
class MultiValueJsonEditorRule extends jsonEditorRule_1.JsonEditorRuleBase {
|
|
40
40
|
/**
|
|
41
|
-
* Creates a new {@link
|
|
42
|
-
* @param options - Optional {@link
|
|
41
|
+
* Creates a new {@link EditorRules.MultiValueJsonEditorRule | MultiValueJsonEditorRule}.
|
|
42
|
+
* @param options - Optional {@link IJsonEditorOptions | configuration options}.
|
|
43
43
|
*/
|
|
44
44
|
constructor(options) {
|
|
45
45
|
super();
|
|
46
46
|
this._options = options;
|
|
47
47
|
}
|
|
48
48
|
/**
|
|
49
|
-
* Creates a new {@link
|
|
50
|
-
* @param options - Optional {@link
|
|
49
|
+
* Creates a new {@link EditorRules.MultiValueJsonEditorRule | MultiValueJsonEditorRule}.
|
|
50
|
+
* @param options - Optional {@link IJsonEditorOptions | configuration options}.
|
|
51
51
|
*/
|
|
52
52
|
static create(options) {
|
|
53
53
|
return (0, ts_utils_1.captureResult)(() => new MultiValueJsonEditorRule(options));
|
|
@@ -55,8 +55,8 @@ class MultiValueJsonEditorRule extends jsonEditorRule_1.JsonEditorRuleBase {
|
|
|
55
55
|
/**
|
|
56
56
|
* Evaluates a property for multi-value expansion.
|
|
57
57
|
* @param key - The key of the property to be considered
|
|
58
|
-
* @param value - The
|
|
59
|
-
* @param state - The {@link
|
|
58
|
+
* @param value - The `JsonValue` of the property to be considered.
|
|
59
|
+
* @param state - The {@link JsonEditorState | editor state} for the object being edited.
|
|
60
60
|
* @returns `Success` with an object containing the fully-resolved child values to be merged for
|
|
61
61
|
* matching multi-value property. Returns `Failure` with detail `'error'` if an error occurs or
|
|
62
62
|
* with detail `'inapplicable'` if the property key is not a conditional property.
|
|
@@ -89,7 +89,7 @@ class MultiValueJsonEditorRule extends jsonEditorRule_1.JsonEditorRuleBase {
|
|
|
89
89
|
}
|
|
90
90
|
/**
|
|
91
91
|
* Extends the {@link IJsonContext | current context} with a supplied state and values.
|
|
92
|
-
* @param state - The {@link
|
|
92
|
+
* @param state - The {@link JsonEditorState | editor state} for the object being edited.
|
|
93
93
|
* @param values - An array of {@link VariableValue | VariableValue} to be added to the
|
|
94
94
|
* context.
|
|
95
95
|
* @returns The extended {@link IJsonContext | context}.
|
|
@@ -102,10 +102,10 @@ class MultiValueJsonEditorRule extends jsonEditorRule_1.JsonEditorRuleBase {
|
|
|
102
102
|
/**
|
|
103
103
|
* Determines if a given property key is multi-value. Derived classes can override this
|
|
104
104
|
* method to use a different format for multi-value properties.
|
|
105
|
-
* @param value - The
|
|
106
|
-
* @param state - The {@link
|
|
105
|
+
* @param value - The `JsonValue` of the property to be considered.
|
|
106
|
+
* @param state - The {@link JsonEditorState | editor state} for the object being edited.
|
|
107
107
|
* @returns `Success` with detail `'deferred'` and an
|
|
108
|
-
* {@link
|
|
108
|
+
* {@link EditorRules.IMultiValuePropertyParts | IMultiValuePropertyParts}
|
|
109
109
|
* describing the match for matching multi-value property. Returns `Failure` with detail `'error'` if an error occurs
|
|
110
110
|
* or with detail `'inapplicable'` if the key does not represent a multi-value property.
|
|
111
111
|
* @public
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"multivalue.js","sourceRoot":"","sources":["../../../../src/packlets/editor/rules/multivalue.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;;;AAGH,4CAQuB;AAIvB,sDAAuD;AAiCvD;;;;;;;;;;;GAWG;AACH,MAAa,wBAAyB,SAAQ,mCAAkB;IAQ9D;;;OAGG;IACH,YAAmB,OAA4B;QAC7C,KAAK,EAAE,CAAC;QACR,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAC;IAC1B,CAAC;IAED;;;OAGG;IACI,MAAM,CAAC,MAAM,CAAC,OAA4B;QAC/C,OAAO,IAAA,wBAAa,EAAC,GAAG,EAAE,CAAC,IAAI,wBAAwB,CAAC,OAAO,CAAC,CAAC,CAAC;IACpE,CAAC;IAED;;;;;;;;OAQG;IACI,YAAY,CACjB,GAAW,EACX,KAAgB,EAChB,KAAsB;QAEtB,MAAM,IAAI,GAAe,EAAE,CAAC;QAC5B,MAAM,MAAM,GAAG,IAAI,CAAC,SAAS,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC,SAAS,CAAC,CAAC,KAAK,EAAE,EAAE;YAC5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C,KAAK,CAAC,GAAG,CAAC,CAAC;QAC3C,OAAO,IAAA,4BAAiB,EAAC,EAAE,KAAK,EAAE,gBAAgB,EAAE,cAAc,EAAE,OAAO,EAAE,CAAC,CAAC;IACjF,CAAC;CACF;AA7HD,4DA6HC","sourcesContent":["/*\n * Copyright (c) 2020 Erik Fortune\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in all\n * copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * SOFTWARE.\n */\n\nimport { JsonObject, JsonValue } from '@fgv/ts-json-base';\nimport {\n DetailedResult,\n Result,\n allSucceed,\n captureResult,\n failWithDetail,\n succeed,\n succeedWithDetail\n} from '@fgv/ts-utils';\nimport { IJsonContext, VariableValue } from '../../context';\nimport { IJsonEditorOptions, JsonEditFailureReason, JsonPropertyEditFailureReason } from '../common';\n\nimport { JsonEditorRuleBase } from '../jsonEditorRule';\nimport { JsonEditorState } from '../jsonEditorState';\n\n/**\n * Represents the parts of a multi-value property key.\n * @public\n */\nexport interface IMultiValuePropertyParts {\n /**\n * The original matched token.\n */\n readonly token: string;\n\n /**\n * The name of the variable used to project each possible\n * property value into the child values or objects being\n * resolved.\n */\n readonly propertyVariable: string;\n\n /**\n * The set of property values to be expanded.\n */\n readonly propertyValues: string[];\n\n /**\n * If `true`, the resolved values are added as an array\n * with the name of the {@link Editor.Rules.IMultiValuePropertyParts.propertyVariable | propertyVariable}.\n * If false, values are added as individual properties with names that correspond the value.\n */\n readonly asArray: boolean;\n}\n\n/**\n * The {@link Editor.Rules.MultiValueJsonEditorRule | Multi-Value JSON editor rule}\n * expands matching keys multiple times, projecting the value into the template\n * context for any child objects rendered by the rule.\n *\n * The default syntax for a multi-value key is:\n * \"[[var]]=value1,value2,value3\"\n * Where \"var\" is the name of the variable that will be passed to\n * child template resolution, and \"value1,value2,value3\" is a\n * comma-separated list of values to be expanded.\n * @public\n */\nexport class MultiValueJsonEditorRule extends JsonEditorRuleBase {\n /**\n * Stored fully-resolved {@link Editor.IJsonEditorOptions | editor options}\n * for this rule.\n * @public\n */\n protected _options?: IJsonEditorOptions;\n\n /**\n * Creates a new {@link Editor.Rules.MultiValueJsonEditorRule | MultiValueJsonEditorRule}.\n * @param options - Optional {@link Editor.IJsonEditorOptions | configuration options}.\n */\n public constructor(options?: IJsonEditorOptions) {\n super();\n this._options = options;\n }\n\n /**\n * Creates a new {@link Editor.Rules.MultiValueJsonEditorRule | MultiValueJsonEditorRule}.\n * @param options - Optional {@link Editor.IJsonEditorOptions | configuration options}.\n */\n public static create(options?: IJsonEditorOptions): Result<MultiValueJsonEditorRule> {\n return captureResult(() => new MultiValueJsonEditorRule(options));\n }\n\n /**\n * Evaluates a property for multi-value expansion.\n * @param key - The key of the property to be considered\n * @param value - The {@link JsonValue | value} of the property to be considered.\n * @param state - The {@link Editor.JsonEditorState | editor state} for the object being edited.\n * @returns `Success` with an object containing the fully-resolved child values to be merged for\n * matching multi-value property. Returns `Failure` with detail `'error'` if an error occurs or\n * with detail `'inapplicable'` if the property key is not a conditional property.\n */\n public editProperty(\n key: string,\n value: JsonValue,\n state: JsonEditorState\n ): DetailedResult<JsonObject, JsonPropertyEditFailureReason> {\n const json: JsonObject = {};\n const result = this._tryParse(key, state).onSuccess((parts) => {\n return allSucceed(\n parts.propertyValues.map((pv) => {\n return this._deriveContext(state, [parts.propertyVariable, pv]).onSuccess((ctx) => {\n return state.editor.clone(value, ctx).onSuccess((cloned) => {\n json[pv] = cloned;\n return succeedWithDetail(cloned);\n });\n });\n }),\n json\n )\n .onSuccess(() => {\n if (parts.asArray) {\n const arrayRtrn: JsonObject = {};\n arrayRtrn[parts.propertyVariable] = Array.from(Object.values(json));\n return succeed(arrayRtrn);\n }\n return succeed(json);\n })\n .withFailureDetail('error');\n });\n\n if (result.isFailure() && result.detail === 'error') {\n return state.failValidation('invalidPropertyName', result.message);\n }\n return result;\n }\n\n /**\n * Extends the {@link IJsonContext | current context} with a supplied state and values.\n * @param state - The {@link Editor.JsonEditorState | editor state} for the object being edited.\n * @param values - An array of {@link VariableValue | VariableValue} to be added to the\n * context.\n * @returns The extended {@link IJsonContext | context}.\n * @public\n */\n protected _deriveContext(\n state: JsonEditorState,\n ...values: VariableValue[]\n ): Result<IJsonContext | undefined> {\n return state.extendContext(this._options?.context, { vars: values });\n }\n\n /**\n * Determines if a given property key is multi-value. Derived classes can override this\n * method to use a different format for multi-value properties.\n * @param value - The {@link JsonValue | value} of the property to be considered.\n * @param state - The {@link Editor.JsonEditorState | editor state} for the object being edited.\n * @returns `Success` with detail `'deferred'` and an\n * {@link Editor.Rules.IMultiValuePropertyParts | IMultiValuePropertyParts}\n * describing the match for matching multi-value property. Returns `Failure` with detail `'error'` if an error occurs\n * or with detail `'inapplicable'` if the key does not represent a multi-value property.\n * @public\n */\n protected _tryParse(\n token: string,\n state: JsonEditorState\n ): DetailedResult<IMultiValuePropertyParts, JsonEditFailureReason> {\n let parts: string[] = [];\n let asArray = false;\n\n if (token.startsWith('[[')) {\n parts = token.substring(2).split(']]=');\n asArray = true;\n } else if (token.startsWith('*')) {\n parts = token.substring(1).split('=');\n asArray = false;\n } else {\n return failWithDetail(token, 'inapplicable');\n }\n\n if (parts.length !== 2) {\n const message = `Malformed multi-value property: ${token}`;\n return state.failValidation('invalidPropertyName', message, this._options?.validation);\n }\n\n if (parts[1].includes('{{')) {\n return failWithDetail('unresolved template', 'inapplicable');\n }\n\n const propertyVariable = parts[0];\n const propertyValues = parts[1].split(',');\n return succeedWithDetail({ token, propertyVariable, propertyValues, asArray });\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"multivalue.js","sourceRoot":"","sources":["../../../../src/packlets/editor/rules/multivalue.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;;;AAGH,4CAQuB;AAIvB,sDAAuD;AAiCvD;;;;;;;;;;;GAWG;AACH,MAAa,wBAAyB,SAAQ,mCAAkB;IAQ9D;;;OAGG;IACH,YAAmB,OAA4B;QAC7C,KAAK,EAAE,CAAC;QACR,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAC;IAC1B,CAAC;IAED;;;OAGG;IACI,MAAM,CAAC,MAAM,CAAC,OAA4B;QAC/C,OAAO,IAAA,wBAAa,EAAC,GAAG,EAAE,CAAC,IAAI,wBAAwB,CAAC,OAAO,CAAC,CAAC,CAAC;IACpE,CAAC;IAED;;;;;;;;OAQG;IACI,YAAY,CACjB,GAAW,EACX,KAAgB,EAChB,KAAsB;QAEtB,MAAM,IAAI,GAAe,EAAE,CAAC;QAC5B,MAAM,MAAM,GAAG,IAAI,CAAC,SAAS,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC,SAAS,CAAC,CAAC,KAAK,EAAE,EAAE;YAC5D,OAAO,IAAA,qBAAU,EACf,KAAK,CAAC,cAAc,CAAC,GAAG,CAAC,CAAC,EAAE,EAAE,EAAE;gBAC9B,OAAO,IAAI,CAAC,cAAc,CAAC,KAAK,EAAE,CAAC,KAAK,CAAC,gBAAgB,EAAE,EAAE,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,GAAG,EAAE,EAAE;oBAChF,OAAO,KAAK,CAA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,KAAK,CAAC;QAEpB,IAAI,KAAK,CAAC,UAAU,CAAC,IAAI,CAAC,EAAE,CAAC;YAC3B,KAAK,GAAG,KAAK,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;YACxC,OAAO,GAAG,IAAI,CAAC;QACjB,CAAC;aAAM,IAAI,KAAK,CAAC,UAAU,CAAC,GAAG,CAAC,EAAE,CAAC;YACjC,KAAK,GAAG,KAAK,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;YACtC,OAAO,GAAG,KAAK,CAAC;QAClB,CAAC;aAAM,CAAC;YACN,OAAO,IAAA,yBAAc,EAAC,KAAK,EAAE,cAAc,CAAC,CAAC;QAC/C,CAAC;QAED,IAAI,KAAK,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;YACvB,MAAM,OAAO,GAAG,mCAAmC,KAAK,EAAE,CAAC;YAC3D,OAAO,KAAK,CAAC,cAAc,CAAC,qBAAqB,EAAE,OAAO,EAAE,MAAA,IAAI,CAAC,QAAQ,0CAAE,UAAU,CAAC,CAAC;QACzF,CAAC;QAED,IAAI,KAAK,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,IAAI,CAAC,EAAE,CAAC;YAC5B,OAAO,IAAA,yBAAc,EAAC,qBAAqB,EAAE,cAAc,CAAC,CAAC;QAC/D,CAAC;QAED,MAAM,gBAAgB,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC;QAClC,MAAM,cAAc,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;QAC3C,OAAO,IAAA,4BAAiB,EAAC,EAAE,KAAK,EAAE,gBAAgB,EAAE,cAAc,EAAE,OAAO,EAAE,CAAC,CAAC;IACjF,CAAC;CACF;AA7HD,4DA6HC","sourcesContent":["/*\n * Copyright (c) 2020 Erik Fortune\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in all\n * copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * SOFTWARE.\n */\n\nimport { JsonObject, JsonValue } from '@fgv/ts-json-base';\nimport {\n DetailedResult,\n Result,\n allSucceed,\n captureResult,\n failWithDetail,\n succeed,\n succeedWithDetail\n} from '@fgv/ts-utils';\nimport { IJsonContext, VariableValue } from '../../context';\nimport { IJsonEditorOptions, JsonEditFailureReason, JsonPropertyEditFailureReason } from '../common';\n\nimport { JsonEditorRuleBase } from '../jsonEditorRule';\nimport { JsonEditorState } from '../jsonEditorState';\n\n/**\n * Represents the parts of a multi-value property key.\n * @public\n */\nexport interface IMultiValuePropertyParts {\n /**\n * The original matched token.\n */\n readonly token: string;\n\n /**\n * The name of the variable used to project each possible\n * property value into the child values or objects being\n * resolved.\n */\n readonly propertyVariable: string;\n\n /**\n * The set of property values to be expanded.\n */\n readonly propertyValues: string[];\n\n /**\n * If `true`, the resolved values are added as an array\n * with the name of the {@link EditorRules.IMultiValuePropertyParts.propertyVariable | propertyVariable}.\n * If false, values are added as individual properties with names that correspond the value.\n */\n readonly asArray: boolean;\n}\n\n/**\n * The {@link EditorRules.MultiValueJsonEditorRule | Multi-Value JSON editor rule}\n * expands matching keys multiple times, projecting the value into the template\n * context for any child objects rendered by the rule.\n *\n * The default syntax for a multi-value key is:\n * \"[[var]]=value1,value2,value3\"\n * Where \"var\" is the name of the variable that will be passed to\n * child template resolution, and \"value1,value2,value3\" is a\n * comma-separated list of values to be expanded.\n * @public\n */\nexport class MultiValueJsonEditorRule extends JsonEditorRuleBase {\n /**\n * Stored fully-resolved {@link IJsonEditorOptions | editor options}\n * for this rule.\n * @public\n */\n protected _options?: IJsonEditorOptions;\n\n /**\n * Creates a new {@link EditorRules.MultiValueJsonEditorRule | MultiValueJsonEditorRule}.\n * @param options - Optional {@link IJsonEditorOptions | configuration options}.\n */\n public constructor(options?: IJsonEditorOptions) {\n super();\n this._options = options;\n }\n\n /**\n * Creates a new {@link EditorRules.MultiValueJsonEditorRule | MultiValueJsonEditorRule}.\n * @param options - Optional {@link IJsonEditorOptions | configuration options}.\n */\n public static create(options?: IJsonEditorOptions): Result<MultiValueJsonEditorRule> {\n return captureResult(() => new MultiValueJsonEditorRule(options));\n }\n\n /**\n * Evaluates a property for multi-value expansion.\n * @param key - The key of the property to be considered\n * @param value - The `JsonValue` of the property to be considered.\n * @param state - The {@link JsonEditorState | editor state} for the object being edited.\n * @returns `Success` with an object containing the fully-resolved child values to be merged for\n * matching multi-value property. Returns `Failure` with detail `'error'` if an error occurs or\n * with detail `'inapplicable'` if the property key is not a conditional property.\n */\n public editProperty(\n key: string,\n value: JsonValue,\n state: JsonEditorState\n ): DetailedResult<JsonObject, JsonPropertyEditFailureReason> {\n const json: JsonObject = {};\n const result = this._tryParse(key, state).onSuccess((parts) => {\n return allSucceed(\n parts.propertyValues.map((pv) => {\n return this._deriveContext(state, [parts.propertyVariable, pv]).onSuccess((ctx) => {\n return state.editor.clone(value, ctx).onSuccess((cloned) => {\n json[pv] = cloned;\n return succeedWithDetail(cloned);\n });\n });\n }),\n json\n )\n .onSuccess(() => {\n if (parts.asArray) {\n const arrayRtrn: JsonObject = {};\n arrayRtrn[parts.propertyVariable] = Array.from(Object.values(json));\n return succeed(arrayRtrn);\n }\n return succeed(json);\n })\n .withFailureDetail('error');\n });\n\n if (result.isFailure() && result.detail === 'error') {\n return state.failValidation('invalidPropertyName', result.message);\n }\n return result;\n }\n\n /**\n * Extends the {@link IJsonContext | current context} with a supplied state and values.\n * @param state - The {@link JsonEditorState | editor state} for the object being edited.\n * @param values - An array of {@link VariableValue | VariableValue} to be added to the\n * context.\n * @returns The extended {@link IJsonContext | context}.\n * @public\n */\n protected _deriveContext(\n state: JsonEditorState,\n ...values: VariableValue[]\n ): Result<IJsonContext | undefined> {\n return state.extendContext(this._options?.context, { vars: values });\n }\n\n /**\n * Determines if a given property key is multi-value. Derived classes can override this\n * method to use a different format for multi-value properties.\n * @param value - The `JsonValue` of the property to be considered.\n * @param state - The {@link JsonEditorState | editor state} for the object being edited.\n * @returns `Success` with detail `'deferred'` and an\n * {@link EditorRules.IMultiValuePropertyParts | IMultiValuePropertyParts}\n * describing the match for matching multi-value property. Returns `Failure` with detail `'error'` if an error occurs\n * or with detail `'inapplicable'` if the key does not represent a multi-value property.\n * @public\n */\n protected _tryParse(\n token: string,\n state: JsonEditorState\n ): DetailedResult<IMultiValuePropertyParts, JsonEditFailureReason> {\n let parts: string[] = [];\n let asArray = false;\n\n if (token.startsWith('[[')) {\n parts = token.substring(2).split(']]=');\n asArray = true;\n } else if (token.startsWith('*')) {\n parts = token.substring(1).split('=');\n asArray = false;\n } else {\n return failWithDetail(token, 'inapplicable');\n }\n\n if (parts.length !== 2) {\n const message = `Malformed multi-value property: ${token}`;\n return state.failValidation('invalidPropertyName', message, this._options?.validation);\n }\n\n if (parts[1].includes('{{')) {\n return failWithDetail('unresolved template', 'inapplicable');\n }\n\n const propertyVariable = parts[0];\n const propertyValues = parts[1].split(',');\n return succeedWithDetail({ token, propertyVariable, propertyValues, asArray });\n }\n}\n"]}
|
|
@@ -5,7 +5,7 @@ import { IJsonEditorOptions, JsonEditFailureReason, JsonPropertyEditFailureReaso
|
|
|
5
5
|
import { JsonEditorRuleBase } from '../jsonEditorRule';
|
|
6
6
|
import { JsonEditorState } from '../jsonEditorState';
|
|
7
7
|
/**
|
|
8
|
-
* The {@link
|
|
8
|
+
* The {@link EditorRules.ReferenceJsonEditorRule | Reference JSON editor rule} replaces property
|
|
9
9
|
* keys or values that match some known object with a copy of that referenced object, formatted
|
|
10
10
|
* according to the current context.
|
|
11
11
|
*
|
|
@@ -27,40 +27,40 @@ import { JsonEditorState } from '../jsonEditorState';
|
|
|
27
27
|
*/
|
|
28
28
|
export declare class ReferenceJsonEditorRule extends JsonEditorRuleBase {
|
|
29
29
|
/**
|
|
30
|
-
* Stored fully-resolved {@link
|
|
30
|
+
* Stored fully-resolved {@link IJsonEditorOptions | editor options} for this rule.
|
|
31
31
|
* @public
|
|
32
32
|
*/
|
|
33
33
|
protected _options?: IJsonEditorOptions;
|
|
34
34
|
/**
|
|
35
|
-
* Creates a new {@link
|
|
36
|
-
* @param options - Optional {@link
|
|
35
|
+
* Creates a new {@link EditorRules.ReferenceJsonEditorRule | ReferenceJsonEditorRule}.
|
|
36
|
+
* @param options - Optional {@link IJsonEditorOptions | configuration options} for this rule.
|
|
37
37
|
*/
|
|
38
38
|
constructor(options?: IJsonEditorOptions);
|
|
39
39
|
/**
|
|
40
|
-
* Creates a new {@link
|
|
41
|
-
* @param options - Optional {@link
|
|
40
|
+
* Creates a new {@link EditorRules.ReferenceJsonEditorRule | ReferenceJsonEditorRule}.
|
|
41
|
+
* @param options - Optional {@link IJsonEditorOptions | configuration options} for this rule.
|
|
42
42
|
*/
|
|
43
43
|
static create(options?: IJsonEditorOptions): Result<ReferenceJsonEditorRule>;
|
|
44
44
|
/**
|
|
45
45
|
* Evaluates a property for reference expansion.
|
|
46
46
|
* @param key - The key of the property to be considered.
|
|
47
|
-
* @param value - The
|
|
48
|
-
* @param state - The {@link
|
|
49
|
-
* @returns If the reference is successful, returns `Success` with a
|
|
47
|
+
* @param value - The `JsonValue` of the property to be considered.
|
|
48
|
+
* @param state - The {@link JsonEditorState | editor state} for the object being edited.
|
|
49
|
+
* @returns If the reference is successful, returns `Success` with a `JsonObject`
|
|
50
50
|
* to be flattened and merged into the current object. Returns `Failure` with detail `'inapplicable'`
|
|
51
51
|
* for non-reference keys or with detail `'error'` if an error occurs.
|
|
52
52
|
*/
|
|
53
53
|
editProperty(key: string, value: JsonValue, state: JsonEditorState): DetailedResult<JsonObject, JsonPropertyEditFailureReason>;
|
|
54
54
|
/**
|
|
55
55
|
* Evaluates a property, array or literal value for reference replacement.
|
|
56
|
-
* @param value - The
|
|
57
|
-
* @param state - The {@link
|
|
56
|
+
* @param value - The `JsonValue` of the property to be considered.
|
|
57
|
+
* @param state - The {@link JsonEditorState | editor state} for the object being edited.
|
|
58
58
|
*/
|
|
59
59
|
editValue(value: JsonValue, state: JsonEditorState): DetailedResult<JsonValue, JsonEditFailureReason>;
|
|
60
60
|
/**
|
|
61
61
|
* Gets the template variables to use given the value of some property whose name matched a
|
|
62
62
|
* resource plus the base template context.
|
|
63
|
-
* @param state - The {@link
|
|
63
|
+
* @param state - The {@link JsonEditorState | editor state} to be extended.
|
|
64
64
|
* @param supplied - The string or object supplied in the source json.
|
|
65
65
|
* @internal
|
|
66
66
|
*/
|
|
@@ -26,7 +26,7 @@ const ts_json_base_1 = require("@fgv/ts-json-base");
|
|
|
26
26
|
const ts_utils_1 = require("@fgv/ts-utils");
|
|
27
27
|
const jsonEditorRule_1 = require("../jsonEditorRule");
|
|
28
28
|
/**
|
|
29
|
-
* The {@link
|
|
29
|
+
* The {@link EditorRules.ReferenceJsonEditorRule | Reference JSON editor rule} replaces property
|
|
30
30
|
* keys or values that match some known object with a copy of that referenced object, formatted
|
|
31
31
|
* according to the current context.
|
|
32
32
|
*
|
|
@@ -48,16 +48,16 @@ const jsonEditorRule_1 = require("../jsonEditorRule");
|
|
|
48
48
|
*/
|
|
49
49
|
class ReferenceJsonEditorRule extends jsonEditorRule_1.JsonEditorRuleBase {
|
|
50
50
|
/**
|
|
51
|
-
* Creates a new {@link
|
|
52
|
-
* @param options - Optional {@link
|
|
51
|
+
* Creates a new {@link EditorRules.ReferenceJsonEditorRule | ReferenceJsonEditorRule}.
|
|
52
|
+
* @param options - Optional {@link IJsonEditorOptions | configuration options} for this rule.
|
|
53
53
|
*/
|
|
54
54
|
constructor(options) {
|
|
55
55
|
super();
|
|
56
56
|
this._options = options;
|
|
57
57
|
}
|
|
58
58
|
/**
|
|
59
|
-
* Creates a new {@link
|
|
60
|
-
* @param options - Optional {@link
|
|
59
|
+
* Creates a new {@link EditorRules.ReferenceJsonEditorRule | ReferenceJsonEditorRule}.
|
|
60
|
+
* @param options - Optional {@link IJsonEditorOptions | configuration options} for this rule.
|
|
61
61
|
*/
|
|
62
62
|
static create(options) {
|
|
63
63
|
return (0, ts_utils_1.captureResult)(() => new ReferenceJsonEditorRule(options));
|
|
@@ -65,9 +65,9 @@ class ReferenceJsonEditorRule extends jsonEditorRule_1.JsonEditorRuleBase {
|
|
|
65
65
|
/**
|
|
66
66
|
* Evaluates a property for reference expansion.
|
|
67
67
|
* @param key - The key of the property to be considered.
|
|
68
|
-
* @param value - The
|
|
69
|
-
* @param state - The {@link
|
|
70
|
-
* @returns If the reference is successful, returns `Success` with a
|
|
68
|
+
* @param value - The `JsonValue` of the property to be considered.
|
|
69
|
+
* @param state - The {@link JsonEditorState | editor state} for the object being edited.
|
|
70
|
+
* @returns If the reference is successful, returns `Success` with a `JsonObject`
|
|
71
71
|
* to be flattened and merged into the current object. Returns `Failure` with detail `'inapplicable'`
|
|
72
72
|
* for non-reference keys or with detail `'error'` if an error occurs.
|
|
73
73
|
*/
|
|
@@ -116,8 +116,8 @@ class ReferenceJsonEditorRule extends jsonEditorRule_1.JsonEditorRuleBase {
|
|
|
116
116
|
}
|
|
117
117
|
/**
|
|
118
118
|
* Evaluates a property, array or literal value for reference replacement.
|
|
119
|
-
* @param value - The
|
|
120
|
-
* @param state - The {@link
|
|
119
|
+
* @param value - The `JsonValue` of the property to be considered.
|
|
120
|
+
* @param state - The {@link JsonEditorState | editor state} for the object being edited.
|
|
121
121
|
*/
|
|
122
122
|
editValue(value, state) {
|
|
123
123
|
var _a, _b, _c;
|
|
@@ -139,7 +139,7 @@ class ReferenceJsonEditorRule extends jsonEditorRule_1.JsonEditorRuleBase {
|
|
|
139
139
|
/**
|
|
140
140
|
* Gets the template variables to use given the value of some property whose name matched a
|
|
141
141
|
* resource plus the base template context.
|
|
142
|
-
* @param state - The {@link
|
|
142
|
+
* @param state - The {@link JsonEditorState | editor state} to be extended.
|
|
143
143
|
* @param supplied - The string or object supplied in the source json.
|
|
144
144
|
* @internal
|
|
145
145
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"references.js","sourceRoot":"","sources":["../../../../src/packlets/editor/rules/references.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;;;AAEH,oDAAwF;AACxF,4CAOuB;AAGvB,sDAAuD;AAGvD;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,MAAa,uBAAwB,SAAQ,mCAAkB;IAO7D;;;OAGG;IACH,YAAmB,OAA4B;QAC7C,KAAK,EAAE,CAAC;QACR,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAC;IAC1B,CAAC;IAED;;;OAGG;IACI,MAAM,CAAC,MAAM,CAAC,OAA4B;QAC/C,OAAO,IAAA,wBAAa,EAAC,GAAG,EAAE,CAAC,IAAI,uBAAuB,CAAC,OAAO,CAAC,CAAC,CAAC;IACnE,CAAC;IAED;;;;;;;;OAQG;IACI,YAAY,CACjB,GAAW,EACX,KAAgB,EAChB,KAAsB;;QAEtB,sBAAsB;QACtB,MAAM,UAAU,GAAG,MAAA,IAAI,CAAC,QAAQ,0CAAE,UAAU,CAAC;QAC7C,MAAM,IAAI,GAAG,KAAK,CAAC,OAAO,CAAC,MAAA,IAAI,CAAC,QAAQ,0CAAE,OAAO,CAAC,CAAC;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AAK,CAAC,cAAc,CAAC,sBAAsB,EAAE,MAAM,CAAC,OAAO,EAAE,MAAA,IAAI,CAAC,QAAQ,0CAAE,UAAU,CAAC,CAAC;YACjG,CAAC;QACH,CAAC;QACD,OAAO,IAAA,yBAAc,EAAC,cAAc,EAAE,cAAc,CAAC,CAAC;IACxD,CAAC;IAED;;;;;;OAMG;IACO,cAAc,CAAC,KAAsB,EAAE,QAAmB;;QAClE,MAAM,GAAG,GAA4B,EAAE,CAAC;QACxC,IAAI,IAAA,2BAAY,EAAC,QAAQ,CAAC,EAAE,CAAC;YAC3B,GAAG,CAAC,IAAI,GAAG,MAAM,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC;QACtC,CAAC;aAAM,IAAI,OAAO,QAAQ,KAAK,QAAQ,EAAE,CAAC;YACxC,OAAO,IAAA,eAAI,EAAC,sCAAsC,IAAI,CAAC,SAAS,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC;QACjF,CAAC;QACD,OAAO,KAAK,CAAC,aAAa,CAAC,MAAA,IAAI,CAAC,QAAQ,0CAAE,OAAO,EAAE,GAAG,CAAC,CAAC;IAC1D,CAAC;CACF;AAvHD,0DAuHC","sourcesContent":["/*\n * Copyright (c) 2020 Erik Fortune\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in all\n * copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * SOFTWARE.\n */\n\nimport { JsonObject, JsonValue, isJsonObject, pickJsonObject } from '@fgv/ts-json-base';\nimport {\n DetailedResult,\n Result,\n captureResult,\n fail,\n failWithDetail,\n succeedWithDetail\n} from '@fgv/ts-utils';\nimport { IJsonContext } from '../../context';\nimport { IJsonEditorOptions, JsonEditFailureReason, JsonPropertyEditFailureReason } from '../common';\nimport { JsonEditorRuleBase } from '../jsonEditorRule';\nimport { JsonEditorState } from '../jsonEditorState';\n\n/**\n * The {@link Editor.Rules.ReferenceJsonEditorRule | Reference JSON editor rule} replaces property\n * keys or values that match some known object with a copy of that referenced object, formatted\n * according to the current context.\n *\n * A property key is matched if it matches any known referenced value.\n * - If the value of the matched key is `'default'`, then the entire object is formatted\n * with the current context, flattened and merged into the current object.\n * - If the value of the matched key is some other string, then the entire\n * object is formatted with the current context, and the child of the resulting\n * object at the specified path is flattened and merged into the current object.\n * - If the value of the matched key is an object, then the entire object is\n * formatted with the current context extended to include any properties of\n * that object, flattened, and merged into the current object.\n * - It is an error if the referenced value is not an object.\n *\n * Any property, array or literal value is matched if it matches any known\n * value reference. The referenced value is replaced by the referenced\n * value, formatted using the current editor context.\n * @public\n */\nexport class ReferenceJsonEditorRule extends JsonEditorRuleBase {\n /**\n * Stored fully-resolved {@link Editor.IJsonEditorOptions | editor options} for this rule.\n * @public\n */\n protected _options?: IJsonEditorOptions;\n\n /**\n * Creates a new {@link Editor.Rules.ReferenceJsonEditorRule | ReferenceJsonEditorRule}.\n * @param options - Optional {@link Editor.IJsonEditorOptions | configuration options} for this rule.\n */\n public constructor(options?: IJsonEditorOptions) {\n super();\n this._options = options;\n }\n\n /**\n * Creates a new {@link Editor.Rules.ReferenceJsonEditorRule | ReferenceJsonEditorRule}.\n * @param options - Optional {@link Editor.IJsonEditorOptions | configuration options} for this rule.\n */\n public static create(options?: IJsonEditorOptions): Result<ReferenceJsonEditorRule> {\n return captureResult(() => new ReferenceJsonEditorRule(options));\n }\n\n /**\n * Evaluates a property for reference expansion.\n * @param key - The key of the property to be considered.\n * @param value - The {@link JsonValue | value} of the property to be considered.\n * @param state - The {@link Editor.JsonEditorState | editor state} for the object being edited.\n * @returns If the reference is successful, returns `Success` with a {@link JsonObject | JsonObject}\n * to be flattened and merged into the current object. Returns `Failure` with detail `'inapplicable'`\n * for non-reference keys or with detail `'error'` if an error occurs.\n */\n public editProperty(\n key: string,\n value: JsonValue,\n state: JsonEditorState\n ): DetailedResult<JsonObject, JsonPropertyEditFailureReason> {\n /* c8 ignore next 2 */\n const validation = this._options?.validation;\n const refs = state.getRefs(this._options?.context);\n if (refs?.has(key)) {\n // need to apply any rules to the value before we evaluate it\n const cloneResult = state.editor.clone(value, state.context);\n if (cloneResult.isSuccess()) {\n value = cloneResult.value;\n } else {\n const message = `${key}: ${cloneResult.message}`;\n return state.failValidation('invalidPropertyName', message, validation);\n }\n\n const contextResult = this._extendContext(state, value);\n if (contextResult.isSuccess()) {\n const objResult = refs.getJsonObject(key, contextResult.value);\n // guarded by the has above so should never happen\n /* c8 ignore else */\n if (objResult.isSuccess()) {\n if (typeof value !== 'string' || value === 'default') {\n return succeedWithDetail<JsonObject, JsonEditFailureReason>(objResult.value, 'edited');\n }\n const pickResult = pickJsonObject(objResult.value, value);\n if (pickResult.isFailure()) {\n const message = `${key}: ${pickResult.message}`;\n return state.failValidation('invalidPropertyName', message, validation);\n }\n return pickResult.withDetail('edited');\n } else if (objResult.detail !== 'unknown') {\n const message = `${key}: ${objResult.message}`;\n return state.failValidation('invalidPropertyName', message, validation);\n }\n } else {\n const message = `${key}: ${contextResult.message}`;\n return state.failValidation('invalidPropertyName', message, validation);\n }\n }\n return failWithDetail('inapplicable', 'inapplicable');\n }\n\n /**\n * Evaluates a property, array or literal value for reference replacement.\n * @param value - The {@link JsonValue | value} of the property to be considered.\n * @param state - The {@link Editor.JsonEditorState | editor state} for the object being edited.\n */\n public editValue(\n value: JsonValue,\n state: JsonEditorState\n ): DetailedResult<JsonValue, JsonEditFailureReason> {\n /* c8 ignore next */\n const refs = state.getRefs(this._options?.context);\n\n if (refs && typeof value === 'string') {\n /* c8 ignore next */\n const context = state.getContext(this._options?.context);\n const result = refs.getJsonValue(value, context);\n if (result.isSuccess()) {\n return succeedWithDetail(result.value, 'edited');\n } else if (result.detail === 'error') {\n return state.failValidation('invalidPropertyValue', result.message, this._options?.validation);\n }\n }\n return failWithDetail('inapplicable', 'inapplicable');\n }\n\n /**\n * Gets the template variables to use given the value of some property whose name matched a\n * resource plus the base template context.\n * @param state - The {@link Editor.JsonEditorState | editor state} to be extended.\n * @param supplied - The string or object supplied in the source json.\n * @internal\n */\n protected _extendContext(state: JsonEditorState, supplied: JsonValue): Result<IJsonContext | undefined> {\n const add: Record<string, unknown> = {};\n if (isJsonObject(supplied)) {\n add.vars = Object.entries(supplied);\n } else if (typeof supplied !== 'string') {\n return fail(`Invalid template path or context: \"${JSON.stringify(supplied)}\"`);\n }\n return state.extendContext(this._options?.context, add);\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"references.js","sourceRoot":"","sources":["../../../../src/packlets/editor/rules/references.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;;;AAEH,oDAAwF;AACxF,4CAOuB;AAGvB,sDAAuD;AAGvD;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,MAAa,uBAAwB,SAAQ,mCAAkB;IAO7D;;;OAGG;IACH,YAAmB,OAA4B;QAC7C,KAAK,EAAE,CAAC;QACR,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAC;IAC1B,CAAC;IAED;;;OAGG;IACI,MAAM,CAAC,MAAM,CAAC,OAA4B;QAC/C,OAAO,IAAA,wBAAa,EAAC,GAAG,EAAE,CAAC,IAAI,uBAAuB,CA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,CAAC;YACzD,MAAM,MAAM,GAAG,IAAI,CAAC,YAAY,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;YACjD,IAAI,MAAM,CAAC,SAAS,EAAE,EAAE,CAAC;gBACvB,OAAO,IAAA,4BAAiB,EAAC,MAAM,CAAC,KAAK,EAAE,QAAQ,CAAC,CAAC;YACnD,CAAC;iBAAM,IAAI,MAAM,CAAC,MAAM,KAAK,OAAO,EAAE,CAAC;gBACrC,OAAO,KAAK,CAAC,cAAc,CAAC,sBAAsB,EAAE,MAAM,CAAC,OAAO,EAAE,MAAA,IAAI,CAAC,QAAQ,0CAAE,UAAU,CAAC,CAAC;YACjG,CAAC;QACH,CAAC;QACD,OAAO,IAAA,yBAAc,EAAC,cAAc,EAAE,cAAc,CAAC,CAAC;IACxD,CAAC;IAED;;;;;;OAMG;IACO,cAAc,CAAC,KAAsB,EAAE,QAAmB;;QAClE,MAAM,GAAG,GAA4B,EAAE,CAAC;QACxC,IAAI,IAAA,2BAAY,EAAC,QAAQ,CAAC,EAAE,CAAC;YAC3B,GAAG,CAAC,IAAI,GAAG,MAAM,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC;QACtC,CAAC;aAAM,IAAI,OAAO,QAAQ,KAAK,QAAQ,EAAE,CAAC;YACxC,OAAO,IAAA,eAAI,EAAC,sCAAsC,IAAI,CAAC,SAAS,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC;QACjF,CAAC;QACD,OAAO,KAAK,CAAC,aAAa,CAAC,MAAA,IAAI,CAAC,QAAQ,0CAAE,OAAO,EAAE,GAAG,CAAC,CAAC;IAC1D,CAAC;CACF;AAvHD,0DAuHC","sourcesContent":["/*\n * Copyright (c) 2020 Erik Fortune\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in all\n * copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * SOFTWARE.\n */\n\nimport { JsonObject, JsonValue, isJsonObject, pickJsonObject } from '@fgv/ts-json-base';\nimport {\n DetailedResult,\n Result,\n captureResult,\n fail,\n failWithDetail,\n succeedWithDetail\n} from '@fgv/ts-utils';\nimport { IJsonContext } from '../../context';\nimport { IJsonEditorOptions, JsonEditFailureReason, JsonPropertyEditFailureReason } from '../common';\nimport { JsonEditorRuleBase } from '../jsonEditorRule';\nimport { JsonEditorState } from '../jsonEditorState';\n\n/**\n * The {@link EditorRules.ReferenceJsonEditorRule | Reference JSON editor rule} replaces property\n * keys or values that match some known object with a copy of that referenced object, formatted\n * according to the current context.\n *\n * A property key is matched if it matches any known referenced value.\n * - If the value of the matched key is `'default'`, then the entire object is formatted\n * with the current context, flattened and merged into the current object.\n * - If the value of the matched key is some other string, then the entire\n * object is formatted with the current context, and the child of the resulting\n * object at the specified path is flattened and merged into the current object.\n * - If the value of the matched key is an object, then the entire object is\n * formatted with the current context extended to include any properties of\n * that object, flattened, and merged into the current object.\n * - It is an error if the referenced value is not an object.\n *\n * Any property, array or literal value is matched if it matches any known\n * value reference. The referenced value is replaced by the referenced\n * value, formatted using the current editor context.\n * @public\n */\nexport class ReferenceJsonEditorRule extends JsonEditorRuleBase {\n /**\n * Stored fully-resolved {@link IJsonEditorOptions | editor options} for this rule.\n * @public\n */\n protected _options?: IJsonEditorOptions;\n\n /**\n * Creates a new {@link EditorRules.ReferenceJsonEditorRule | ReferenceJsonEditorRule}.\n * @param options - Optional {@link IJsonEditorOptions | configuration options} for this rule.\n */\n public constructor(options?: IJsonEditorOptions) {\n super();\n this._options = options;\n }\n\n /**\n * Creates a new {@link EditorRules.ReferenceJsonEditorRule | ReferenceJsonEditorRule}.\n * @param options - Optional {@link IJsonEditorOptions | configuration options} for this rule.\n */\n public static create(options?: IJsonEditorOptions): Result<ReferenceJsonEditorRule> {\n return captureResult(() => new ReferenceJsonEditorRule(options));\n }\n\n /**\n * Evaluates a property for reference expansion.\n * @param key - The key of the property to be considered.\n * @param value - The `JsonValue` of the property to be considered.\n * @param state - The {@link JsonEditorState | editor state} for the object being edited.\n * @returns If the reference is successful, returns `Success` with a `JsonObject`\n * to be flattened and merged into the current object. Returns `Failure` with detail `'inapplicable'`\n * for non-reference keys or with detail `'error'` if an error occurs.\n */\n public editProperty(\n key: string,\n value: JsonValue,\n state: JsonEditorState\n ): DetailedResult<JsonObject, JsonPropertyEditFailureReason> {\n /* c8 ignore next 2 */\n const validation = this._options?.validation;\n const refs = state.getRefs(this._options?.context);\n if (refs?.has(key)) {\n // need to apply any rules to the value before we evaluate it\n const cloneResult = state.editor.clone(value, state.context);\n if (cloneResult.isSuccess()) {\n value = cloneResult.value;\n } else {\n const message = `${key}: ${cloneResult.message}`;\n return state.failValidation('invalidPropertyName', message, validation);\n }\n\n const contextResult = this._extendContext(state, value);\n if (contextResult.isSuccess()) {\n const objResult = refs.getJsonObject(key, contextResult.value);\n // guarded by the has above so should never happen\n /* c8 ignore else */\n if (objResult.isSuccess()) {\n if (typeof value !== 'string' || value === 'default') {\n return succeedWithDetail<JsonObject, JsonEditFailureReason>(objResult.value, 'edited');\n }\n const pickResult = pickJsonObject(objResult.value, value);\n if (pickResult.isFailure()) {\n const message = `${key}: ${pickResult.message}`;\n return state.failValidation('invalidPropertyName', message, validation);\n }\n return pickResult.withDetail('edited');\n } else if (objResult.detail !== 'unknown') {\n const message = `${key}: ${objResult.message}`;\n return state.failValidation('invalidPropertyName', message, validation);\n }\n } else {\n const message = `${key}: ${contextResult.message}`;\n return state.failValidation('invalidPropertyName', message, validation);\n }\n }\n return failWithDetail('inapplicable', 'inapplicable');\n }\n\n /**\n * Evaluates a property, array or literal value for reference replacement.\n * @param value - The `JsonValue` of the property to be considered.\n * @param state - The {@link JsonEditorState | editor state} for the object being edited.\n */\n public editValue(\n value: JsonValue,\n state: JsonEditorState\n ): DetailedResult<JsonValue, JsonEditFailureReason> {\n /* c8 ignore next */\n const refs = state.getRefs(this._options?.context);\n\n if (refs && typeof value === 'string') {\n /* c8 ignore next */\n const context = state.getContext(this._options?.context);\n const result = refs.getJsonValue(value, context);\n if (result.isSuccess()) {\n return succeedWithDetail(result.value, 'edited');\n } else if (result.detail === 'error') {\n return state.failValidation('invalidPropertyValue', result.message, this._options?.validation);\n }\n }\n return failWithDetail('inapplicable', 'inapplicable');\n }\n\n /**\n * Gets the template variables to use given the value of some property whose name matched a\n * resource plus the base template context.\n * @param state - The {@link JsonEditorState | editor state} to be extended.\n * @param supplied - The string or object supplied in the source json.\n * @internal\n */\n protected _extendContext(state: JsonEditorState, supplied: JsonValue): Result<IJsonContext | undefined> {\n const add: Record<string, unknown> = {};\n if (isJsonObject(supplied)) {\n add.vars = Object.entries(supplied);\n } else if (typeof supplied !== 'string') {\n return fail(`Invalid template path or context: \"${JSON.stringify(supplied)}\"`);\n }\n return state.extendContext(this._options?.context, add);\n }\n}\n"]}
|