@tinacms/schema-tools 1.7.1 → 1.7.2

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/index.js CHANGED
@@ -1945,7 +1945,7 @@
1945
1945
  }
1946
1946
  }
1947
1947
  };
1948
- this.walkFields = (cb) => {
1948
+ this.legacyWalkFields = (cb) => {
1949
1949
  const walk = (collectionOrObject, collection, path) => {
1950
1950
  if (collectionOrObject.templates) {
1951
1951
  collectionOrObject.templates.forEach((template) => {
@@ -1967,7 +1967,7 @@
1967
1967
  collections.forEach((collection) => walk(collection, collection, []));
1968
1968
  };
1969
1969
  this.schema = config;
1970
- this.walkFields(({ field, collection, path }) => {
1970
+ this.legacyWalkFields(({ field, collection }) => {
1971
1971
  if (!("searchable" in field)) {
1972
1972
  if (field.type === "image") {
1973
1973
  field.searchable = false;
@@ -1988,20 +1988,67 @@
1988
1988
  field.uid = field.uid || false;
1989
1989
  });
1990
1990
  }
1991
- findReferences(name2) {
1991
+ findReferencesFromCollection(name2) {
1992
1992
  const result = {};
1993
1993
  this.walkFields(({ field, collection: c, path }) => {
1994
+ if (c.name !== name2) {
1995
+ return;
1996
+ }
1994
1997
  if (field.type === "reference") {
1995
- if (field.collections.includes(name2)) {
1996
- if (result[c.name] === void 0) {
1997
- result[c.name] = [];
1998
+ field.collections.forEach((name22) => {
1999
+ if (result[name22] === void 0) {
2000
+ result[name22] = [];
1998
2001
  }
1999
- result[c.name].push({ path, field });
2000
- }
2002
+ result[name22].push(path);
2003
+ });
2001
2004
  }
2002
2005
  });
2003
2006
  return result;
2004
2007
  }
2008
+ /**
2009
+ * Walk all fields in tina schema
2010
+ *
2011
+ * @param cb callback function invoked for each field
2012
+ */
2013
+ walkFields(cb) {
2014
+ const walk = (collectionOrObject, collection, path = "$") => {
2015
+ if (collectionOrObject.templates) {
2016
+ collectionOrObject.templates.forEach((template) => {
2017
+ const templatePath = `${path}.${template.name}`;
2018
+ template.fields.forEach((field) => {
2019
+ const fieldPath = field.list ? `${templatePath}[*].${field.name}` : `${templatePath}.${field.name}`;
2020
+ cb({ field, collection, path: fieldPath });
2021
+ if (field.type === "object") {
2022
+ walk(field, collection, fieldPath);
2023
+ }
2024
+ });
2025
+ });
2026
+ }
2027
+ if (collectionOrObject.fields) {
2028
+ collectionOrObject.fields.forEach((field) => {
2029
+ const fieldPath = field.list ? `${path}.${field.name}[*]` : `${path}.${field.name}`;
2030
+ cb({ field, collection, path: fieldPath });
2031
+ if (field.type === "object" && field.fields) {
2032
+ walk(field, collection, fieldPath);
2033
+ } else if (field.templates) {
2034
+ field.templates.forEach((template) => {
2035
+ const templatePath = `${fieldPath}.${template.name}`;
2036
+ template.fields.forEach((field2) => {
2037
+ const fieldPath2 = field2.list ? `${templatePath}[*].${field2.name}` : `${templatePath}.${field2.name}`;
2038
+ cb({ field: field2, collection, path: fieldPath2 });
2039
+ if (field2.type === "object") {
2040
+ walk(field2, collection, fieldPath2);
2041
+ }
2042
+ });
2043
+ });
2044
+ }
2045
+ });
2046
+ }
2047
+ };
2048
+ this.getCollections().forEach((collection) => {
2049
+ walk(collection, collection);
2050
+ });
2051
+ }
2005
2052
  /**
2006
2053
  * This function returns an array of glob matches for a given collection.
2007
2054
  *
package/dist/index.mjs CHANGED
@@ -1927,7 +1927,7 @@ class TinaSchema {
1927
1927
  }
1928
1928
  }
1929
1929
  };
1930
- this.walkFields = (cb) => {
1930
+ this.legacyWalkFields = (cb) => {
1931
1931
  const walk = (collectionOrObject, collection, path) => {
1932
1932
  if (collectionOrObject.templates) {
1933
1933
  collectionOrObject.templates.forEach((template) => {
@@ -1949,7 +1949,7 @@ class TinaSchema {
1949
1949
  collections.forEach((collection) => walk(collection, collection, []));
1950
1950
  };
1951
1951
  this.schema = config;
1952
- this.walkFields(({ field, collection, path }) => {
1952
+ this.legacyWalkFields(({ field, collection }) => {
1953
1953
  if (!("searchable" in field)) {
1954
1954
  if (field.type === "image") {
1955
1955
  field.searchable = false;
@@ -1970,20 +1970,67 @@ class TinaSchema {
1970
1970
  field.uid = field.uid || false;
1971
1971
  });
1972
1972
  }
1973
- findReferences(name2) {
1973
+ findReferencesFromCollection(name2) {
1974
1974
  const result = {};
1975
1975
  this.walkFields(({ field, collection: c, path }) => {
1976
+ if (c.name !== name2) {
1977
+ return;
1978
+ }
1976
1979
  if (field.type === "reference") {
1977
- if (field.collections.includes(name2)) {
1978
- if (result[c.name] === void 0) {
1979
- result[c.name] = [];
1980
+ field.collections.forEach((name22) => {
1981
+ if (result[name22] === void 0) {
1982
+ result[name22] = [];
1980
1983
  }
1981
- result[c.name].push({ path, field });
1982
- }
1984
+ result[name22].push(path);
1985
+ });
1983
1986
  }
1984
1987
  });
1985
1988
  return result;
1986
1989
  }
1990
+ /**
1991
+ * Walk all fields in tina schema
1992
+ *
1993
+ * @param cb callback function invoked for each field
1994
+ */
1995
+ walkFields(cb) {
1996
+ const walk = (collectionOrObject, collection, path = "$") => {
1997
+ if (collectionOrObject.templates) {
1998
+ collectionOrObject.templates.forEach((template) => {
1999
+ const templatePath = `${path}.${template.name}`;
2000
+ template.fields.forEach((field) => {
2001
+ const fieldPath = field.list ? `${templatePath}[*].${field.name}` : `${templatePath}.${field.name}`;
2002
+ cb({ field, collection, path: fieldPath });
2003
+ if (field.type === "object") {
2004
+ walk(field, collection, fieldPath);
2005
+ }
2006
+ });
2007
+ });
2008
+ }
2009
+ if (collectionOrObject.fields) {
2010
+ collectionOrObject.fields.forEach((field) => {
2011
+ const fieldPath = field.list ? `${path}.${field.name}[*]` : `${path}.${field.name}`;
2012
+ cb({ field, collection, path: fieldPath });
2013
+ if (field.type === "object" && field.fields) {
2014
+ walk(field, collection, fieldPath);
2015
+ } else if (field.templates) {
2016
+ field.templates.forEach((template) => {
2017
+ const templatePath = `${fieldPath}.${template.name}`;
2018
+ template.fields.forEach((field2) => {
2019
+ const fieldPath2 = field2.list ? `${templatePath}[*].${field2.name}` : `${templatePath}.${field2.name}`;
2020
+ cb({ field: field2, collection, path: fieldPath2 });
2021
+ if (field2.type === "object") {
2022
+ walk(field2, collection, fieldPath2);
2023
+ }
2024
+ });
2025
+ });
2026
+ }
2027
+ });
2028
+ }
2029
+ };
2030
+ this.getCollections().forEach((collection) => {
2031
+ walk(collection, collection);
2032
+ });
2033
+ }
1987
2034
  /**
1988
2035
  * This function returns an array of glob matches for a given collection.
1989
2036
  *
@@ -28,10 +28,7 @@ export declare class TinaSchema {
28
28
  } & Schema);
29
29
  getIsTitleFieldName: (collection: string) => string;
30
30
  getCollectionsByName: (collectionNames: string[]) => Collection<true>[];
31
- findReferences(name: string): Record<string, {
32
- path: string[];
33
- field: TinaField;
34
- }[]>;
31
+ findReferencesFromCollection(name: string): Record<string, string[]>;
35
32
  getCollection: (collectionName: string) => Collection<true>;
36
33
  getCollections: () => Collection<true>[];
37
34
  getCollectionByFullPath: (filepath: string) => Collection<true>;
@@ -63,7 +60,26 @@ export declare class TinaSchema {
63
60
  *
64
61
  */
65
62
  getTemplatesForCollectable: (collection: Collectable) => CollectionTemplateable;
66
- walkFields: (cb: (args: {
63
+ /**
64
+ * Walk all fields in tina schema
65
+ *
66
+ * @param cb callback function invoked for each field
67
+ */
68
+ walkFields(cb: (args: {
69
+ field: any;
70
+ collection: any;
71
+ path: string;
72
+ isListItem?: boolean;
73
+ }) => void): void;
74
+ /**
75
+ * Walk all fields in Tina Schema
76
+ *
77
+ * This is a legacy version to preserve backwards compatibility for the tina generated schema. It does not
78
+ * traverse fields in object lists in rich-text templates.
79
+ *
80
+ * @param cb callback function invoked for each field
81
+ */
82
+ legacyWalkFields: (cb: (args: {
67
83
  field: TinaField;
68
84
  collection: Collection;
69
85
  path: string[];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tinacms/schema-tools",
3
- "version": "1.7.1",
3
+ "version": "1.7.2",
4
4
  "main": "dist/index.js",
5
5
  "module": "./dist/index.mjs",
6
6
  "exports": {
@@ -32,7 +32,7 @@
32
32
  "ts-jest": "^29.2.5",
33
33
  "typescript": "^5.7.3",
34
34
  "yup": "^0.32.11",
35
- "@tinacms/scripts": "1.3.2"
35
+ "@tinacms/scripts": "1.3.3"
36
36
  },
37
37
  "peerDependencies": {
38
38
  "react": ">=16.14.0",