@nolwenture/formik-jsonschema-form 1.3.5 → 1.3.6

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
- const anyOf = schema.anyOf;
176
- // anyOf is optional, therefore we cannot change it to array.mixedOf() aka tuple
177
- const anyOfSchema = Yup.array().of(Yup.lazy(() => {
178
- // value has to match with one of the anyOf values in the schema
179
- return Yup.mixed().oneOf(anyOf.map((item) => item.const));
180
- }));
181
- // In case the property is required, there should be at least one item in the array
182
- yupSchema = isRequired ? anyOfSchema.min(1, "Required") : anyOfSchema;
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
- const additionalItemSchema = (() => {
186
- const additionalItems = schema.additionalItems;
187
- if (!additionalItems) {
188
- return undefined;
189
- }
190
- // Yup.lazy() enables validation for infinitely nested schemas
191
- // aka references that refer to themselves or in circular references
192
- return Yup.lazy(() => {
193
- return getValue(schema?.additionalItems, "#ignore-this", true);
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);
@@ -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
- const anyOf = schema.anyOf;
140
- // anyOf is optional, therefore we cannot change it to array.mixedOf() aka tuple
141
- const anyOfSchema = Yup.array().of(Yup.lazy(() => {
142
- // value has to match with one of the anyOf values in the schema
143
- return Yup.mixed().oneOf(anyOf.map((item) => item.const));
144
- }));
145
- // In case the property is required, there should be at least one item in the array
146
- yupSchema = isRequired ? anyOfSchema.min(1, "Required") : anyOfSchema;
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
- const additionalItemSchema = (() => {
150
- const additionalItems = schema.additionalItems;
151
- if (!additionalItems) {
152
- return undefined;
153
- }
154
- // Yup.lazy() enables validation for infinitely nested schemas
155
- // aka references that refer to themselves or in circular references
156
- return Yup.lazy(() => {
157
- return getValue(schema?.additionalItems, "#ignore-this", true);
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);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nolwenture/formik-jsonschema-form",
3
- "version": "1.3.5",
3
+ "version": "1.3.6",
4
4
  "description": "Formik form generation from json schema",
5
5
  "author": "anttivikman <antti.vikman@nolwenture.com>",
6
6
  "contributors": [