@nolwenture/formik-jsonschema-form 0.26.1 → 0.26.3
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/dist/Form.js +3 -3
- package/dist/Form.stories.js +9 -32
- package/dist/FormEditor.js +3 -12
- package/dist/FormPdf.js +3 -3
- package/dist/FormPdf.stories.js +7 -30
- package/dist/fields/AnyField.js +3 -3
- package/dist/fields/ArrayField.js +9 -9
- package/dist/fields/BooleanField.js +6 -3
- package/dist/fields/DateField.js +1 -1
- package/dist/fields/FileUploadField.js +9 -18
- package/dist/fields/MultipleChoiceField.js +2 -2
- package/dist/fields/NullField.js +1 -1
- package/dist/fields/NumberField.js +4 -2
- package/dist/fields/ObjectField.js +11 -23
- package/dist/fields/RadioField.js +1 -1
- package/dist/fields/SchemaField.js +6 -2
- package/dist/fields/SearchMultiTagSelectField.js +1 -1
- package/dist/fields/SearchableSelectField.js +19 -25
- package/dist/fields/StringField.js +11 -9
- package/dist/fields/TextAreaField.js +1 -1
- package/dist/fields/ToggleField.js +1 -1
- package/dist/fields/stories/AnyField.stories.js +22 -31
- package/dist/fields/stories/ArrayField.stories.js +64 -73
- package/dist/fields/stories/BooleanField.stories.js +6 -15
- package/dist/fields/stories/DateField.stories.js +4 -13
- package/dist/fields/stories/FileUploadField.stories.js +9 -18
- package/dist/fields/stories/MultipleChoiceField.stories.js +24 -33
- package/dist/fields/stories/NullField.stories.js +3 -12
- package/dist/fields/stories/NumberField.stories.js +4 -13
- package/dist/fields/stories/ObjectField.stories.js +11 -20
- package/dist/fields/stories/RadioField.stories.js +8 -17
- package/dist/fields/stories/SchemaField.stories.js +24 -17
- package/dist/fields/stories/SearchMultiTagSelectField.stories.js +8 -17
- package/dist/fields/stories/StringField.stories.js +17 -26
- package/dist/fields/stories/TextAreaField.stories.js +4 -13
- package/dist/fields/stories/ToggleField.stories.js +6 -15
- package/dist/hooks/useExtractUiSchema.js +13 -16
- package/dist/pdfFields/AnyField.js +3 -3
- package/dist/pdfFields/ArrayField.js +3 -4
- package/dist/pdfFields/BooleanField.js +1 -1
- package/dist/pdfFields/DateField.js +4 -2
- package/dist/pdfFields/EJSField.js +1 -2
- package/dist/pdfFields/MultipleChoiceField.js +2 -3
- package/dist/pdfFields/NullField.js +1 -1
- package/dist/pdfFields/NumberField.js +4 -2
- package/dist/pdfFields/ObjectField.js +7 -19
- package/dist/pdfFields/RadioField.js +7 -2
- package/dist/pdfFields/SchemaField.js +6 -2
- package/dist/pdfFields/SearchMultiTagSelectField.js +1 -1
- package/dist/pdfFields/SearchableSelectField.js +1 -1
- package/dist/pdfFields/StringField.js +2 -3
- package/dist/pdfFields/TextAreaField.js +1 -1
- package/dist/pdfFields/ToggleField.js +7 -2
- package/dist/pdfTemplates/ArrayFieldTemplate.js +1 -1
- package/dist/pdfTemplates/CollapsibleObjectFieldTemplate.js +1 -1
- package/dist/pdfTemplates/FieldTemplate.js +1 -1
- package/dist/pdfTemplates/ObjectFieldTemplate.js +1 -1
- package/dist/styledComponents.js +39 -59
- package/dist/templates/ArrayFieldTemplate.js +6 -14
- package/dist/templates/CollapsibleObjectFieldTemplate.js +6 -14
- package/dist/templates/FieldTemplate.js +6 -14
- package/dist/templates/ObjectFieldTemplate.js +6 -14
- package/dist/utils/__tests/createInitialValues.test.js +2 -11
- package/dist/utils/__tests/createValidationSchema.test.js +30 -35
- package/dist/utils/__tests/solveReferences.test.js +4 -8
- package/dist/utils/createInitialValues.js +9 -7
- package/dist/utils/createValidationSchema.js +19 -32
- package/dist/utils/getEnums.js +2 -2
- package/dist/utils/getTitleAndDescription.js +7 -9
- package/dist/utils/hasReferences.js +2 -2
- package/dist/utils/resolveSchema.js +16 -30
- package/dist/utils/solveReferences.js +3 -4
- package/package.json +9 -9
|
@@ -1,12 +1,3 @@
|
|
|
1
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
-
});
|
|
9
|
-
};
|
|
10
1
|
import { ageFieldDefinition } from "../../sampleData/ageFieldDefinition";
|
|
11
2
|
import { createValidationSchema } from "../createValidationSchema";
|
|
12
3
|
const exampleSchema = {
|
|
@@ -324,7 +315,6 @@ const nestedRequiredConditionalExampleSchema = {
|
|
|
324
315
|
};
|
|
325
316
|
describe("Create Validation Schema", () => {
|
|
326
317
|
function listErrors(schema, data, definitions) {
|
|
327
|
-
var _a;
|
|
328
318
|
const yupSchema = createValidationSchema(schema, definitions);
|
|
329
319
|
try {
|
|
330
320
|
// Cannot be strict, since forms always use string based values
|
|
@@ -332,10 +322,10 @@ describe("Create Validation Schema", () => {
|
|
|
332
322
|
}
|
|
333
323
|
catch (err) {
|
|
334
324
|
const error = err;
|
|
335
|
-
return
|
|
325
|
+
return error?.inner?.map((e, i) => `${e.path}: ${error.errors[i].replace(e?.path ?? "", "").trim()}`);
|
|
336
326
|
}
|
|
337
327
|
}
|
|
338
|
-
it("creates yup validationSchema based on given JSON schema", () =>
|
|
328
|
+
it("creates yup validationSchema based on given JSON schema", async () => {
|
|
339
329
|
const result = createValidationSchema(exampleSchema);
|
|
340
330
|
const expected = result.isValidSync({
|
|
341
331
|
name: "Jane",
|
|
@@ -354,19 +344,19 @@ describe("Create Validation Schema", () => {
|
|
|
354
344
|
},
|
|
355
345
|
});
|
|
356
346
|
expect(expected).toEqual(true);
|
|
357
|
-
})
|
|
358
|
-
it("throws error when required input is missing", () =>
|
|
347
|
+
});
|
|
348
|
+
it("throws error when required input is missing", async () => {
|
|
359
349
|
const result = createValidationSchema(simpleExampleSchema);
|
|
360
|
-
|
|
350
|
+
await expect(() => result.validate({
|
|
361
351
|
firstName: "Jane",
|
|
362
352
|
lastName: undefined,
|
|
363
353
|
age: 55,
|
|
364
354
|
})).rejects.toThrow("Required");
|
|
365
|
-
})
|
|
366
|
-
it("throws error when given input is not matching defined type", () =>
|
|
355
|
+
});
|
|
356
|
+
it("throws error when given input is not matching defined type", async () => {
|
|
367
357
|
const result = createValidationSchema(simpleExampleSchema);
|
|
368
358
|
// for 'number' type
|
|
369
|
-
|
|
359
|
+
await expect(() => result.validate({
|
|
370
360
|
firstName: "Ella",
|
|
371
361
|
lastName: "Myller",
|
|
372
362
|
age: true,
|
|
@@ -374,7 +364,7 @@ describe("Create Validation Schema", () => {
|
|
|
374
364
|
simpleArray: [],
|
|
375
365
|
}, { strict: true })).rejects.toThrow("age must be a `number` type, but the final value was: `true`.");
|
|
376
366
|
// for 'string' type
|
|
377
|
-
|
|
367
|
+
await expect(() => result.validate({
|
|
378
368
|
firstName: 123,
|
|
379
369
|
lastName: "Myller",
|
|
380
370
|
age: 17,
|
|
@@ -382,7 +372,7 @@ describe("Create Validation Schema", () => {
|
|
|
382
372
|
simpleArray: [],
|
|
383
373
|
}, { strict: true })).rejects.toThrow("firstName must be a `string` type, but the final value was: `123`.");
|
|
384
374
|
// for 'boolean' type
|
|
385
|
-
|
|
375
|
+
await expect(() => result.validate({
|
|
386
376
|
firstName: "Ella",
|
|
387
377
|
lastName: "Myller",
|
|
388
378
|
age: 17,
|
|
@@ -390,14 +380,14 @@ describe("Create Validation Schema", () => {
|
|
|
390
380
|
simpleArray: [],
|
|
391
381
|
}, { strict: true })).rejects.toThrow('drinksCoffee must be a `boolean` type, but the final value was: `"Well, sometimes..."`.');
|
|
392
382
|
// for 'array' type
|
|
393
|
-
|
|
383
|
+
await expect(() => result.validate({
|
|
394
384
|
firstName: "Ella",
|
|
395
385
|
lastName: "Myller",
|
|
396
386
|
age: 17,
|
|
397
387
|
drinksCoffee: false,
|
|
398
388
|
simpleArray: "Hello :)",
|
|
399
389
|
}, { strict: true })).rejects.toThrow("Required");
|
|
400
|
-
})
|
|
390
|
+
});
|
|
401
391
|
describe("Conditionals", () => {
|
|
402
392
|
it("generates validation for dependencies without oneOf", () => {
|
|
403
393
|
const result = createValidationSchema(dependenciesExampleSchema);
|
|
@@ -502,7 +492,7 @@ describe("Create Validation Schema", () => {
|
|
|
502
492
|
expect(firstTrueInvalid3).toEqual(false);
|
|
503
493
|
expect(firstTrueValid).toEqual(true);
|
|
504
494
|
});
|
|
505
|
-
it("generates validation for if-then", () =>
|
|
495
|
+
it("generates validation for if-then", async () => {
|
|
506
496
|
const faultyBusiness = listErrors(allOfExample, {
|
|
507
497
|
customerType: "BUSINESS",
|
|
508
498
|
});
|
|
@@ -571,7 +561,7 @@ describe("Create Validation Schema", () => {
|
|
|
571
561
|
// eslint-disable-next-line
|
|
572
562
|
// @ts-expect-error
|
|
573
563
|
expect(yupSchema.fields.code.deps.length).toEqual(3);
|
|
574
|
-
})
|
|
564
|
+
});
|
|
575
565
|
it("generates validation for conditional objects with nested validations", () => {
|
|
576
566
|
const result = createValidationSchema(nestedRequiredConditionalExampleSchema);
|
|
577
567
|
const isValidWhenConditionNotNet = result.isValidSync({
|
|
@@ -599,10 +589,15 @@ describe("Create Validation Schema", () => {
|
|
|
599
589
|
title: "Array of objects Schema",
|
|
600
590
|
required: ["arrayOfObjects"],
|
|
601
591
|
properties: {
|
|
602
|
-
arrayOfObjects:
|
|
592
|
+
arrayOfObjects: {
|
|
593
|
+
type: "array",
|
|
594
|
+
title: "A list of fixed items",
|
|
595
|
+
items,
|
|
596
|
+
...(additionalItems ? { additionalItems } : {}),
|
|
597
|
+
},
|
|
603
598
|
},
|
|
604
599
|
});
|
|
605
|
-
it("requires fields with array items and additionalItems", () =>
|
|
600
|
+
it("requires fields with array items and additionalItems", async () => {
|
|
606
601
|
const arraySchema = createArraySchema({
|
|
607
602
|
items: [
|
|
608
603
|
{ title: "A string value", type: "string" },
|
|
@@ -740,8 +735,8 @@ describe("Create Validation Schema", () => {
|
|
|
740
735
|
],
|
|
741
736
|
});
|
|
742
737
|
expect(correct).toEqual(undefined);
|
|
743
|
-
})
|
|
744
|
-
it("required fields with array items only", () =>
|
|
738
|
+
});
|
|
739
|
+
it("required fields with array items only", async () => {
|
|
745
740
|
const arraySchema = createArraySchema({
|
|
746
741
|
items: [
|
|
747
742
|
{ title: "A string value", type: "string" },
|
|
@@ -762,8 +757,8 @@ describe("Create Validation Schema", () => {
|
|
|
762
757
|
arrayOfObjects: ["name", 123, true, 123.141241],
|
|
763
758
|
});
|
|
764
759
|
expect(coprrecorrect).toEqual(undefined);
|
|
765
|
-
})
|
|
766
|
-
it("requires fields with array items and additionalItems with definitions", () =>
|
|
760
|
+
});
|
|
761
|
+
it("requires fields with array items and additionalItems with definitions", async () => {
|
|
767
762
|
const arraySchema = {
|
|
768
763
|
title: "TITLE",
|
|
769
764
|
properties: {
|
|
@@ -814,8 +809,8 @@ describe("Create Validation Schema", () => {
|
|
|
814
809
|
],
|
|
815
810
|
}, definitions);
|
|
816
811
|
expect(correct).toEqual(undefined);
|
|
817
|
-
})
|
|
818
|
-
it("validates anyOf correctly", () =>
|
|
812
|
+
});
|
|
813
|
+
it("validates anyOf correctly", async () => {
|
|
819
814
|
const optionalInput = listErrors(anyOfExample(), { warningSymbols: [] });
|
|
820
815
|
expect(optionalInput).toEqual(undefined);
|
|
821
816
|
const requiredInput = listErrors(anyOfExample(["warningSymbols"]), {
|
|
@@ -841,7 +836,7 @@ describe("Create Validation Schema", () => {
|
|
|
841
836
|
warningSymbols: ["GHS01", "GHS05", 7890],
|
|
842
837
|
});
|
|
843
838
|
expect(validInput).toEqual(undefined);
|
|
844
|
-
})
|
|
839
|
+
});
|
|
845
840
|
});
|
|
846
841
|
describe("Infinite loop validation", () => {
|
|
847
842
|
const definitions = {
|
|
@@ -873,7 +868,7 @@ describe("Create Validation Schema", () => {
|
|
|
873
868
|
},
|
|
874
869
|
},
|
|
875
870
|
};
|
|
876
|
-
it("does not throw RangeError: Maximum call stack size exceeded", () =>
|
|
871
|
+
it("does not throw RangeError: Maximum call stack size exceeded", async () => {
|
|
877
872
|
const validInput1 = listErrors(treeSchema, {
|
|
878
873
|
organization: {
|
|
879
874
|
name: "Org A",
|
|
@@ -944,6 +939,6 @@ describe("Create Validation Schema", () => {
|
|
|
944
939
|
},
|
|
945
940
|
}, definitions);
|
|
946
941
|
expect(invalidInput).toEqual(["organization.subOrganizations: Required"]);
|
|
947
|
-
})
|
|
942
|
+
});
|
|
948
943
|
});
|
|
949
944
|
});
|
|
@@ -2,8 +2,7 @@ import { solveReferences } from "../solveReferences";
|
|
|
2
2
|
import { refDefSchema, refDefUiSchema } from "./testSchemas";
|
|
3
3
|
describe("solveReferences", () => {
|
|
4
4
|
it("should resolve simple references correctly", () => {
|
|
5
|
-
|
|
6
|
-
const schemaSolvedForReferences = solveReferences((_a = refDefSchema === null || refDefSchema === void 0 ? void 0 : refDefSchema.properties) === null || _a === void 0 ? void 0 : _a.name, refDefSchema.definitions, {}, {});
|
|
5
|
+
const schemaSolvedForReferences = solveReferences(refDefSchema?.properties?.name, refDefSchema.definitions, {}, {});
|
|
7
6
|
expect(schemaSolvedForReferences).toEqual({
|
|
8
7
|
schema: {
|
|
9
8
|
type: "string",
|
|
@@ -13,8 +12,7 @@ describe("solveReferences", () => {
|
|
|
13
12
|
});
|
|
14
13
|
});
|
|
15
14
|
it("should resolve simple references with uiSchema correctly", () => {
|
|
16
|
-
|
|
17
|
-
const schemaSolvedForReferences = solveReferences((_a = refDefSchema === null || refDefSchema === void 0 ? void 0 : refDefSchema.properties) === null || _a === void 0 ? void 0 : _a.birthday, refDefSchema.definitions, (_b = refDefUiSchema.birthday) !== null && _b !== void 0 ? _b : {}, refDefUiSchema.definitions);
|
|
15
|
+
const schemaSolvedForReferences = solveReferences(refDefSchema?.properties?.birthday, refDefSchema.definitions, refDefUiSchema.birthday ?? {}, refDefUiSchema.definitions);
|
|
18
16
|
expect(schemaSolvedForReferences).toEqual({
|
|
19
17
|
schema: {
|
|
20
18
|
type: "string",
|
|
@@ -24,8 +22,7 @@ describe("solveReferences", () => {
|
|
|
24
22
|
});
|
|
25
23
|
});
|
|
26
24
|
it("should override uiSchema settings when key of field is also given outside definitions object of uiSchema", () => {
|
|
27
|
-
|
|
28
|
-
const schemaSolvedForReferences = solveReferences((_a = refDefSchema === null || refDefSchema === void 0 ? void 0 : refDefSchema.properties) === null || _a === void 0 ? void 0 : _a.friendBirthday, refDefSchema.definitions, (_b = refDefUiSchema.friendBirthday) !== null && _b !== void 0 ? _b : {}, refDefUiSchema.definitions);
|
|
25
|
+
const schemaSolvedForReferences = solveReferences(refDefSchema?.properties?.friendBirthday, refDefSchema.definitions, refDefUiSchema.friendBirthday ?? {}, refDefUiSchema.definitions);
|
|
29
26
|
expect(schemaSolvedForReferences).toEqual({
|
|
30
27
|
schema: {
|
|
31
28
|
type: "string",
|
|
@@ -38,8 +35,7 @@ describe("solveReferences", () => {
|
|
|
38
35
|
});
|
|
39
36
|
});
|
|
40
37
|
it("should merge field and definition schema", () => {
|
|
41
|
-
|
|
42
|
-
const schemaSolvedForReferences = solveReferences((_a = refDefSchema === null || refDefSchema === void 0 ? void 0 : refDefSchema.properties) === null || _a === void 0 ? void 0 : _a.address, refDefSchema.definitions, {}, {});
|
|
38
|
+
const schemaSolvedForReferences = solveReferences(refDefSchema?.properties?.address, refDefSchema.definitions, {}, {});
|
|
43
39
|
expect(schemaSolvedForReferences).toEqual({
|
|
44
40
|
schema: {
|
|
45
41
|
type: "object",
|
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
import { solveReferences } from "./solveReferences";
|
|
2
2
|
export function createInitialValues(schema, definitions = {}) {
|
|
3
|
-
var _a;
|
|
4
3
|
if (!schema || !schema.properties) {
|
|
5
4
|
return {};
|
|
6
5
|
}
|
|
7
|
-
const schemaDefinitions =
|
|
6
|
+
const schemaDefinitions = { ...definitions, ...(schema.definitions ?? {}) };
|
|
8
7
|
function getValue(schema) {
|
|
9
8
|
if (schema.properties) {
|
|
10
9
|
return createInitialValues(schema, schemaDefinitions);
|
|
@@ -21,13 +20,16 @@ export function createInitialValues(schema, definitions = {}) {
|
|
|
21
20
|
case "string":
|
|
22
21
|
return "";
|
|
23
22
|
default:
|
|
24
|
-
|
|
23
|
+
console.error({ schema });
|
|
24
|
+
throw new Error(`createInitialValues: Unsupported schema type: ${type}`);
|
|
25
25
|
}
|
|
26
26
|
}
|
|
27
|
-
return Object.keys(schema
|
|
28
|
-
|
|
29
|
-
const value = (_a = schema === null || schema === void 0 ? void 0 : schema.properties) === null || _a === void 0 ? void 0 : _a[key];
|
|
27
|
+
return Object.keys(schema?.properties).reduce((acc, key) => {
|
|
28
|
+
const value = schema?.properties?.[key];
|
|
30
29
|
const solvedValue = solveReferences(value, schemaDefinitions, {}, {});
|
|
31
|
-
return
|
|
30
|
+
return {
|
|
31
|
+
...acc,
|
|
32
|
+
[key]: getValue(solvedValue.schema),
|
|
33
|
+
};
|
|
32
34
|
}, {});
|
|
33
35
|
}
|
|
@@ -1,14 +1,3 @@
|
|
|
1
|
-
var __rest = (this && this.__rest) || function (s, e) {
|
|
2
|
-
var t = {};
|
|
3
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
4
|
-
t[p] = s[p];
|
|
5
|
-
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
6
|
-
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
7
|
-
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
8
|
-
t[p[i]] = s[p[i]];
|
|
9
|
-
}
|
|
10
|
-
return t;
|
|
11
|
-
};
|
|
12
1
|
import * as Yup from "yup";
|
|
13
2
|
import { resolveOneOfValue } from "./resolveSchema";
|
|
14
3
|
import { solveReferences } from "./solveReferences";
|
|
@@ -16,8 +5,7 @@ import { solveReferences } from "./solveReferences";
|
|
|
16
5
|
// @ts-expect-error
|
|
17
6
|
Yup.addMethod(Yup.array, "mixedOf", function ({ itemSchemas, additionalItemSchema }) {
|
|
18
7
|
return Yup.lazy((value) => {
|
|
19
|
-
|
|
20
|
-
const valueLength = (_a = value === null || value === void 0 ? void 0 : value.length) !== null && _a !== void 0 ? _a : 0;
|
|
8
|
+
const valueLength = value?.length ?? 0;
|
|
21
9
|
const numOfAdditionalItems = itemSchemas.length > valueLength ? 0 : valueLength - itemSchemas.length;
|
|
22
10
|
const additionalSchemas = additionalItemSchema
|
|
23
11
|
? Array(numOfAdditionalItems).fill(additionalItemSchema)
|
|
@@ -36,16 +24,15 @@ function getConditionalValidations(schema) {
|
|
|
36
24
|
const oneOf = dependency.oneOf;
|
|
37
25
|
if (Array.isArray(oneOf)) {
|
|
38
26
|
oneOf.forEach((oneOfSchema) => {
|
|
39
|
-
var _a, _b;
|
|
40
27
|
if (typeof oneOfSchema === "boolean") {
|
|
41
28
|
return;
|
|
42
29
|
}
|
|
43
|
-
const
|
|
30
|
+
const { [key]: propertyToCheck, ...propertiesSansKey } = oneOfSchema.properties ?? {};
|
|
44
31
|
// Push validation only if there are additions in schema
|
|
45
|
-
if (
|
|
32
|
+
if (Object.keys(propertiesSansKey)?.length ||
|
|
46
33
|
Object.keys(oneOfSchema).length > 1) {
|
|
47
34
|
validations.push({
|
|
48
|
-
schema:
|
|
35
|
+
schema: { ...oneOfSchema, properties: propertiesSansKey },
|
|
49
36
|
condition: {
|
|
50
37
|
dependentKeys: [key],
|
|
51
38
|
conditionFn: (value) => value === resolveOneOfValue(propertyToCheck),
|
|
@@ -71,13 +58,13 @@ function getConditionalValidations(schema) {
|
|
|
71
58
|
if (typeof condition === "boolean") {
|
|
72
59
|
return validations;
|
|
73
60
|
}
|
|
74
|
-
const { if: expression, then, else: otherwise
|
|
61
|
+
const { if: expression, then, else: otherwise, ...schemaWithoutCondition } = condition;
|
|
75
62
|
if (expression &&
|
|
76
63
|
typeof expression !== "boolean" &&
|
|
77
64
|
expression.properties) {
|
|
78
65
|
const keyValueMap = Object.entries(expression.properties).reduce((result, [key, value]) => {
|
|
79
66
|
if (typeof value !== "boolean") {
|
|
80
|
-
return
|
|
67
|
+
return { ...result, [key]: value.const };
|
|
81
68
|
}
|
|
82
69
|
return result;
|
|
83
70
|
}, {});
|
|
@@ -110,7 +97,7 @@ function getConditionalValidations(schema) {
|
|
|
110
97
|
return conditionalValidations;
|
|
111
98
|
}
|
|
112
99
|
function getYupType(schema) {
|
|
113
|
-
const type = schema
|
|
100
|
+
const type = schema?.type;
|
|
114
101
|
switch (type) {
|
|
115
102
|
case "object":
|
|
116
103
|
return Yup.object();
|
|
@@ -127,25 +114,26 @@ function getYupType(schema) {
|
|
|
127
114
|
return Yup.string();
|
|
128
115
|
default:
|
|
129
116
|
console.error({ schema });
|
|
130
|
-
throw new Error(`Unsupported schema type: ${type}`);
|
|
117
|
+
throw new Error(`createValidationSchema: Unsupported schema type: ${type}`);
|
|
131
118
|
}
|
|
132
119
|
}
|
|
133
120
|
export function createValidationSchema(schema, definitions = {}, conditionsContext = {}, condition) {
|
|
134
|
-
|
|
135
|
-
if (!(schema === null || schema === void 0 ? void 0 : schema.properties)) {
|
|
121
|
+
if (!schema?.properties) {
|
|
136
122
|
return Yup.object().shape({});
|
|
137
123
|
}
|
|
138
|
-
const schemaDefinitions =
|
|
124
|
+
const schemaDefinitions = {
|
|
125
|
+
...definitions,
|
|
126
|
+
...(schema.definitions ?? {}),
|
|
127
|
+
};
|
|
139
128
|
const requiredFields = schema.required;
|
|
140
129
|
function getValue(givenSchema, key, required) {
|
|
141
|
-
var _a, _b;
|
|
142
130
|
const { schema } = solveReferences(givenSchema, schemaDefinitions, {}, {});
|
|
143
|
-
const isRequired =
|
|
131
|
+
const isRequired = requiredFields?.includes(key) || required === true;
|
|
144
132
|
let type = schema.type;
|
|
145
133
|
let yupSchema;
|
|
146
134
|
if (schema.properties) {
|
|
147
135
|
yupSchema = createValidationSchema(schema, schemaDefinitions, conditionsContext);
|
|
148
|
-
type = type
|
|
136
|
+
type = type ?? "object";
|
|
149
137
|
}
|
|
150
138
|
else if (type === "array" && schema.anyOf) {
|
|
151
139
|
const anyOf = schema.anyOf;
|
|
@@ -166,10 +154,10 @@ export function createValidationSchema(schema, definitions = {}, conditionsConte
|
|
|
166
154
|
// Yup.lazy() enables validation for infinitely nested schemas
|
|
167
155
|
// aka references that refer to themselves or in circular references
|
|
168
156
|
return Yup.lazy(() => {
|
|
169
|
-
return getValue(schema
|
|
157
|
+
return getValue(schema?.additionalItems, "#ignore-this", true);
|
|
170
158
|
});
|
|
171
159
|
})();
|
|
172
|
-
const schemaItems =
|
|
160
|
+
const schemaItems = schema?.items ?? [];
|
|
173
161
|
const items = Array.isArray(schemaItems) ? schemaItems : [schemaItems];
|
|
174
162
|
yupSchema = Yup.array().mixedOf({
|
|
175
163
|
itemSchemas: items.map((item) => getValue(item, "#ignore-this", true)),
|
|
@@ -187,7 +175,7 @@ export function createValidationSchema(schema, definitions = {}, conditionsConte
|
|
|
187
175
|
if (!condition) {
|
|
188
176
|
return yupSchema;
|
|
189
177
|
}
|
|
190
|
-
const existing =
|
|
178
|
+
const existing = conditionsContext[key] ?? [];
|
|
191
179
|
conditionsContext[key] = [...existing, { yupSchema, condition }];
|
|
192
180
|
// TODO: The latest given type for duplicate field is used.
|
|
193
181
|
// This might become a problem at some point, but testing has not
|
|
@@ -209,8 +197,7 @@ export function createValidationSchema(schema, definitions = {}, conditionsConte
|
|
|
209
197
|
}, yupType);
|
|
210
198
|
}
|
|
211
199
|
const baseValidationSchema = Object.keys(schema.properties).reduce((validationSchema, key) => {
|
|
212
|
-
|
|
213
|
-
const value = (_a = schema.properties) === null || _a === void 0 ? void 0 : _a[key];
|
|
200
|
+
const value = schema.properties?.[key];
|
|
214
201
|
return validationSchema.concat(Yup.object().shape({ [key]: getValue(value, key) }));
|
|
215
202
|
}, Yup.object().shape({}));
|
|
216
203
|
return getConditionalValidations(schema).reduce((validationSchema, conditionalValidation) => {
|
package/dist/utils/getEnums.js
CHANGED
|
@@ -34,12 +34,12 @@ export const getEnums = ({ schema }) => {
|
|
|
34
34
|
export const getSelectedEnum = (selected, enums) => {
|
|
35
35
|
if (Array.isArray(selected)) {
|
|
36
36
|
const stuff = selected
|
|
37
|
-
.map((v) => enums
|
|
37
|
+
.map((v) => enums?.values.find((e) => e.const === v))
|
|
38
38
|
.filter((e) => !!e);
|
|
39
39
|
return stuff.length > 0 ? stuff : undefined;
|
|
40
40
|
}
|
|
41
41
|
if (typeof selected === "string" || typeof selected === "number") {
|
|
42
|
-
return enums
|
|
42
|
+
return enums?.values.filter((e) => e.const === selected);
|
|
43
43
|
}
|
|
44
44
|
return undefined;
|
|
45
45
|
};
|
|
@@ -1,15 +1,13 @@
|
|
|
1
1
|
export const getTitle = ({ title, uiSchema, schema, name, }) => {
|
|
2
|
-
var _a;
|
|
3
2
|
return (title ||
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
3
|
+
uiSchema?.["ui:options"]?.title ||
|
|
4
|
+
uiSchema?.["ui:title"] ||
|
|
5
|
+
schema?.title ||
|
|
6
|
+
name?.split(".")[name?.split(".").length - 1]);
|
|
8
7
|
};
|
|
9
8
|
export const getDescription = ({ description, uiSchema, schema, }) => {
|
|
10
|
-
var _a;
|
|
11
9
|
return (description ||
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
10
|
+
uiSchema?.["ui:options"]?.description ||
|
|
11
|
+
uiSchema?.["ui:description"] ||
|
|
12
|
+
schema?.description);
|
|
15
13
|
};
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
export function hasReferences(schema) {
|
|
2
|
-
const properties = schema
|
|
2
|
+
const properties = schema?.properties;
|
|
3
3
|
if (!properties)
|
|
4
4
|
return false;
|
|
5
5
|
return Object.keys(properties).some((key) => {
|
|
6
6
|
const value = properties[key];
|
|
7
|
-
if (value
|
|
7
|
+
if (value?.properties) {
|
|
8
8
|
return hasReferences(value);
|
|
9
9
|
}
|
|
10
10
|
return Object.keys(value).includes("$ref");
|
|
@@ -1,27 +1,18 @@
|
|
|
1
|
-
var __rest = (this && this.__rest) || function (s, e) {
|
|
2
|
-
var t = {};
|
|
3
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
4
|
-
t[p] = s[p];
|
|
5
|
-
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
6
|
-
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
7
|
-
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
8
|
-
t[p[i]] = s[p[i]];
|
|
9
|
-
}
|
|
10
|
-
return t;
|
|
11
|
-
};
|
|
12
1
|
import { deepOverlap } from "./deepOverlap";
|
|
13
2
|
import { mergeSchemas } from "./mergeSchemas";
|
|
14
3
|
const resolveEquality = (value, expression) => {
|
|
15
4
|
const keyOfValue = Object.keys(value);
|
|
16
5
|
const valueOfValue = Object.values(value);
|
|
17
6
|
const newObject = {
|
|
18
|
-
properties: keyOfValue.reduce((properties, key, index) => (
|
|
7
|
+
properties: keyOfValue.reduce((properties, key, index) => ({
|
|
8
|
+
...properties,
|
|
9
|
+
[key]: { const: valueOfValue[index] },
|
|
10
|
+
}), {}),
|
|
19
11
|
};
|
|
20
12
|
return deepOverlap(expression, newObject);
|
|
21
13
|
};
|
|
22
14
|
export function resolveOneOfValue(value) {
|
|
23
|
-
|
|
24
|
-
if (typeof value !== "boolean" && ((_a = value.enum) === null || _a === void 0 ? void 0 : _a[0]) !== undefined) {
|
|
15
|
+
if (typeof value !== "boolean" && value.enum?.[0] !== undefined) {
|
|
25
16
|
return value.enum[0];
|
|
26
17
|
}
|
|
27
18
|
else if (value === true || value === false) {
|
|
@@ -31,22 +22,18 @@ export function resolveOneOfValue(value) {
|
|
|
31
22
|
return undefined;
|
|
32
23
|
}
|
|
33
24
|
const resolveSelectedOneOfValue = (key, value, oneOf) => {
|
|
34
|
-
|
|
35
|
-
const update = ((_a = oneOf.find((one) => typeof one !== "boolean" && (one === null || one === void 0 ? void 0 : one.properties)
|
|
25
|
+
const update = (oneOf.find((one) => typeof one !== "boolean" && one?.properties
|
|
36
26
|
? resolveOneOfValue(one.properties[key]) === value[key]
|
|
37
|
-
: false)
|
|
38
|
-
const manipulatedUpdateValue = Object.keys(
|
|
27
|
+
: false) ?? {});
|
|
28
|
+
const manipulatedUpdateValue = Object.keys(update.properties ?? {})
|
|
39
29
|
.filter((objKey) => objKey !== key)
|
|
40
30
|
.reduce((newObj, innerKey) => {
|
|
41
|
-
|
|
42
|
-
newObj[innerKey] = (_a = update.properties) === null || _a === void 0 ? void 0 : _a[innerKey];
|
|
31
|
+
newObj[innerKey] = update.properties?.[innerKey];
|
|
43
32
|
return newObj;
|
|
44
33
|
}, {});
|
|
45
|
-
return
|
|
34
|
+
return { ...update, properties: manipulatedUpdateValue };
|
|
46
35
|
};
|
|
47
|
-
export const resolveSchema = (
|
|
48
|
-
var _b, _c;
|
|
49
|
-
var { allOf, dependencies } = _a, schema = __rest(_a, ["allOf", "dependencies"]);
|
|
36
|
+
export const resolveSchema = ({ allOf, dependencies, ...schema }, value) => {
|
|
50
37
|
let schemaCopy = schema;
|
|
51
38
|
let valuesToRemoveDeps = [];
|
|
52
39
|
let valuesToRemoveAllOf = [];
|
|
@@ -67,7 +54,7 @@ export const resolveSchema = (_a, value) => {
|
|
|
67
54
|
}, schemaCopy);
|
|
68
55
|
}
|
|
69
56
|
if (dependencies) {
|
|
70
|
-
const usedProperties = Object.keys(
|
|
57
|
+
const usedProperties = Object.keys(schemaCopy.properties ?? {});
|
|
71
58
|
Object.keys(value).map((key) => {
|
|
72
59
|
if (!usedProperties.includes(key)) {
|
|
73
60
|
valuesToRemoveDeps.push(key);
|
|
@@ -79,11 +66,10 @@ export const resolveSchema = (_a, value) => {
|
|
|
79
66
|
}
|
|
80
67
|
if (allOf) {
|
|
81
68
|
const { resolvedSchema, notAddedProperties } = allOf.reduce(({ resolvedSchema, notAddedProperties }, current) => {
|
|
82
|
-
var _a, _b;
|
|
83
69
|
if (!current || typeof current === "boolean") {
|
|
84
70
|
return { resolvedSchema, notAddedProperties };
|
|
85
71
|
}
|
|
86
|
-
const { if: expression, then, else: otherwise
|
|
72
|
+
const { if: expression, then, else: otherwise, ...resolvedSchemaLessConditional } = current;
|
|
87
73
|
if (!expression || typeof expression === "boolean") {
|
|
88
74
|
return {
|
|
89
75
|
resolvedSchema: mergeSchemas(resolvedSchema, resolvedSchemaLessConditional),
|
|
@@ -97,7 +83,7 @@ export const resolveSchema = (_a, value) => {
|
|
|
97
83
|
if (otherwise && typeof otherwise !== "boolean") {
|
|
98
84
|
notAddedProperties = [
|
|
99
85
|
...notAddedProperties,
|
|
100
|
-
...Object.keys(
|
|
86
|
+
...Object.keys(otherwise.properties ?? {}),
|
|
101
87
|
];
|
|
102
88
|
}
|
|
103
89
|
}
|
|
@@ -105,7 +91,7 @@ export const resolveSchema = (_a, value) => {
|
|
|
105
91
|
if (then && typeof then !== "boolean") {
|
|
106
92
|
notAddedProperties = [
|
|
107
93
|
...notAddedProperties,
|
|
108
|
-
...Object.keys(
|
|
94
|
+
...Object.keys(then.properties ?? {}),
|
|
109
95
|
];
|
|
110
96
|
}
|
|
111
97
|
if (otherwise && typeof otherwise !== "boolean") {
|
|
@@ -115,7 +101,7 @@ export const resolveSchema = (_a, value) => {
|
|
|
115
101
|
return { resolvedSchema, notAddedProperties };
|
|
116
102
|
}, { resolvedSchema: schemaCopy, notAddedProperties: [] });
|
|
117
103
|
//Values that do not exists due to conditions
|
|
118
|
-
const usedPropertiesAllOf = Object.keys(
|
|
104
|
+
const usedPropertiesAllOf = Object.keys(resolvedSchema.properties ?? {});
|
|
119
105
|
valuesToRemoveAllOf = [...new Set([...notAddedProperties])].filter((key) => !usedPropertiesAllOf.includes(key) && typeof value[key] !== "undefined");
|
|
120
106
|
const valuesToRemove = [
|
|
121
107
|
...new Set([...valuesToRemoveDeps, ...valuesToRemoveAllOf]),
|
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
import { mergeSchemas } from "./mergeSchemas";
|
|
2
2
|
export function solveReferences(schema, definitions, uiSchema, uiSchemaDefinitions) {
|
|
3
|
-
var _a, _b, _c, _d;
|
|
4
3
|
let solvedSchema = schema;
|
|
5
4
|
let solvedUiSchema = uiSchema;
|
|
6
5
|
if (typeof schema === "object" && schema["$ref"] && definitions) {
|
|
7
|
-
const schemaKey =
|
|
6
|
+
const schemaKey = schema["$ref"]?.substring(14) ?? "";
|
|
8
7
|
// TODO: Should we overwrite rather than merge? Maybe add a flag for choosing the behavior?
|
|
9
|
-
solvedSchema = mergeSchemas(
|
|
8
|
+
solvedSchema = mergeSchemas(definitions[schemaKey] ?? {}, schema ?? {});
|
|
10
9
|
delete solvedSchema.$ref;
|
|
11
|
-
solvedUiSchema = mergeSchemas(
|
|
10
|
+
solvedUiSchema = mergeSchemas(uiSchemaDefinitions?.[schemaKey] ?? {}, uiSchema ?? {});
|
|
12
11
|
}
|
|
13
12
|
return { schema: solvedSchema, uiSchema: solvedUiSchema };
|
|
14
13
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nolwenture/formik-jsonschema-form",
|
|
3
|
-
"version": "0.26.
|
|
3
|
+
"version": "0.26.3",
|
|
4
4
|
"description": "Formik form generation from json schema",
|
|
5
5
|
"author": "Antti Vikman <antti.vikman@nolwenture.com>",
|
|
6
6
|
"contributors": [
|
|
@@ -8,6 +8,14 @@
|
|
|
8
8
|
"Niko Lintunen <niko.lintunen@nolwenture.com>"
|
|
9
9
|
],
|
|
10
10
|
"license": "MIT",
|
|
11
|
+
"type": "module",
|
|
12
|
+
"main": "./dist/index.js",
|
|
13
|
+
"types": "./dist/index.d.ts",
|
|
14
|
+
"files": [
|
|
15
|
+
"dist",
|
|
16
|
+
"package.json",
|
|
17
|
+
"LICENSE"
|
|
18
|
+
],
|
|
11
19
|
"repository": {
|
|
12
20
|
"type": "git",
|
|
13
21
|
"url": "git+https://github.com/nolwenture/formik-jsonschema-form.git"
|
|
@@ -20,14 +28,6 @@
|
|
|
20
28
|
"formik",
|
|
21
29
|
"jsonschema"
|
|
22
30
|
],
|
|
23
|
-
"type": "commonjs",
|
|
24
|
-
"main": "./dist/index.js",
|
|
25
|
-
"types": "./dist/index.d.ts",
|
|
26
|
-
"files": [
|
|
27
|
-
"dist",
|
|
28
|
-
"package.json",
|
|
29
|
-
"LICENSE"
|
|
30
|
-
],
|
|
31
31
|
"scripts": {
|
|
32
32
|
"build": "tsc --build",
|
|
33
33
|
"clean": "tsc --build --clean",
|