@ngx-formly/core 6.3.11 → 7.0.0-alpha.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/README.md +1 -0
- package/esm2022/json-schema/formly-json-schema.service.mjs +628 -0
- package/esm2022/lib/components/formly.field.mjs +292 -0
- package/esm2022/lib/components/formly.form.mjs +164 -0
- package/{esm2020 → esm2022}/lib/components/formly.template.mjs +7 -7
- package/esm2022/lib/core.config.mjs +36 -0
- package/esm2022/lib/core.mjs +20 -0
- package/esm2022/lib/core.module.mjs +74 -0
- package/esm2022/lib/extensions/core/core.mjs +148 -0
- package/esm2022/lib/extensions/field-expression/field-expression.mjs +250 -0
- package/esm2022/lib/extensions/field-expression/utils.mjs +17 -0
- package/esm2022/lib/extensions/field-form/field-form.mjs +141 -0
- package/esm2022/lib/extensions/field-form/utils.mjs +100 -0
- package/esm2022/lib/extensions/index.mjs +2 -0
- package/esm2022/lib/models/fieldconfig.cache.mjs +2 -0
- package/esm2022/lib/models/fieldconfig.mjs +2 -0
- package/esm2022/lib/services/formly.builder.mjs +100 -0
- package/esm2022/lib/services/formly.config.mjs +229 -0
- package/esm2022/lib/templates/field-array.type.mjs +84 -0
- package/{esm2020 → esm2022}/lib/templates/field-template.type.mjs +9 -9
- package/esm2022/lib/templates/field.type.mjs +57 -0
- package/{esm2020 → esm2022}/lib/templates/field.wrapper.mjs +4 -4
- package/esm2022/lib/templates/formly.attributes.mjs +187 -0
- package/{esm2020 → esm2022}/lib/templates/formly.group.mjs +8 -8
- package/{esm2020 → esm2022}/lib/templates/formly.validation-message.mjs +4 -4
- package/esm2022/lib/utils.mjs +312 -0
- package/esm2022/preset/preset.config.mjs +11 -0
- package/esm2022/preset/preset.module.mjs +18 -0
- package/{esm2020 → esm2022}/select/select-options.pipe.mjs +4 -4
- package/{esm2020 → esm2022}/select/select.module.mjs +5 -5
- package/{esm2020 → esm2022}/testing/component-factory.mjs +5 -4
- package/{fesm2020 → fesm2022}/ngx-formly-core-json-schema.mjs +6 -6
- package/fesm2022/ngx-formly-core-json-schema.mjs.map +1 -0
- package/{fesm2020 → fesm2022}/ngx-formly-core-preset.mjs +14 -19
- package/fesm2022/ngx-formly-core-preset.mjs.map +1 -0
- package/{fesm2020 → fesm2022}/ngx-formly-core-select.mjs +7 -7
- package/{fesm2020 → fesm2022}/ngx-formly-core-select.mjs.map +1 -1
- package/{fesm2020 → fesm2022}/ngx-formly-core-testing.mjs +4 -3
- package/fesm2022/ngx-formly-core-testing.mjs.map +1 -0
- package/{fesm2020 → fesm2022}/ngx-formly-core.mjs +1750 -1705
- package/fesm2022/ngx-formly-core.mjs.map +1 -0
- package/lib/components/formly.field.d.ts +5 -1
- package/lib/components/formly.form.d.ts +8 -4
- package/lib/components/formly.template.d.ts +1 -1
- package/lib/core.config.d.ts +10 -0
- package/lib/core.d.ts +4 -3
- package/lib/core.module.d.ts +2 -5
- package/lib/extensions/field-expression/utils.d.ts +1 -1
- package/lib/extensions/index.d.ts +1 -0
- package/lib/models/config.d.ts +1 -1
- package/lib/models/fieldconfig.cache.d.ts +3 -3
- package/lib/models/fieldconfig.d.ts +8 -7
- package/lib/services/formly.builder.d.ts +5 -5
- package/lib/services/formly.config.d.ts +2 -6
- package/lib/templates/field-array.type.d.ts +3 -3
- package/lib/templates/field-template.type.d.ts +1 -1
- package/lib/templates/field.type.d.ts +5 -5
- package/lib/templates/field.wrapper.d.ts +1 -1
- package/lib/templates/formly.attributes.d.ts +1 -1
- package/lib/templates/formly.group.d.ts +1 -1
- package/lib/templates/formly.validation-message.d.ts +1 -1
- package/lib/utils.d.ts +3 -3
- package/package.json +23 -37
- package/preset/preset.config.d.ts +2 -0
- package/select/select-options.pipe.d.ts +1 -1
- package/esm2020/json-schema/formly-json-schema.service.mjs +0 -628
- package/esm2020/lib/components/formly.field.mjs +0 -284
- package/esm2020/lib/components/formly.form.mjs +0 -147
- package/esm2020/lib/core.mjs +0 -19
- package/esm2020/lib/core.module.mjs +0 -91
- package/esm2020/lib/extensions/core/core.mjs +0 -148
- package/esm2020/lib/extensions/field-expression/field-expression.mjs +0 -250
- package/esm2020/lib/extensions/field-expression/utils.mjs +0 -17
- package/esm2020/lib/extensions/field-form/field-form.mjs +0 -141
- package/esm2020/lib/extensions/field-form/utils.mjs +0 -100
- package/esm2020/lib/models/fieldconfig.cache.mjs +0 -2
- package/esm2020/lib/models/fieldconfig.mjs +0 -2
- package/esm2020/lib/services/formly.builder.mjs +0 -93
- package/esm2020/lib/services/formly.config.mjs +0 -229
- package/esm2020/lib/templates/field-array.type.mjs +0 -84
- package/esm2020/lib/templates/field.type.mjs +0 -57
- package/esm2020/lib/templates/formly.attributes.mjs +0 -187
- package/esm2020/lib/utils.mjs +0 -312
- package/esm2020/preset/preset.module.mjs +0 -32
- package/fesm2015/ngx-formly-core-json-schema.mjs +0 -615
- package/fesm2015/ngx-formly-core-json-schema.mjs.map +0 -1
- package/fesm2015/ngx-formly-core-preset.mjs +0 -69
- package/fesm2015/ngx-formly-core-preset.mjs.map +0 -1
- package/fesm2015/ngx-formly-core-select.mjs +0 -120
- package/fesm2015/ngx-formly-core-select.mjs.map +0 -1
- package/fesm2015/ngx-formly-core-testing.mjs +0 -87
- package/fesm2015/ngx-formly-core-testing.mjs.map +0 -1
- package/fesm2015/ngx-formly-core.mjs +0 -2417
- package/fesm2015/ngx-formly-core.mjs.map +0 -1
- package/fesm2020/ngx-formly-core-json-schema.mjs.map +0 -1
- package/fesm2020/ngx-formly-core-preset.mjs.map +0 -1
- package/fesm2020/ngx-formly-core-testing.mjs.map +0 -1
- package/fesm2020/ngx-formly-core.mjs.map +0 -1
- package/json-schema/package.json +0 -10
- package/preset/package.json +0 -10
- package/select/package.json +0 -10
- package/testing/package.json +0 -10
- /package/{esm2020 → esm2022}/json-schema/ngx-formly-core-json-schema.mjs +0 -0
- /package/{esm2020 → esm2022}/json-schema/public_api.mjs +0 -0
- /package/{esm2020 → esm2022}/lib/extensions/field-validation/field-validation.mjs +0 -0
- /package/{esm2020 → esm2022}/lib/models/config.mjs +0 -0
- /package/{esm2020 → esm2022}/lib/models/index.mjs +0 -0
- /package/{esm2020 → esm2022}/ngx-formly-core.mjs +0 -0
- /package/{esm2020 → esm2022}/preset/ngx-formly-core-preset.mjs +0 -0
- /package/{esm2020 → esm2022}/preset/preset-substitution.extension.mjs +0 -0
- /package/{esm2020 → esm2022}/preset/public_api.mjs +0 -0
- /package/{esm2020 → esm2022}/public_api.mjs +0 -0
- /package/{esm2020 → esm2022}/select/ngx-formly-core-select.mjs +0 -0
- /package/{esm2020 → esm2022}/select/public_api.mjs +0 -0
- /package/{esm2020 → esm2022}/testing/ngx-formly-core-testing.mjs +0 -0
- /package/{esm2020 → esm2022}/testing/public_api.mjs +0 -0
- /package/{ngx-formly-core.d.ts → index.d.ts} +0 -0
- /package/json-schema/{ngx-formly-core-json-schema.d.ts → index.d.ts} +0 -0
- /package/preset/{ngx-formly-core-preset.d.ts → index.d.ts} +0 -0
- /package/select/{ngx-formly-core-select.d.ts → index.d.ts} +0 -0
- /package/testing/{ngx-formly-core-testing.d.ts → index.d.ts} +0 -0
|
@@ -1,615 +0,0 @@
|
|
|
1
|
-
import { __rest } from 'tslib';
|
|
2
|
-
import * as i0 from '@angular/core';
|
|
3
|
-
import { Injectable } from '@angular/core';
|
|
4
|
-
import { FormArray, FormGroup } from '@angular/forms';
|
|
5
|
-
import { ɵhasKey, ɵgetFieldValue, ɵclone, ɵreverseDeepMerge } from '@ngx-formly/core';
|
|
6
|
-
import { tap } from 'rxjs/operators';
|
|
7
|
-
|
|
8
|
-
// https://stackoverflow.com/a/27865285
|
|
9
|
-
function decimalPlaces(a) {
|
|
10
|
-
if (!isFinite(a)) {
|
|
11
|
-
return 0;
|
|
12
|
-
}
|
|
13
|
-
let e = 1, p = 0;
|
|
14
|
-
while (Math.round(a * e) / e !== a) {
|
|
15
|
-
e *= 10;
|
|
16
|
-
p++;
|
|
17
|
-
}
|
|
18
|
-
return p;
|
|
19
|
-
}
|
|
20
|
-
function isEmpty(v) {
|
|
21
|
-
return v === '' || v == null;
|
|
22
|
-
}
|
|
23
|
-
function isObject(v) {
|
|
24
|
-
return v != null && typeof v === 'object' && !Array.isArray(v);
|
|
25
|
-
}
|
|
26
|
-
function isInteger(value) {
|
|
27
|
-
return Number.isInteger ? Number.isInteger(value) : typeof value === 'number' && Math.floor(value) === value;
|
|
28
|
-
}
|
|
29
|
-
function isConst(schema) {
|
|
30
|
-
return typeof schema === 'object' && (schema.hasOwnProperty('const') || (schema.enum && schema.enum.length === 1));
|
|
31
|
-
}
|
|
32
|
-
function toNumber(value) {
|
|
33
|
-
if (value === '' || value === undefined) {
|
|
34
|
-
return undefined;
|
|
35
|
-
}
|
|
36
|
-
if (value === null) {
|
|
37
|
-
return null;
|
|
38
|
-
}
|
|
39
|
-
if (typeof value === 'number') {
|
|
40
|
-
return value;
|
|
41
|
-
}
|
|
42
|
-
const val = parseFloat(value);
|
|
43
|
-
return !isNaN(val) ? val : value;
|
|
44
|
-
}
|
|
45
|
-
function totalMatchedFields(field) {
|
|
46
|
-
if (!field.fieldGroup) {
|
|
47
|
-
return ɵhasKey(field) && ɵgetFieldValue(field) !== undefined ? 1 : 0;
|
|
48
|
-
}
|
|
49
|
-
const total = field.fieldGroup.reduce((s, f) => totalMatchedFields(f) + s, 0);
|
|
50
|
-
if (total === 0 && ɵhasKey(field)) {
|
|
51
|
-
const value = ɵgetFieldValue(field);
|
|
52
|
-
if (value === null ||
|
|
53
|
-
(value !== undefined && ((field.fieldArray && Array.isArray(value)) || (!field.fieldArray && isObject(value))))) {
|
|
54
|
-
return 1;
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
return total;
|
|
58
|
-
}
|
|
59
|
-
class FormlyJsonschema {
|
|
60
|
-
toFieldConfig(schema, options) {
|
|
61
|
-
schema = ɵclone(schema);
|
|
62
|
-
return this._toFieldConfig(schema, Object.assign({ schema }, (options || {})));
|
|
63
|
-
}
|
|
64
|
-
_toFieldConfig(schema, _a) {
|
|
65
|
-
var _b;
|
|
66
|
-
var { key, isOptional } = _a, options = __rest(_a, ["key", "isOptional"]);
|
|
67
|
-
schema = this.resolveSchema(schema, options);
|
|
68
|
-
const types = this.guessSchemaType(schema);
|
|
69
|
-
let field = {
|
|
70
|
-
type: types[0],
|
|
71
|
-
defaultValue: schema.default,
|
|
72
|
-
props: {
|
|
73
|
-
label: schema.title,
|
|
74
|
-
readonly: schema.readOnly,
|
|
75
|
-
description: schema.description,
|
|
76
|
-
},
|
|
77
|
-
};
|
|
78
|
-
if (key != null) {
|
|
79
|
-
field.key = key;
|
|
80
|
-
}
|
|
81
|
-
if (!options.ignoreDefault && (schema.readOnly || options.readOnly)) {
|
|
82
|
-
field.props.disabled = true;
|
|
83
|
-
options = Object.assign(Object.assign({}, options), { readOnly: true });
|
|
84
|
-
}
|
|
85
|
-
if (options.resetOnHide) {
|
|
86
|
-
field.resetOnHide = true;
|
|
87
|
-
}
|
|
88
|
-
if (options.shareFormControl === false) {
|
|
89
|
-
field.shareFormControl = false;
|
|
90
|
-
}
|
|
91
|
-
if (field.defaultValue === undefined && types.length === 1 && isOptional === false) {
|
|
92
|
-
switch (types[0]) {
|
|
93
|
-
case 'null': {
|
|
94
|
-
field.defaultValue = null;
|
|
95
|
-
break;
|
|
96
|
-
}
|
|
97
|
-
case 'string': {
|
|
98
|
-
field.defaultValue = '';
|
|
99
|
-
break;
|
|
100
|
-
}
|
|
101
|
-
case 'object': {
|
|
102
|
-
field.defaultValue = {};
|
|
103
|
-
break;
|
|
104
|
-
}
|
|
105
|
-
case 'array': {
|
|
106
|
-
field.defaultValue = schema.minItems > 0 ? Array.from(new Array(schema.minItems)) : [];
|
|
107
|
-
break;
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
if (options.ignoreDefault) {
|
|
112
|
-
delete field.defaultValue;
|
|
113
|
-
}
|
|
114
|
-
this.addValidator(field, 'type', {
|
|
115
|
-
schemaType: types,
|
|
116
|
-
expression: ({ value }) => {
|
|
117
|
-
if (value === undefined) {
|
|
118
|
-
return true;
|
|
119
|
-
}
|
|
120
|
-
if (value === null && types.indexOf('null') !== -1) {
|
|
121
|
-
return true;
|
|
122
|
-
}
|
|
123
|
-
switch (types[0]) {
|
|
124
|
-
case 'null': {
|
|
125
|
-
return typeof value === null;
|
|
126
|
-
}
|
|
127
|
-
case 'string': {
|
|
128
|
-
return typeof value === 'string';
|
|
129
|
-
}
|
|
130
|
-
case 'integer': {
|
|
131
|
-
return isInteger(value);
|
|
132
|
-
}
|
|
133
|
-
case 'number': {
|
|
134
|
-
return typeof value === 'number';
|
|
135
|
-
}
|
|
136
|
-
case 'object': {
|
|
137
|
-
return isObject(value);
|
|
138
|
-
}
|
|
139
|
-
case 'array': {
|
|
140
|
-
return Array.isArray(value);
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
return true;
|
|
144
|
-
},
|
|
145
|
-
});
|
|
146
|
-
switch (field.type) {
|
|
147
|
-
case 'number':
|
|
148
|
-
case 'integer': {
|
|
149
|
-
field.parsers = [
|
|
150
|
-
(v, f) => {
|
|
151
|
-
v = toNumber(v);
|
|
152
|
-
if (v === null && f) {
|
|
153
|
-
const input = typeof document !== 'undefined' && f.id
|
|
154
|
-
? document.querySelector(`#${f.id}`)
|
|
155
|
-
: undefined;
|
|
156
|
-
if (input && !input.validity.badInput) {
|
|
157
|
-
v = undefined;
|
|
158
|
-
}
|
|
159
|
-
if (v !== f.formControl.value) {
|
|
160
|
-
f.formControl.setValue(v, { emitModelToViewChange: false });
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
return v;
|
|
164
|
-
},
|
|
165
|
-
];
|
|
166
|
-
if (schema.hasOwnProperty('minimum')) {
|
|
167
|
-
field.props.min = schema.minimum;
|
|
168
|
-
}
|
|
169
|
-
if (schema.hasOwnProperty('maximum')) {
|
|
170
|
-
field.props.max = schema.maximum;
|
|
171
|
-
}
|
|
172
|
-
if (schema.hasOwnProperty('exclusiveMinimum')) {
|
|
173
|
-
field.props.exclusiveMinimum = schema.exclusiveMinimum;
|
|
174
|
-
this.addValidator(field, 'exclusiveMinimum', ({ value }) => isEmpty(value) || value > schema.exclusiveMinimum);
|
|
175
|
-
}
|
|
176
|
-
if (schema.hasOwnProperty('exclusiveMaximum')) {
|
|
177
|
-
field.props.exclusiveMaximum = schema.exclusiveMaximum;
|
|
178
|
-
this.addValidator(field, 'exclusiveMaximum', ({ value }) => isEmpty(value) || value < schema.exclusiveMaximum);
|
|
179
|
-
}
|
|
180
|
-
if (schema.hasOwnProperty('multipleOf')) {
|
|
181
|
-
field.props.step = schema.multipleOf;
|
|
182
|
-
this.addValidator(field, 'multipleOf', ({ value }) => {
|
|
183
|
-
if (isEmpty(value) || typeof value !== 'number' || value === 0 || schema.multipleOf <= 0) {
|
|
184
|
-
return true;
|
|
185
|
-
}
|
|
186
|
-
// https://github.com/ajv-validator/ajv/issues/652#issue-283610859
|
|
187
|
-
const multiplier = Math.pow(10, decimalPlaces(schema.multipleOf));
|
|
188
|
-
return Math.round(value * multiplier) % Math.round(schema.multipleOf * multiplier) === 0;
|
|
189
|
-
});
|
|
190
|
-
}
|
|
191
|
-
break;
|
|
192
|
-
}
|
|
193
|
-
case 'string': {
|
|
194
|
-
field.parsers = [
|
|
195
|
-
(v, f) => {
|
|
196
|
-
if (types.indexOf('null') !== -1) {
|
|
197
|
-
v = isEmpty(v) ? null : v;
|
|
198
|
-
}
|
|
199
|
-
else if (f && !f.props.required) {
|
|
200
|
-
v = v === '' ? undefined : v;
|
|
201
|
-
}
|
|
202
|
-
return v;
|
|
203
|
-
},
|
|
204
|
-
];
|
|
205
|
-
['minLength', 'maxLength', 'pattern'].forEach((prop) => {
|
|
206
|
-
if (schema.hasOwnProperty(prop)) {
|
|
207
|
-
field.props[prop] = schema[prop];
|
|
208
|
-
}
|
|
209
|
-
});
|
|
210
|
-
break;
|
|
211
|
-
}
|
|
212
|
-
case 'object': {
|
|
213
|
-
if (!field.fieldGroup) {
|
|
214
|
-
field.fieldGroup = [];
|
|
215
|
-
}
|
|
216
|
-
const { propDeps, schemaDeps } = this.resolveDependencies(schema);
|
|
217
|
-
Object.keys(schema.properties || {}).forEach((property) => {
|
|
218
|
-
const isRequired = Array.isArray(schema.required) && schema.required.indexOf(property) !== -1;
|
|
219
|
-
const f = this._toFieldConfig(schema.properties[property], Object.assign(Object.assign({}, options), { key: property, isOptional: isOptional || !isRequired }));
|
|
220
|
-
field.fieldGroup.push(f);
|
|
221
|
-
if (isRequired || propDeps[property]) {
|
|
222
|
-
f.expressions = Object.assign(Object.assign({}, (f.expressions || {})), { 'props.required': (f) => {
|
|
223
|
-
let parent = f.parent;
|
|
224
|
-
const model = f.fieldGroup && f.key != null ? parent.model : f.model;
|
|
225
|
-
while (parent.key == null && parent.parent) {
|
|
226
|
-
parent = parent.parent;
|
|
227
|
-
}
|
|
228
|
-
const required = parent && parent.props ? parent.props.required : false;
|
|
229
|
-
if (!model && !required) {
|
|
230
|
-
return false;
|
|
231
|
-
}
|
|
232
|
-
if (Array.isArray(schema.required) && schema.required.indexOf(property) !== -1) {
|
|
233
|
-
return true;
|
|
234
|
-
}
|
|
235
|
-
return propDeps[property] && f.model && propDeps[property].some((k) => !isEmpty(f.model[k]));
|
|
236
|
-
} });
|
|
237
|
-
}
|
|
238
|
-
if (schemaDeps[property]) {
|
|
239
|
-
const getConstValue = (s) => {
|
|
240
|
-
return s.hasOwnProperty('const') ? s.const : s.enum[0];
|
|
241
|
-
};
|
|
242
|
-
const oneOfSchema = schemaDeps[property].oneOf;
|
|
243
|
-
if (oneOfSchema &&
|
|
244
|
-
oneOfSchema.every((o) => o.properties && o.properties[property] && isConst(o.properties[property]))) {
|
|
245
|
-
oneOfSchema.forEach((oneOfSchemaItem) => {
|
|
246
|
-
const _a = oneOfSchemaItem.properties, _b = property, constSchema = _a[_b], properties = __rest(_a, [typeof _b === "symbol" ? _b : _b + ""]);
|
|
247
|
-
field.fieldGroup.push(Object.assign(Object.assign({}, this._toFieldConfig(Object.assign(Object.assign({}, oneOfSchemaItem), { properties }), Object.assign(Object.assign({}, options), { shareFormControl: false, resetOnHide: true }))), { expressions: {
|
|
248
|
-
hide: (f) => !f.model || getConstValue(constSchema) !== f.model[property],
|
|
249
|
-
} }));
|
|
250
|
-
});
|
|
251
|
-
}
|
|
252
|
-
else {
|
|
253
|
-
field.fieldGroup.push(Object.assign(Object.assign({}, this._toFieldConfig(schemaDeps[property], options)), { expressions: {
|
|
254
|
-
hide: (f) => !f.model || isEmpty(f.model[property]),
|
|
255
|
-
} }));
|
|
256
|
-
}
|
|
257
|
-
}
|
|
258
|
-
});
|
|
259
|
-
if (schema.oneOf) {
|
|
260
|
-
field.fieldGroup.push(this.resolveMultiSchema('oneOf', schema.oneOf, Object.assign(Object.assign({}, options), { shareFormControl: false })));
|
|
261
|
-
}
|
|
262
|
-
if (schema.anyOf) {
|
|
263
|
-
field.fieldGroup.push(this.resolveMultiSchema('anyOf', schema.anyOf, options));
|
|
264
|
-
}
|
|
265
|
-
break;
|
|
266
|
-
}
|
|
267
|
-
case 'array': {
|
|
268
|
-
if (schema.hasOwnProperty('minItems')) {
|
|
269
|
-
field.props.minItems = schema.minItems;
|
|
270
|
-
this.addValidator(field, 'minItems', ({ value }) => {
|
|
271
|
-
return isEmpty(value) || value.length >= schema.minItems;
|
|
272
|
-
});
|
|
273
|
-
if (!isOptional && schema.minItems > 0 && field.defaultValue === undefined) {
|
|
274
|
-
field.defaultValue = Array.from(new Array(schema.minItems));
|
|
275
|
-
}
|
|
276
|
-
}
|
|
277
|
-
if (schema.hasOwnProperty('maxItems')) {
|
|
278
|
-
field.props.maxItems = schema.maxItems;
|
|
279
|
-
this.addValidator(field, 'maxItems', ({ value }) => {
|
|
280
|
-
return isEmpty(value) || value.length <= schema.maxItems;
|
|
281
|
-
});
|
|
282
|
-
}
|
|
283
|
-
if (schema.hasOwnProperty('uniqueItems')) {
|
|
284
|
-
field.props.uniqueItems = schema.uniqueItems;
|
|
285
|
-
this.addValidator(field, 'uniqueItems', ({ value }) => {
|
|
286
|
-
if (isEmpty(value) || !schema.uniqueItems) {
|
|
287
|
-
return true;
|
|
288
|
-
}
|
|
289
|
-
const uniqueItems = Array.from(new Set(value.map((v) => JSON.stringify(v, (k, o) => {
|
|
290
|
-
if (isObject(o)) {
|
|
291
|
-
return Object.keys(o)
|
|
292
|
-
.sort()
|
|
293
|
-
.reduce((obj, key) => {
|
|
294
|
-
obj[key] = o[key];
|
|
295
|
-
return obj;
|
|
296
|
-
}, {});
|
|
297
|
-
}
|
|
298
|
-
return o;
|
|
299
|
-
}))));
|
|
300
|
-
return uniqueItems.length === value.length;
|
|
301
|
-
});
|
|
302
|
-
}
|
|
303
|
-
// resolve items schema needed for isEnum check
|
|
304
|
-
if (schema.items && !Array.isArray(schema.items)) {
|
|
305
|
-
schema.items = this.resolveSchema(schema.items, options);
|
|
306
|
-
}
|
|
307
|
-
// TODO: remove isEnum check once adding an option to skip extension
|
|
308
|
-
if (!this.isEnum(schema)) {
|
|
309
|
-
field.fieldArray = (root) => {
|
|
310
|
-
const length = root.fieldGroup ? root.fieldGroup.length : 0;
|
|
311
|
-
const items = schema.items;
|
|
312
|
-
if (!Array.isArray(items)) {
|
|
313
|
-
if (!items) {
|
|
314
|
-
return {};
|
|
315
|
-
}
|
|
316
|
-
const isMultiSchema = items.oneOf || items.anyOf;
|
|
317
|
-
// When items is a single schema, the additionalItems keyword is meaningless, and it should not be used.
|
|
318
|
-
const f = this._toFieldConfig(items, isMultiSchema ? Object.assign(Object.assign({}, options), { key: `${length}`, isOptional: false }) : Object.assign(Object.assign({}, options), { isOptional: false }));
|
|
319
|
-
if (isMultiSchema && !ɵhasKey(f)) {
|
|
320
|
-
f.key = null;
|
|
321
|
-
}
|
|
322
|
-
return f;
|
|
323
|
-
}
|
|
324
|
-
const itemSchema = items[length] ? items[length] : schema.additionalItems;
|
|
325
|
-
const f = itemSchema ? this._toFieldConfig(itemSchema, options) : {};
|
|
326
|
-
if (f.props) {
|
|
327
|
-
f.props.required = true;
|
|
328
|
-
}
|
|
329
|
-
if (items[length]) {
|
|
330
|
-
f.props.removable = false;
|
|
331
|
-
}
|
|
332
|
-
return f;
|
|
333
|
-
};
|
|
334
|
-
}
|
|
335
|
-
break;
|
|
336
|
-
}
|
|
337
|
-
}
|
|
338
|
-
if (schema.hasOwnProperty('const')) {
|
|
339
|
-
field.props.const = schema.const;
|
|
340
|
-
this.addValidator(field, 'const', ({ value }) => value === schema.const);
|
|
341
|
-
if (!field.type) {
|
|
342
|
-
field.defaultValue = schema.const;
|
|
343
|
-
}
|
|
344
|
-
}
|
|
345
|
-
if (this.isEnum(schema)) {
|
|
346
|
-
const enumOptions = this.toEnumOptions(schema);
|
|
347
|
-
const multiple = field.type === 'array';
|
|
348
|
-
field.type = 'enum';
|
|
349
|
-
field.props.multiple = multiple;
|
|
350
|
-
field.props.options = enumOptions;
|
|
351
|
-
const enumValues = enumOptions.map((o) => o.value);
|
|
352
|
-
this.addValidator(field, 'enum', ({ value }) => {
|
|
353
|
-
if (value === undefined) {
|
|
354
|
-
return true;
|
|
355
|
-
}
|
|
356
|
-
if (multiple) {
|
|
357
|
-
return Array.isArray(value) ? value.every((o) => enumValues.includes(o)) : false;
|
|
358
|
-
}
|
|
359
|
-
return enumValues.includes(value);
|
|
360
|
-
});
|
|
361
|
-
}
|
|
362
|
-
if (schema.oneOf && !field.type) {
|
|
363
|
-
delete field.key;
|
|
364
|
-
field.fieldGroup = [
|
|
365
|
-
this.resolveMultiSchema('oneOf', schema.oneOf, Object.assign(Object.assign({}, options), { key, shareFormControl: false })),
|
|
366
|
-
];
|
|
367
|
-
}
|
|
368
|
-
if (schema.anyOf && !field.type) {
|
|
369
|
-
delete field.key;
|
|
370
|
-
field.fieldGroup = [
|
|
371
|
-
this.resolveMultiSchema('oneOf', schema.anyOf, Object.assign(Object.assign({}, options), { key, shareFormControl: false })),
|
|
372
|
-
];
|
|
373
|
-
}
|
|
374
|
-
// map in possible formlyConfig options from the widget property
|
|
375
|
-
if ((_b = schema.widget) === null || _b === void 0 ? void 0 : _b.formlyConfig) {
|
|
376
|
-
field = this.mergeFields(field, schema.widget.formlyConfig);
|
|
377
|
-
}
|
|
378
|
-
field.templateOptions = field.props;
|
|
379
|
-
// if there is a map function passed in, use it to allow the user to
|
|
380
|
-
// further customize how fields are being mapped
|
|
381
|
-
return options.map ? options.map(field, schema) : field;
|
|
382
|
-
}
|
|
383
|
-
resolveSchema(schema, options) {
|
|
384
|
-
if (schema && schema.$ref) {
|
|
385
|
-
schema = this.resolveDefinition(schema, options);
|
|
386
|
-
}
|
|
387
|
-
if (schema && schema.allOf) {
|
|
388
|
-
schema = this.resolveAllOf(schema, options);
|
|
389
|
-
}
|
|
390
|
-
return schema;
|
|
391
|
-
}
|
|
392
|
-
resolveAllOf(_a, options) {
|
|
393
|
-
var { allOf } = _a, baseSchema = __rest(_a, ["allOf"]);
|
|
394
|
-
if (!allOf.length) {
|
|
395
|
-
throw Error(`allOf array can not be empty ${allOf}.`);
|
|
396
|
-
}
|
|
397
|
-
return allOf.reduce((base, schema) => {
|
|
398
|
-
schema = this.resolveSchema(schema, options);
|
|
399
|
-
if (base.required && schema.required) {
|
|
400
|
-
base.required = [...base.required, ...schema.required];
|
|
401
|
-
}
|
|
402
|
-
if (schema.uniqueItems) {
|
|
403
|
-
base.uniqueItems = schema.uniqueItems;
|
|
404
|
-
}
|
|
405
|
-
// resolve to min value
|
|
406
|
-
['maxLength', 'maximum', 'exclusiveMaximum', 'maxItems', 'maxProperties'].forEach((prop) => {
|
|
407
|
-
if (!isEmpty(base[prop]) && !isEmpty(schema[prop])) {
|
|
408
|
-
base[prop] = base[prop] < schema[prop] ? base[prop] : schema[prop];
|
|
409
|
-
}
|
|
410
|
-
});
|
|
411
|
-
// resolve to max value
|
|
412
|
-
['minLength', 'minimum', 'exclusiveMinimum', 'minItems', 'minProperties'].forEach((prop) => {
|
|
413
|
-
if (!isEmpty(base[prop]) && !isEmpty(schema[prop])) {
|
|
414
|
-
base[prop] = base[prop] > schema[prop] ? base[prop] : schema[prop];
|
|
415
|
-
}
|
|
416
|
-
});
|
|
417
|
-
return ɵreverseDeepMerge(base, schema);
|
|
418
|
-
}, baseSchema);
|
|
419
|
-
}
|
|
420
|
-
resolveMultiSchema(mode, schemas, options) {
|
|
421
|
-
return {
|
|
422
|
-
type: 'multischema',
|
|
423
|
-
fieldGroup: [
|
|
424
|
-
{
|
|
425
|
-
type: 'enum',
|
|
426
|
-
defaultValue: -1,
|
|
427
|
-
props: {
|
|
428
|
-
multiple: mode === 'anyOf',
|
|
429
|
-
options: schemas.map((s, i) => ({ label: s.title, value: i, disabled: s.readOnly })),
|
|
430
|
-
},
|
|
431
|
-
hooks: {
|
|
432
|
-
onInit: (f) => f.formControl.valueChanges.pipe(tap(() => f.options.detectChanges(f.parent))),
|
|
433
|
-
},
|
|
434
|
-
},
|
|
435
|
-
{
|
|
436
|
-
fieldGroup: schemas.map((s, i) => (Object.assign(Object.assign({}, this._toFieldConfig(s, Object.assign(Object.assign({}, options), { resetOnHide: true }))), { expressions: {
|
|
437
|
-
hide: (f, forceUpdate) => {
|
|
438
|
-
const control = f.parent.parent.fieldGroup[0].formControl;
|
|
439
|
-
if (control.value === -1 || forceUpdate) {
|
|
440
|
-
let value = f.parent.fieldGroup
|
|
441
|
-
.map((f, i) => [f, i, this.isFieldValid(f, i, schemas, options)])
|
|
442
|
-
.sort(([f1, , f1Valid], [f2, , f2Valid]) => {
|
|
443
|
-
if (f1Valid !== f2Valid) {
|
|
444
|
-
return f2Valid ? 1 : -1;
|
|
445
|
-
}
|
|
446
|
-
const matchedFields1 = totalMatchedFields(f1);
|
|
447
|
-
const matchedFields2 = totalMatchedFields(f2);
|
|
448
|
-
if (matchedFields1 === matchedFields2) {
|
|
449
|
-
if (f1.props.disabled === f2.props.disabled) {
|
|
450
|
-
return 0;
|
|
451
|
-
}
|
|
452
|
-
return matchedFields2 > matchedFields1 ? 1 : -1;
|
|
453
|
-
}
|
|
454
|
-
return matchedFields2 > matchedFields1 ? 1 : -1;
|
|
455
|
-
})
|
|
456
|
-
.map(([, i]) => i);
|
|
457
|
-
if (mode === 'anyOf') {
|
|
458
|
-
const definedValue = value.filter((i) => totalMatchedFields(f.parent.fieldGroup[i]));
|
|
459
|
-
value = definedValue.length > 0 ? definedValue : [value[0] || 0];
|
|
460
|
-
}
|
|
461
|
-
value = value.length > 0 ? value : [0];
|
|
462
|
-
control.setValue(mode === 'anyOf' ? value : value[0]);
|
|
463
|
-
}
|
|
464
|
-
return Array.isArray(control.value) ? control.value.indexOf(i) === -1 : control.value !== i;
|
|
465
|
-
},
|
|
466
|
-
} }))),
|
|
467
|
-
},
|
|
468
|
-
],
|
|
469
|
-
};
|
|
470
|
-
}
|
|
471
|
-
resolveDefinition(schema, options) {
|
|
472
|
-
const [uri, pointer] = schema.$ref.split('#/');
|
|
473
|
-
if (uri) {
|
|
474
|
-
throw Error(`Remote schemas for ${schema.$ref} not supported yet.`);
|
|
475
|
-
}
|
|
476
|
-
const definition = !pointer
|
|
477
|
-
? null
|
|
478
|
-
: pointer
|
|
479
|
-
.split('/')
|
|
480
|
-
.reduce((def, path) => ((def === null || def === void 0 ? void 0 : def.hasOwnProperty(path)) ? def[path] : null), options.schema);
|
|
481
|
-
if (!definition) {
|
|
482
|
-
throw Error(`Cannot find a definition for ${schema.$ref}.`);
|
|
483
|
-
}
|
|
484
|
-
if (definition.$ref) {
|
|
485
|
-
return this.resolveDefinition(definition, options);
|
|
486
|
-
}
|
|
487
|
-
return Object.assign(Object.assign({}, definition), ['title', 'description', 'default', 'widget'].reduce((annotation, p) => {
|
|
488
|
-
if (schema.hasOwnProperty(p)) {
|
|
489
|
-
annotation[p] = schema[p];
|
|
490
|
-
}
|
|
491
|
-
return annotation;
|
|
492
|
-
}, {}));
|
|
493
|
-
}
|
|
494
|
-
resolveDependencies(schema) {
|
|
495
|
-
const propDeps = {};
|
|
496
|
-
const schemaDeps = {};
|
|
497
|
-
Object.keys(schema.dependencies || {}).forEach((prop) => {
|
|
498
|
-
const dependency = schema.dependencies[prop];
|
|
499
|
-
if (Array.isArray(dependency)) {
|
|
500
|
-
// Property dependencies
|
|
501
|
-
dependency.forEach((dep) => {
|
|
502
|
-
if (!propDeps[dep]) {
|
|
503
|
-
propDeps[dep] = [prop];
|
|
504
|
-
}
|
|
505
|
-
else {
|
|
506
|
-
propDeps[dep].push(prop);
|
|
507
|
-
}
|
|
508
|
-
});
|
|
509
|
-
}
|
|
510
|
-
else {
|
|
511
|
-
// schema dependencies
|
|
512
|
-
schemaDeps[prop] = dependency;
|
|
513
|
-
}
|
|
514
|
-
});
|
|
515
|
-
return { propDeps, schemaDeps };
|
|
516
|
-
}
|
|
517
|
-
guessSchemaType(schema) {
|
|
518
|
-
const type = schema === null || schema === void 0 ? void 0 : schema.type;
|
|
519
|
-
if (!type && (schema === null || schema === void 0 ? void 0 : schema.properties)) {
|
|
520
|
-
return ['object'];
|
|
521
|
-
}
|
|
522
|
-
if (Array.isArray(type)) {
|
|
523
|
-
if (type.length === 1) {
|
|
524
|
-
return type;
|
|
525
|
-
}
|
|
526
|
-
if (type.length === 2 && type.indexOf('null') !== -1) {
|
|
527
|
-
return type.sort((t1) => (t1 == 'null' ? 1 : -1));
|
|
528
|
-
}
|
|
529
|
-
return type;
|
|
530
|
-
}
|
|
531
|
-
return type ? [type] : [];
|
|
532
|
-
}
|
|
533
|
-
addValidator(field, name, validator) {
|
|
534
|
-
field.validators = field.validators || {};
|
|
535
|
-
field.validators[name] = validator;
|
|
536
|
-
}
|
|
537
|
-
isEnum(schema) {
|
|
538
|
-
return (!!schema.enum ||
|
|
539
|
-
(schema.anyOf && schema.anyOf.every(isConst)) ||
|
|
540
|
-
(schema.oneOf && schema.oneOf.every(isConst)) ||
|
|
541
|
-
(schema.uniqueItems && schema.items && !Array.isArray(schema.items) && this.isEnum(schema.items)));
|
|
542
|
-
}
|
|
543
|
-
toEnumOptions(schema) {
|
|
544
|
-
if (schema.enum) {
|
|
545
|
-
return schema.enum.map((value) => ({ value, label: value }));
|
|
546
|
-
}
|
|
547
|
-
const toEnum = (s) => {
|
|
548
|
-
const value = s.hasOwnProperty('const') ? s.const : s.enum[0];
|
|
549
|
-
const option = { value, label: s.title || value };
|
|
550
|
-
if (s.readOnly) {
|
|
551
|
-
option.disabled = true;
|
|
552
|
-
}
|
|
553
|
-
return option;
|
|
554
|
-
};
|
|
555
|
-
if (schema.anyOf) {
|
|
556
|
-
return schema.anyOf.map(toEnum);
|
|
557
|
-
}
|
|
558
|
-
if (schema.oneOf) {
|
|
559
|
-
return schema.oneOf.map(toEnum);
|
|
560
|
-
}
|
|
561
|
-
return this.toEnumOptions(schema.items);
|
|
562
|
-
}
|
|
563
|
-
isFieldValid(root, i, schemas, options) {
|
|
564
|
-
const schema = schemas[i];
|
|
565
|
-
if (!schema._field) {
|
|
566
|
-
Object.defineProperty(schema, '_field', { enumerable: false, writable: true, configurable: true });
|
|
567
|
-
}
|
|
568
|
-
let field = schema._field;
|
|
569
|
-
let model = root.model ? root.model : root.fieldArray ? [] : {};
|
|
570
|
-
if (root.model && ɵhasKey(root)) {
|
|
571
|
-
model = { [Array.isArray(root.key) ? root.key.join('.') : root.key]: ɵgetFieldValue(root) };
|
|
572
|
-
}
|
|
573
|
-
model = ɵclone(model);
|
|
574
|
-
if (!field) {
|
|
575
|
-
field = schema._field = root.options.build({
|
|
576
|
-
form: Array.isArray(model) ? new FormArray([]) : new FormGroup({}),
|
|
577
|
-
fieldGroup: [
|
|
578
|
-
this._toFieldConfig(schema, Object.assign(Object.assign({}, options), { resetOnHide: true, ignoreDefault: true, map: null })),
|
|
579
|
-
],
|
|
580
|
-
model,
|
|
581
|
-
options: {},
|
|
582
|
-
});
|
|
583
|
-
}
|
|
584
|
-
else {
|
|
585
|
-
field.model = model;
|
|
586
|
-
root.options.build(field);
|
|
587
|
-
}
|
|
588
|
-
return field.form.valid;
|
|
589
|
-
}
|
|
590
|
-
mergeFields(f1, f2) {
|
|
591
|
-
for (let prop in f2) {
|
|
592
|
-
const f1Prop = prop === 'templateOptions' ? 'props' : prop;
|
|
593
|
-
if (isObject(f1[f1Prop]) && isObject(f2[prop])) {
|
|
594
|
-
f1[f1Prop] = this.mergeFields(f1[f1Prop], f2[prop]);
|
|
595
|
-
}
|
|
596
|
-
else if (f2[prop] != null) {
|
|
597
|
-
f1[f1Prop] = f2[prop];
|
|
598
|
-
}
|
|
599
|
-
}
|
|
600
|
-
return f1;
|
|
601
|
-
}
|
|
602
|
-
}
|
|
603
|
-
FormlyJsonschema.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "13.3.12", ngImport: i0, type: FormlyJsonschema, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
|
|
604
|
-
FormlyJsonschema.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "13.3.12", ngImport: i0, type: FormlyJsonschema, providedIn: 'root' });
|
|
605
|
-
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "13.3.12", ngImport: i0, type: FormlyJsonschema, decorators: [{
|
|
606
|
-
type: Injectable,
|
|
607
|
-
args: [{ providedIn: 'root' }]
|
|
608
|
-
}] });
|
|
609
|
-
|
|
610
|
-
/**
|
|
611
|
-
* Generated bundle index. Do not edit.
|
|
612
|
-
*/
|
|
613
|
-
|
|
614
|
-
export { FormlyJsonschema };
|
|
615
|
-
//# sourceMappingURL=ngx-formly-core-json-schema.mjs.map
|