@nolwenture/formik-jsonschema-form 0.16.0 → 0.17.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,7 +786,56 @@ 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?".
789
+ The schema of the field and the referenced schema are merged, meaning that aspects of the reused schema can be adapted:
790
+
791
+ ```tsx
792
+ const schema: Schema = {
793
+ type: "object",
794
+ properties: {
795
+ address: {
796
+ $ref: "#/definitions/address",
797
+ required: ["postcode"],
798
+ default: { country: "US" },
799
+ properties: {
800
+ apartmentNumber: { type: "string", title: "Apartment number" },
801
+ },
802
+ },
803
+ },
804
+ definitions: {
805
+ address: {
806
+ type: "object",
807
+ title: "Address",
808
+ required: ["country", "city", "street"],
809
+ properties: {
810
+ country: { type: "string", title: "Country" },
811
+ city: { type: "string", title: "City" },
812
+ street: { type: "string", title: "Street" },
813
+ postcode: { type: "string", title: "Postcode" },
814
+ },
815
+ },
816
+ },
817
+ };
818
+ ```
819
+
820
+ The resulting schema of the `address` field:
821
+
822
+ ```tsx
823
+ {
824
+ type: "object",
825
+ title: "Address",
826
+ required: ["country", "city", "street", "postcode"],
827
+ default: { country: "US" },
828
+ properties: {
829
+ country: { type: "string", title: "Country" },
830
+ city: { type: "string", title: "City" },
831
+ street: { type: "string", title: "Street" },
832
+ postcode: { type: "string", title: "Postcode" },
833
+ apartmentNumber: { type: "string", title: "Apartment number" },
834
+ },
835
+ }
836
+ ```
837
+
838
+ If aspects of the `uiSchema` 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
839
 
791
840
  ```tsx
792
841
  const familyObjectField: Schema = {
@@ -14,6 +14,7 @@ export const refDefSchema = {
14
14
  properties: {
15
15
  names: {
16
16
  $ref: "#/definitions/names",
17
+ required: ["firstName", "lastName"],
17
18
  },
18
19
  family: {
19
20
  type: "object",
@@ -37,4 +37,24 @@ describe("solveReferences", () => {
37
37
  },
38
38
  });
39
39
  });
40
+ it("should merge field and definition schema", () => {
41
+ var _a;
42
+ const schemaSolvedForReferences = solveReferences((_a = refDefSchema === null || refDefSchema === void 0 ? void 0 : refDefSchema.properties) === null || _a === void 0 ? void 0 : _a.address, refDefSchema.definitions, {}, {});
43
+ expect(schemaSolvedForReferences).toEqual({
44
+ schema: {
45
+ type: "object",
46
+ title: "Address",
47
+ required: ["country", "city", "street", "postcode"],
48
+ default: { country: "US" },
49
+ properties: {
50
+ country: { type: "string", title: "Country" },
51
+ city: { type: "string", title: "City" },
52
+ street: { type: "string", title: "Street" },
53
+ postcode: { type: "string", title: "Postcode" },
54
+ apartmentNumber: { type: "string", title: "Apartment number" },
55
+ },
56
+ },
57
+ uiSchema: {},
58
+ });
59
+ });
40
60
  });
@@ -12,10 +12,29 @@ export const refDefSchema = {
12
12
  friendBirthday: {
13
13
  $ref: "#/definitions/birthday",
14
14
  },
15
+ address: {
16
+ $ref: "#/definitions/address",
17
+ required: ["postcode"],
18
+ default: { country: "US" },
19
+ properties: {
20
+ apartmentNumber: { type: "string", title: "Apartment number" },
21
+ },
22
+ },
15
23
  },
16
24
  definitions: {
17
25
  name: { type: "string", title: "Name" },
18
26
  birthday: { type: "string", title: "Birthday" },
27
+ address: {
28
+ type: "object",
29
+ title: "Address",
30
+ required: ["country", "city", "street"],
31
+ properties: {
32
+ country: { type: "string", title: "Country" },
33
+ city: { type: "string", title: "City" },
34
+ street: { type: "string", title: "Street" },
35
+ postcode: { type: "string", title: "Postcode" },
36
+ },
37
+ },
19
38
  },
20
39
  };
21
40
  export const refDefUiSchema = {
@@ -1,12 +1,14 @@
1
1
  import { mergeSchemas } from "./mergeSchemas";
2
2
  export function solveReferences(schema, definitions, uiSchema, uiSchemaDefinitions) {
3
- var _a, _b, _c;
3
+ var _a, _b, _c, _d;
4
4
  let solvedSchema = schema;
5
5
  let solvedUiSchema = uiSchema;
6
6
  if (typeof schema === "object" && schema["$ref"] && definitions) {
7
7
  const schemaKey = (_b = (_a = schema["$ref"]) === null || _a === void 0 ? void 0 : _a.substring(14)) !== null && _b !== void 0 ? _b : "";
8
- solvedSchema = definitions[schemaKey];
9
- solvedUiSchema = mergeSchemas((_c = uiSchemaDefinitions === null || uiSchemaDefinitions === void 0 ? void 0 : uiSchemaDefinitions[schemaKey]) !== null && _c !== void 0 ? _c : {}, uiSchema !== null && uiSchema !== void 0 ? uiSchema : {});
8
+ // TODO: Should we overwrite rather than merge? Maybe add a flag for choosing the behavior?
9
+ solvedSchema = mergeSchemas((_c = definitions[schemaKey]) !== null && _c !== void 0 ? _c : {}, schema !== null && schema !== void 0 ? schema : {});
10
+ delete solvedSchema.$ref;
11
+ solvedUiSchema = mergeSchemas((_d = uiSchemaDefinitions === null || uiSchemaDefinitions === void 0 ? void 0 : uiSchemaDefinitions[schemaKey]) !== null && _d !== void 0 ? _d : {}, uiSchema !== null && uiSchema !== void 0 ? uiSchema : {});
10
12
  }
11
13
  return { schema: solvedSchema, uiSchema: solvedUiSchema };
12
14
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nolwenture/formik-jsonschema-form",
3
- "version": "0.16.0",
3
+ "version": "0.17.0",
4
4
  "description": "Formik form generation from json schema",
5
5
  "author": "Antti Vikman <antti.vikman@nolwenture.com>",
6
6
  "contributors": [