@nolwenture/formik-jsonschema-form 0.15.0 → 0.16.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 CHANGED
@@ -786,6 +786,8 @@ Schemas can be referenced. A referenced schema can be given to a property using
786
786
 
787
787
  In order to give uiSchema to reused or recursive fields defined in `schema.definitons`, the uiSchema needs to be given to the matching prop of `definitions`-object. This way, the given uiProps are added to the referencing field, no matter how deeply nested it is inside the schema.
788
788
 
789
+ If certain aspects of a reused field need to be adapted, `uiSchema.definitions` of that field can be overridden inside `uiSchema` by giving the desired properties to the respective field key. In the following schema/uiSchema `petBirthDate` can be used as an example. The settings are overriding the title of the field from the original "When is your birthday?" to "When is the birthday of your pet?".
790
+
789
791
  ```tsx
790
792
  const familyObjectField: Schema = {
791
793
  type: "object",
@@ -822,6 +824,8 @@ const refDefSchema: Schema = {
822
824
  },
823
825
  },
824
826
  },
827
+ pet: { type: "string", title: "Pet name" },
828
+ petBirthDate: { $ref: "#/definitions/birthDate" },
825
829
  },
826
830
 
827
831
  definitions: {
@@ -846,6 +850,7 @@ const refDefSchema: Schema = {
846
850
  };
847
851
 
848
852
  const refDefUiSchema: UiSchema = {
853
+ petBirthDate: { "ui:title": "When is the birthday of your pet?" },
849
854
  definitions: {
850
855
  birthDate: { "ui:field": "DateField" },
851
856
  names: {
@@ -98,7 +98,10 @@ export const ArrayField = (props) => {
98
98
  // Backwards compatibility ends
99
99
  const { disabled } = useExtractUiSchema(uiSchema);
100
100
  const valueArray = Array.isArray(value) ? value : undefined;
101
- return schema.anyOf !== undefined ? (_jsx(FieldTemplate, Object.assign({}, props, { children: _jsx(FieldInput, Object.assign({ type }, inputProps, { name: name, formikAs: "select", multiple: (enums === null || enums === void 0 ? void 0 : enums.type) === "multiSelect", children: enums === null || enums === void 0 ? void 0 : enums.values.map((option, index) => (_jsx("option", { value: option.const, children: option.title }, index))) })) }))) : (_jsxs(ArrayFieldTemplate, Object.assign({}, props, { children: [_jsxs(ArrayFieldList, { children: [(items || []).map((itemSchema, index) => (_jsx(ArrayFieldItem, { children: _jsx(SchemaField, { name: `${name}[${index}]`, schema: typeof itemSchema !== "boolean" ? itemSchema : {}, uiSchema: (uiSchema === null || uiSchema === void 0 ? void 0 : uiSchema[index]) || (uiSchema === null || uiSchema === void 0 ? void 0 : uiSchema.items), forceDefaultValue: true }) }, index))), ((valueArray === null || valueArray === void 0 ? void 0 : valueArray.slice(items.length)) || []).map((_, index) => {
101
+ return schema.anyOf !== undefined ? (_jsx(FieldTemplate, Object.assign({}, props, { children: _jsx(FieldInput, Object.assign({ type }, inputProps, { name: name, formikAs: "select", multiple: (enums === null || enums === void 0 ? void 0 : enums.type) === "multiSelect", children: enums === null || enums === void 0 ? void 0 : enums.values.map((option, index) => (_jsx("option", { value: option.const, children: option.title }, index))) })) }))) : (_jsxs(ArrayFieldTemplate, Object.assign({}, props, { children: [_jsxs(ArrayFieldList, { children: [(items || []).map((itemSchema, index) => (_jsx(ArrayFieldItem, { children: _jsx(SchemaField, { name: `${name}[${index}]`, schema: typeof itemSchema !== "boolean" ? itemSchema : {}, uiSchema: (uiSchema === null || uiSchema === void 0 ? void 0 : uiSchema[index]) || (uiSchema === null || uiSchema === void 0 ? void 0 : uiSchema.items),
102
+ // "true" will prevent validation for required array fields
103
+ // since a value always exists in the array
104
+ forceDefaultValue: false }) }, index))), ((valueArray === null || valueArray === void 0 ? void 0 : valueArray.slice(items.length)) || []).map((_, index) => {
102
105
  var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
103
106
  return (_jsxs(ArrayFieldItem, { children: [_jsx(SchemaField, { name: `${name}[${index + items.length}]`, schema: additionalItems && typeof additionalItems !== "boolean"
104
107
  ? additionalItems
@@ -45,7 +45,10 @@ export const refDefSchema = {
45
45
  $ref: "#/definitions/age",
46
46
  },
47
47
  birthDate: {
48
- $ref: "#/definitions/birthDate",
48
+ $ref: "#/definitions/someDate",
49
+ },
50
+ specialDate: {
51
+ $ref: "#/definitions/someDate",
49
52
  },
50
53
  creditCard: {
51
54
  type: "number",
@@ -243,7 +246,7 @@ export const refDefSchema = {
243
246
  },
244
247
  },
245
248
  age: ageFieldDefinition,
246
- birthDate: { type: "string", title: "When is your birthday?" },
249
+ someDate: { type: "string", title: "When is your birthday?" },
247
250
  middleName: { type: "string", title: " Middle name" },
248
251
  },
249
252
  };
@@ -252,6 +255,7 @@ export const refDefUiSchema = {
252
255
  "names",
253
256
  "age",
254
257
  "birthDate",
258
+ "specialDate",
255
259
  "family",
256
260
  "creditCard",
257
261
  "billing_address",
@@ -271,8 +275,13 @@ export const refDefUiSchema = {
271
275
  },
272
276
  },
273
277
  feedback: { "ui:field": "TextAreaField" },
278
+ specialDate: {
279
+ "ui:title": "A day to remember",
280
+ },
274
281
  definitions: {
275
- birthDate: { "ui:field": "DateField" },
282
+ someDate: {
283
+ "ui:field": "DateField",
284
+ },
276
285
  names: {
277
286
  history: { "ui:field": "TextAreaField" },
278
287
  middleNames: { "ui:info": { content: "You can leave those blank" } },
@@ -13,8 +13,8 @@ describe("solveReferences", () => {
13
13
  });
14
14
  });
15
15
  it("should resolve simple references with uiSchema correctly", () => {
16
- var _a;
17
- const schemaSolvedForReferences = solveReferences((_a = refDefSchema === null || refDefSchema === void 0 ? void 0 : refDefSchema.properties) === null || _a === void 0 ? void 0 : _a.birthday, refDefSchema.definitions, refDefUiSchema, refDefUiSchema.definitions);
16
+ var _a, _b;
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);
18
18
  expect(schemaSolvedForReferences).toEqual({
19
19
  schema: {
20
20
  type: "string",
@@ -23,4 +23,18 @@ describe("solveReferences", () => {
23
23
  uiSchema: { "ui:field": "DateField" },
24
24
  });
25
25
  });
26
+ it("should override uiSchema settings when key of field is also given outside definitions object of uiSchema", () => {
27
+ var _a, _b;
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);
29
+ expect(schemaSolvedForReferences).toEqual({
30
+ schema: {
31
+ type: "string",
32
+ title: "Birthday", //this is the original title that comes from the definitions, but is getting overridden
33
+ },
34
+ uiSchema: {
35
+ "ui:field": "DateField",
36
+ "ui:title": "The birthday of your best friend", //this is the title that will be displayed as field title
37
+ },
38
+ });
39
+ });
26
40
  });
@@ -9,6 +9,9 @@ export const refDefSchema = {
9
9
  birthday: {
10
10
  $ref: "#/definitions/birthday",
11
11
  },
12
+ friendBirthday: {
13
+ $ref: "#/definitions/birthday",
14
+ },
12
15
  },
13
16
  definitions: {
14
17
  name: { type: "string", title: "Name" },
@@ -16,6 +19,7 @@ export const refDefSchema = {
16
19
  },
17
20
  };
18
21
  export const refDefUiSchema = {
22
+ friendBirthday: { "ui:title": "The birthday of your best friend" },
19
23
  definitions: {
20
24
  birthday: { "ui:field": "DateField" },
21
25
  },
@@ -34,7 +34,14 @@ export function createValidationSchema(schema, definitions = {}) {
34
34
  throw new Error("Unsupported schema type");
35
35
  }
36
36
  if (requiredFields !== undefined && requiredFields.includes(key)) {
37
- yupType = yupType["required"]("Required");
37
+ if (type === "array") {
38
+ yupType = yupType
39
+ .min(1, "Required")
40
+ .required("Required");
41
+ }
42
+ else {
43
+ yupType = yupType["required"]("Required");
44
+ }
38
45
  }
39
46
  return yupType;
40
47
  }
@@ -1,3 +1,4 @@
1
+ import { mergeSchemas } from "./mergeSchemas";
1
2
  export function solveReferences(schema, definitions, uiSchema, uiSchemaDefinitions) {
2
3
  var _a, _b, _c;
3
4
  let solvedSchema = schema;
@@ -5,7 +6,7 @@ export function solveReferences(schema, definitions, uiSchema, uiSchemaDefinitio
5
6
  if (typeof schema === "object" && schema["$ref"] && definitions) {
6
7
  const schemaKey = (_b = (_a = schema["$ref"]) === null || _a === void 0 ? void 0 : _a.substring(14)) !== null && _b !== void 0 ? _b : "";
7
8
  solvedSchema = definitions[schemaKey];
8
- solvedUiSchema = (_c = uiSchemaDefinitions === null || uiSchemaDefinitions === void 0 ? void 0 : uiSchemaDefinitions[schemaKey]) !== null && _c !== void 0 ? _c : {};
9
+ solvedUiSchema = mergeSchemas((_c = uiSchemaDefinitions === null || uiSchemaDefinitions === void 0 ? void 0 : uiSchemaDefinitions[schemaKey]) !== null && _c !== void 0 ? _c : {}, uiSchema !== null && uiSchema !== void 0 ? uiSchema : {});
9
10
  }
10
11
  return { schema: solvedSchema, uiSchema: solvedUiSchema };
11
12
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nolwenture/formik-jsonschema-form",
3
- "version": "0.15.0",
3
+ "version": "0.16.0",
4
4
  "description": "Formik form generation from json schema",
5
5
  "author": "Antti Vikman <antti.vikman@nolwenture.com>",
6
6
  "contributors": [