@nolwenture/formik-jsonschema-form 1.3.5 → 1.3.7
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.
|
@@ -41,6 +41,9 @@ const solveReferences_1 = require("./solveReferences");
|
|
|
41
41
|
// @ts-expect-error
|
|
42
42
|
Yup.addMethod(Yup.array, "mixedOf", function ({ itemSchemas, additionalItemSchema }) {
|
|
43
43
|
return Yup.lazy((value) => {
|
|
44
|
+
if (value === undefined) {
|
|
45
|
+
return Yup.mixed().notRequired();
|
|
46
|
+
}
|
|
44
47
|
const valueLength = value?.length ?? 0;
|
|
45
48
|
const numOfAdditionalItems = itemSchemas.length > valueLength ? 0 : valueLength - itemSchemas.length;
|
|
46
49
|
const additionalSchemas = additionalItemSchema
|
|
@@ -172,33 +175,43 @@ function createValidationSchema(schema, definitions = {}, conditionsContext = {}
|
|
|
172
175
|
type = type ?? "object";
|
|
173
176
|
}
|
|
174
177
|
else if (type === "array" && schema.anyOf) {
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
178
|
+
if (!schema.items && !schema.additionalItems) {
|
|
179
|
+
yupSchema = Yup.array();
|
|
180
|
+
}
|
|
181
|
+
else {
|
|
182
|
+
const anyOf = schema.anyOf;
|
|
183
|
+
// anyOf is optional, therefore we cannot change it to array.mixedOf() aka tuple
|
|
184
|
+
const anyOfSchema = Yup.array().of(Yup.lazy(() => {
|
|
185
|
+
// value has to match with one of the anyOf values in the schema
|
|
186
|
+
return Yup.mixed().oneOf(anyOf.map((item) => item.const));
|
|
187
|
+
}));
|
|
188
|
+
// In case the property is required, there should be at least one item in the array
|
|
189
|
+
yupSchema = isRequired ? anyOfSchema.min(1, "Required") : anyOfSchema;
|
|
190
|
+
}
|
|
183
191
|
}
|
|
184
192
|
else if (type === "array") {
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
193
|
+
if (!schema.items && !schema.additionalItems) {
|
|
194
|
+
yupSchema = Yup.array();
|
|
195
|
+
}
|
|
196
|
+
else {
|
|
197
|
+
const additionalItemSchema = (() => {
|
|
198
|
+
const additionalItems = schema.additionalItems;
|
|
199
|
+
if (!additionalItems) {
|
|
200
|
+
return undefined;
|
|
201
|
+
}
|
|
202
|
+
// Yup.lazy() enables validation for infinitely nested schemas
|
|
203
|
+
// aka references that refer to themselves or in circular references
|
|
204
|
+
return Yup.lazy(() => {
|
|
205
|
+
return getValue(schema?.additionalItems, "#ignore-this", true);
|
|
206
|
+
});
|
|
207
|
+
})();
|
|
208
|
+
const schemaItems = schema?.items ?? [];
|
|
209
|
+
const items = Array.isArray(schemaItems) ? schemaItems : [schemaItems];
|
|
210
|
+
yupSchema = Yup.array().mixedOf({
|
|
211
|
+
itemSchemas: items.map((item) => getValue(item, "#ignore-this", true)),
|
|
212
|
+
additionalItemSchema,
|
|
194
213
|
});
|
|
195
|
-
}
|
|
196
|
-
const schemaItems = schema?.items ?? [];
|
|
197
|
-
const items = Array.isArray(schemaItems) ? schemaItems : [schemaItems];
|
|
198
|
-
yupSchema = Yup.array().mixedOf({
|
|
199
|
-
itemSchemas: items.map((item) => getValue(item, "#ignore-this", true)),
|
|
200
|
-
additionalItemSchema,
|
|
201
|
-
});
|
|
214
|
+
}
|
|
202
215
|
}
|
|
203
216
|
else {
|
|
204
217
|
yupSchema = getYupType(schema);
|
|
@@ -10,14 +10,16 @@ const setTitleValues = (name, title, defaultTitle, values) => {
|
|
|
10
10
|
}
|
|
11
11
|
const index = match ? parseInt(match[2]) : -1;
|
|
12
12
|
const itemValues = match ? (0, formik_1.getIn)(values, name)[index] : (0, formik_1.getIn)(values, name);
|
|
13
|
+
const fallbackName = name.split(".").slice(-1)[0];
|
|
13
14
|
return title.replace(/\$\{([\w$.:]+)\}/g, (match) => {
|
|
14
15
|
const keyMatch = match.match(/^\$\{([\w$.:]+)\}$/);
|
|
15
16
|
const key = keyMatch ? keyMatch[1] : "";
|
|
17
|
+
const value = itemValues ? (0, formik_1.getIn)(itemValues, key) : undefined;
|
|
16
18
|
return key === "index"
|
|
17
19
|
? (index + 1).toString()
|
|
18
|
-
:
|
|
19
|
-
?
|
|
20
|
-
: defaultTitle
|
|
20
|
+
: value
|
|
21
|
+
? value
|
|
22
|
+
: defaultTitle || fallbackName;
|
|
21
23
|
});
|
|
22
24
|
};
|
|
23
25
|
exports.setTitleValues = setTitleValues;
|
|
@@ -5,6 +5,9 @@ import { solveReferences } from "./solveReferences";
|
|
|
5
5
|
// @ts-expect-error
|
|
6
6
|
Yup.addMethod(Yup.array, "mixedOf", function ({ itemSchemas, additionalItemSchema }) {
|
|
7
7
|
return Yup.lazy((value) => {
|
|
8
|
+
if (value === undefined) {
|
|
9
|
+
return Yup.mixed().notRequired();
|
|
10
|
+
}
|
|
8
11
|
const valueLength = value?.length ?? 0;
|
|
9
12
|
const numOfAdditionalItems = itemSchemas.length > valueLength ? 0 : valueLength - itemSchemas.length;
|
|
10
13
|
const additionalSchemas = additionalItemSchema
|
|
@@ -136,33 +139,43 @@ export function createValidationSchema(schema, definitions = {}, conditionsConte
|
|
|
136
139
|
type = type ?? "object";
|
|
137
140
|
}
|
|
138
141
|
else if (type === "array" && schema.anyOf) {
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
142
|
+
if (!schema.items && !schema.additionalItems) {
|
|
143
|
+
yupSchema = Yup.array();
|
|
144
|
+
}
|
|
145
|
+
else {
|
|
146
|
+
const anyOf = schema.anyOf;
|
|
147
|
+
// anyOf is optional, therefore we cannot change it to array.mixedOf() aka tuple
|
|
148
|
+
const anyOfSchema = Yup.array().of(Yup.lazy(() => {
|
|
149
|
+
// value has to match with one of the anyOf values in the schema
|
|
150
|
+
return Yup.mixed().oneOf(anyOf.map((item) => item.const));
|
|
151
|
+
}));
|
|
152
|
+
// In case the property is required, there should be at least one item in the array
|
|
153
|
+
yupSchema = isRequired ? anyOfSchema.min(1, "Required") : anyOfSchema;
|
|
154
|
+
}
|
|
147
155
|
}
|
|
148
156
|
else if (type === "array") {
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
157
|
+
if (!schema.items && !schema.additionalItems) {
|
|
158
|
+
yupSchema = Yup.array();
|
|
159
|
+
}
|
|
160
|
+
else {
|
|
161
|
+
const additionalItemSchema = (() => {
|
|
162
|
+
const additionalItems = schema.additionalItems;
|
|
163
|
+
if (!additionalItems) {
|
|
164
|
+
return undefined;
|
|
165
|
+
}
|
|
166
|
+
// Yup.lazy() enables validation for infinitely nested schemas
|
|
167
|
+
// aka references that refer to themselves or in circular references
|
|
168
|
+
return Yup.lazy(() => {
|
|
169
|
+
return getValue(schema?.additionalItems, "#ignore-this", true);
|
|
170
|
+
});
|
|
171
|
+
})();
|
|
172
|
+
const schemaItems = schema?.items ?? [];
|
|
173
|
+
const items = Array.isArray(schemaItems) ? schemaItems : [schemaItems];
|
|
174
|
+
yupSchema = Yup.array().mixedOf({
|
|
175
|
+
itemSchemas: items.map((item) => getValue(item, "#ignore-this", true)),
|
|
176
|
+
additionalItemSchema,
|
|
158
177
|
});
|
|
159
|
-
}
|
|
160
|
-
const schemaItems = schema?.items ?? [];
|
|
161
|
-
const items = Array.isArray(schemaItems) ? schemaItems : [schemaItems];
|
|
162
|
-
yupSchema = Yup.array().mixedOf({
|
|
163
|
-
itemSchemas: items.map((item) => getValue(item, "#ignore-this", true)),
|
|
164
|
-
additionalItemSchema,
|
|
165
|
-
});
|
|
178
|
+
}
|
|
166
179
|
}
|
|
167
180
|
else {
|
|
168
181
|
yupSchema = getYupType(schema);
|
|
@@ -7,13 +7,15 @@ export const setTitleValues = (name, title, defaultTitle, values) => {
|
|
|
7
7
|
}
|
|
8
8
|
const index = match ? parseInt(match[2]) : -1;
|
|
9
9
|
const itemValues = match ? getIn(values, name)[index] : getIn(values, name);
|
|
10
|
+
const fallbackName = name.split(".").slice(-1)[0];
|
|
10
11
|
return title.replace(/\$\{([\w$.:]+)\}/g, (match) => {
|
|
11
12
|
const keyMatch = match.match(/^\$\{([\w$.:]+)\}$/);
|
|
12
13
|
const key = keyMatch ? keyMatch[1] : "";
|
|
14
|
+
const value = itemValues ? getIn(itemValues, key) : undefined;
|
|
13
15
|
return key === "index"
|
|
14
16
|
? (index + 1).toString()
|
|
15
|
-
:
|
|
16
|
-
?
|
|
17
|
-
: defaultTitle
|
|
17
|
+
: value
|
|
18
|
+
? value
|
|
19
|
+
: defaultTitle || fallbackName;
|
|
18
20
|
});
|
|
19
21
|
};
|