-
Notifications
You must be signed in to change notification settings - Fork 391
/
Copy pathcombinators.ts
156 lines (136 loc) · 5.26 KB
/
combinators.ts
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
The MIT License
Copyright (c) 2017-2019 EclipseSource Munich
https://github.com/eclipsesource/jsonforms
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
import type { ControlElement, JsonSchema, UISchemaElement } from '../models';
import { findUISchema } from '../reducers';
import { JsonFormsUISchemaRegistryEntry } from '../store';
import { Resolve } from '../util/util';
export interface CombinatorSubSchemaRenderInfo {
schema: JsonSchema;
uischema: UISchemaElement;
label: string;
}
export type CombinatorKeyword = 'anyOf' | 'oneOf' | 'allOf';
/** Custom schema keyword to define the property identifying different combinator schemas. */
export const COMBINATOR_TYPE_PROPERTY = 'x-jsf-type-property';
/** Default properties that are used to identify combinator schemas. */
export const COMBINATOR_IDENTIFICATION_PROPERTIES = ['type', 'kind'];
export const createCombinatorRenderInfos = (
combinatorSubSchemas: JsonSchema[],
rootSchema: JsonSchema,
keyword: CombinatorKeyword,
control: ControlElement,
path: string,
uischemas: JsonFormsUISchemaRegistryEntry[]
): CombinatorSubSchemaRenderInfo[] =>
combinatorSubSchemas.map((subSchema, subSchemaIndex) => {
const resolvedSubSchema =
subSchema.$ref && Resolve.schema(rootSchema, subSchema.$ref, rootSchema);
const schema = resolvedSubSchema ?? subSchema;
return {
schema,
uischema: findUISchema(
uischemas,
schema,
control.scope,
path,
undefined,
control,
rootSchema
),
label:
subSchema.title ??
resolvedSubSchema?.title ??
`${keyword}-${subSchemaIndex}`,
};
});
/**
* Returns the index of the schema in the given combinator keyword that matches the identification property of the given data object.
* The heuristic only works for data objects with a corresponding schema. If the data is a primitive value or an array, the heuristic does not work.
*
* The following heuristics are applied:
* If the schema defines a `x-jsf-type-property`, it is used as the identification property.
* Otherwise, the first of the following properties is used if it exists in at least one combinator schema and has a `const` entry:
* - `type`
* - `kind`
*
* If the index cannot be determined, `-1` is returned.
*
* @returns the index of the fitting schema or `-1` if no fitting schema was found
*/
export const getCombinatorIndexOfFittingSchema = (
data: any,
keyword: CombinatorKeyword,
schema: JsonSchema,
rootSchema: JsonSchema
): number => {
if (typeof data !== 'object' || data === null) {
return -1;
}
// Resolve all schemas in the combinator.
const resolvedCombinatorSchemas = [];
for (let i = 0; i < schema[keyword]?.length; i++) {
let resolvedSchema = schema[keyword][i];
if (resolvedSchema.$ref) {
resolvedSchema = Resolve.schema(
rootSchema,
resolvedSchema.$ref,
rootSchema
);
}
resolvedCombinatorSchemas.push(resolvedSchema);
}
// Determine the identification property
let idProperty: string | undefined;
if (
COMBINATOR_TYPE_PROPERTY in schema &&
typeof schema[COMBINATOR_TYPE_PROPERTY] === 'string'
) {
idProperty = schema[COMBINATOR_TYPE_PROPERTY];
} else {
// Use the first default identification property that has a const entry in at least one of the schemas
for (const potentialIdProp of COMBINATOR_IDENTIFICATION_PROPERTIES) {
for (const resolvedSchema of resolvedCombinatorSchemas) {
if (resolvedSchema.properties?.[potentialIdProp]?.const !== undefined) {
idProperty = potentialIdProp;
break;
}
}
}
}
let indexOfFittingSchema = -1;
if (idProperty === undefined) {
return indexOfFittingSchema;
}
// Check if the data matches the identification property of one of the resolved schemas
for (let i = 0; i < resolvedCombinatorSchemas.length; i++) {
const resolvedSchema = resolvedCombinatorSchemas[i];
// Match the identification property against a constant value in resolvedSchema
const maybeConstIdValue = resolvedSchema.properties?.[idProperty]?.const;
if (
maybeConstIdValue !== undefined &&
data[idProperty] === maybeConstIdValue
) {
indexOfFittingSchema = i;
break;
}
}
return indexOfFittingSchema;
};