@unito/integration-api 7.1.0 → 7.1.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.
@@ -1,4 +1,16 @@
1
1
  import * as Api from './types.js';
2
+ /**
3
+ * Options for type guard functions.
4
+ */
5
+ export interface GuardOptions {
6
+ /**
7
+ * When false, validate structure only (values are strings) without checking enum membership.
8
+ * This allows forward compatibility when a newer integration-api adds Semantics or FieldValueTypes
9
+ * that the consumer doesn't know about yet.
10
+ * Defaults to true.
11
+ */
12
+ strict?: boolean;
13
+ }
2
14
  /**
3
15
  * Checks if the input is a record<unknown, unknown>
4
16
  * @param potentialObject - The value to check.
@@ -40,31 +52,31 @@ export declare function isRelationPointer(potentialRelationPointer: unknown): po
40
52
  * @param potentialReferenceRelation - The value to check.
41
53
  * @returns True if the value is a ReferenceRelation, false otherwise.
42
54
  */
43
- export declare function isReferenceRelation(potentialReferenceRelation: unknown): potentialReferenceRelation is Api.ReferenceRelation;
55
+ export declare function isReferenceRelation(potentialReferenceRelation: unknown, options?: GuardOptions): potentialReferenceRelation is Api.ReferenceRelation;
44
56
  /**
45
57
  * Checks if the input is an Api.RelationSummary object.
46
58
  * @param potentialRelationSummary - The value to check.
47
59
  * @returns True if the value is a RelationSummary, false otherwise.
48
60
  */
49
- export declare function isRelationSummary(potentialRelationSummary: unknown): potentialRelationSummary is Api.RelationSummary;
61
+ export declare function isRelationSummary(potentialRelationSummary: unknown, options?: GuardOptions): potentialRelationSummary is Api.RelationSummary;
50
62
  /**
51
63
  * Checks if the input is an Api.Relation object.
52
64
  * @param potentialRelation - The value to check.
53
65
  * @returns True if the value is a Relation, false otherwise.
54
66
  */
55
- export declare function isRelation(potentialRelation: unknown): potentialRelation is Api.Relation;
67
+ export declare function isRelation(potentialRelation: unknown, options?: GuardOptions): potentialRelation is Api.Relation;
56
68
  /**
57
69
  * Checks if the input is an Api.RelationSchema object.
58
70
  * @param potentialRelationSchema - The value to check.
59
71
  * @returns True if the value is a RelationSchema, false otherwise.
60
72
  */
61
- export declare function isRelationSchema(potentialRelationSchema: unknown): potentialRelationSchema is Api.RelationSchema;
73
+ export declare function isRelationSchema(potentialRelationSchema: unknown, options?: GuardOptions): potentialRelationSchema is Api.RelationSchema;
62
74
  /**
63
75
  * Checks if the input is an Api.RelationSchema object or the string '__self'.
64
76
  * @param potentialRelationSchema - The value to check.
65
77
  * @returns True if the value is a RelationSchema or '__self', false otherwise.
66
78
  */
67
- export declare function isRelationSchemaOrSelf(potentialRelationSchemaOrSelf: unknown): potentialRelationSchemaOrSelf is Api.RelationSchema | '__self';
79
+ export declare function isRelationSchemaOrSelf(potentialRelationSchemaOrSelf: unknown, options?: GuardOptions): potentialRelationSchemaOrSelf is Api.RelationSchema | '__self';
68
80
  /**
69
81
  * Checks if the input is an Api.FieldValueType.
70
82
  * @param potentialFieldValueType - The value to check.
@@ -82,4 +94,4 @@ export declare function isSemantic(potentialSemantic: unknown): potentialSemanti
82
94
  * @param potentialFieldSchema - The value to check.
83
95
  * @returns True if the value is a FieldSchema, false otherwise.
84
96
  */
85
- export declare function isFieldSchema(potentialFieldSchema: unknown): potentialFieldSchema is Api.FieldSchema;
97
+ export declare function isFieldSchema(potentialFieldSchema: unknown, options?: GuardOptions): potentialFieldSchema is Api.FieldSchema;
@@ -44,7 +44,7 @@ export function isItem(potentialItem) {
44
44
  return (isObject(potentialItem) &&
45
45
  isObject(potentialItem['fields']) &&
46
46
  Array.isArray(potentialItem['relations']) &&
47
- potentialItem['relations'].every(isRelation));
47
+ potentialItem['relations'].every((r) => isRelation(r)));
48
48
  }
49
49
  /**
50
50
  * Checks if the input is an Api.RelationPointer object.
@@ -61,57 +61,68 @@ export function isRelationPointer(potentialRelationPointer) {
61
61
  * @param potentialReferenceRelation - The value to check.
62
62
  * @returns True if the value is a ReferenceRelation, false otherwise.
63
63
  */
64
- export function isReferenceRelation(potentialReferenceRelation) {
64
+ export function isReferenceRelation(potentialReferenceRelation, options) {
65
+ if (!isObject(potentialReferenceRelation)) {
66
+ return false;
67
+ }
68
+ // breaking change introducing the name in the reference.
69
+ if ('name' in potentialReferenceRelation) {
70
+ return (isObject(potentialReferenceRelation) &&
71
+ isString(potentialReferenceRelation['name']) &&
72
+ isString(potentialReferenceRelation['path']) &&
73
+ isString(potentialReferenceRelation['label']) &&
74
+ isRelationSchemaOrSelf(potentialReferenceRelation['schema'], options));
75
+ }
76
+ // backward compatible check
65
77
  return (isObject(potentialReferenceRelation) &&
66
- isString(potentialReferenceRelation['name']) &&
67
78
  isString(potentialReferenceRelation['path']) &&
68
79
  isString(potentialReferenceRelation['label']) &&
69
- isRelationSchemaOrSelf(potentialReferenceRelation['schema']));
80
+ isRelationSchemaOrSelf(potentialReferenceRelation['schema'], options));
70
81
  }
71
82
  /**
72
83
  * Checks if the input is an Api.RelationSummary object.
73
84
  * @param potentialRelationSummary - The value to check.
74
85
  * @returns True if the value is a RelationSummary, false otherwise.
75
86
  */
76
- export function isRelationSummary(potentialRelationSummary) {
87
+ export function isRelationSummary(potentialRelationSummary, options) {
77
88
  return (isObject(potentialRelationSummary) &&
78
89
  isString(potentialRelationSummary['name']) &&
79
90
  isString(potentialRelationSummary['label']) &&
80
- isRelationSchemaOrSelf(potentialRelationSummary['schema']));
91
+ isRelationSchemaOrSelf(potentialRelationSummary['schema'], options));
81
92
  }
82
93
  /**
83
94
  * Checks if the input is an Api.Relation object.
84
95
  * @param potentialRelation - The value to check.
85
96
  * @returns True if the value is a Relation, false otherwise.
86
97
  */
87
- export function isRelation(potentialRelation) {
98
+ export function isRelation(potentialRelation, options) {
88
99
  return (isObject(potentialRelation) &&
89
100
  isString(potentialRelation['name']) &&
90
101
  isString(potentialRelation['path']) &&
91
102
  isString(potentialRelation['label']) &&
92
- isRelationSchema(potentialRelation['schema']));
103
+ isRelationSchema(potentialRelation['schema'], options));
93
104
  }
94
105
  /**
95
106
  * Checks if the input is an Api.RelationSchema object.
96
107
  * @param potentialRelationSchema - The value to check.
97
108
  * @returns True if the value is a RelationSchema, false otherwise.
98
109
  */
99
- export function isRelationSchema(potentialRelationSchema) {
110
+ export function isRelationSchema(potentialRelationSchema, options) {
100
111
  return (isObject(potentialRelationSchema) &&
101
112
  isString(potentialRelationSchema['label']) &&
102
113
  Array.isArray(potentialRelationSchema['fields']) &&
103
- potentialRelationSchema['fields'].every(isFieldSchema) &&
114
+ potentialRelationSchema['fields'].every((f) => isFieldSchema(f, options)) &&
104
115
  (isUndefined(potentialRelationSchema['relations']) ||
105
116
  (Array.isArray(potentialRelationSchema['relations']) &&
106
- potentialRelationSchema['relations'].every(isRelationSummary))));
117
+ potentialRelationSchema['relations'].every((r) => isRelationSummary(r, options)))));
107
118
  }
108
119
  /**
109
120
  * Checks if the input is an Api.RelationSchema object or the string '__self'.
110
121
  * @param potentialRelationSchema - The value to check.
111
122
  * @returns True if the value is a RelationSchema or '__self', false otherwise.
112
123
  */
113
- export function isRelationSchemaOrSelf(potentialRelationSchemaOrSelf) {
114
- return isRelationSchema(potentialRelationSchemaOrSelf) || potentialRelationSchemaOrSelf === '__self';
124
+ export function isRelationSchemaOrSelf(potentialRelationSchemaOrSelf, options) {
125
+ return isRelationSchema(potentialRelationSchemaOrSelf, options) || potentialRelationSchemaOrSelf === '__self';
115
126
  }
116
127
  /**
117
128
  * Checks if the input is an Api.FieldValueType.
@@ -134,11 +145,13 @@ export function isSemantic(potentialSemantic) {
134
145
  * @param potentialFieldSchema - The value to check.
135
146
  * @returns True if the value is a FieldSchema, false otherwise.
136
147
  */
137
- export function isFieldSchema(potentialFieldSchema) {
148
+ export function isFieldSchema(potentialFieldSchema, options) {
149
+ const strict = options?.strict ?? true;
138
150
  return (isObject(potentialFieldSchema) &&
139
151
  isString(potentialFieldSchema['name']) &&
140
152
  isString(potentialFieldSchema['label']) &&
141
153
  isString(potentialFieldSchema['type']) &&
142
- isFieldValueType(potentialFieldSchema['type']) &&
143
- (isUndefined(potentialFieldSchema['semantic']) || isSemantic(potentialFieldSchema['semantic'])));
154
+ (strict ? isFieldValueType(potentialFieldSchema['type']) : true) &&
155
+ (isUndefined(potentialFieldSchema['semantic']) ||
156
+ (strict ? isSemantic(potentialFieldSchema['semantic']) : isString(potentialFieldSchema['semantic']))));
144
157
  }
@@ -161,7 +161,7 @@ function isItem(potentialItem) {
161
161
  return (isObject(potentialItem) &&
162
162
  isObject(potentialItem['fields']) &&
163
163
  Array.isArray(potentialItem['relations']) &&
164
- potentialItem['relations'].every(isRelation));
164
+ potentialItem['relations'].every((r) => isRelation(r)));
165
165
  }
166
166
  /**
167
167
  * Checks if the input is an Api.RelationPointer object.
@@ -178,57 +178,68 @@ function isRelationPointer(potentialRelationPointer) {
178
178
  * @param potentialReferenceRelation - The value to check.
179
179
  * @returns True if the value is a ReferenceRelation, false otherwise.
180
180
  */
181
- function isReferenceRelation(potentialReferenceRelation) {
181
+ function isReferenceRelation(potentialReferenceRelation, options) {
182
+ if (!isObject(potentialReferenceRelation)) {
183
+ return false;
184
+ }
185
+ // breaking change introducing the name in the reference.
186
+ if ('name' in potentialReferenceRelation) {
187
+ return (isObject(potentialReferenceRelation) &&
188
+ isString(potentialReferenceRelation['name']) &&
189
+ isString(potentialReferenceRelation['path']) &&
190
+ isString(potentialReferenceRelation['label']) &&
191
+ isRelationSchemaOrSelf(potentialReferenceRelation['schema'], options));
192
+ }
193
+ // backward compatible check
182
194
  return (isObject(potentialReferenceRelation) &&
183
- isString(potentialReferenceRelation['name']) &&
184
195
  isString(potentialReferenceRelation['path']) &&
185
196
  isString(potentialReferenceRelation['label']) &&
186
- isRelationSchemaOrSelf(potentialReferenceRelation['schema']));
197
+ isRelationSchemaOrSelf(potentialReferenceRelation['schema'], options));
187
198
  }
188
199
  /**
189
200
  * Checks if the input is an Api.RelationSummary object.
190
201
  * @param potentialRelationSummary - The value to check.
191
202
  * @returns True if the value is a RelationSummary, false otherwise.
192
203
  */
193
- function isRelationSummary(potentialRelationSummary) {
204
+ function isRelationSummary(potentialRelationSummary, options) {
194
205
  return (isObject(potentialRelationSummary) &&
195
206
  isString(potentialRelationSummary['name']) &&
196
207
  isString(potentialRelationSummary['label']) &&
197
- isRelationSchemaOrSelf(potentialRelationSummary['schema']));
208
+ isRelationSchemaOrSelf(potentialRelationSummary['schema'], options));
198
209
  }
199
210
  /**
200
211
  * Checks if the input is an Api.Relation object.
201
212
  * @param potentialRelation - The value to check.
202
213
  * @returns True if the value is a Relation, false otherwise.
203
214
  */
204
- function isRelation(potentialRelation) {
215
+ function isRelation(potentialRelation, options) {
205
216
  return (isObject(potentialRelation) &&
206
217
  isString(potentialRelation['name']) &&
207
218
  isString(potentialRelation['path']) &&
208
219
  isString(potentialRelation['label']) &&
209
- isRelationSchema(potentialRelation['schema']));
220
+ isRelationSchema(potentialRelation['schema'], options));
210
221
  }
211
222
  /**
212
223
  * Checks if the input is an Api.RelationSchema object.
213
224
  * @param potentialRelationSchema - The value to check.
214
225
  * @returns True if the value is a RelationSchema, false otherwise.
215
226
  */
216
- function isRelationSchema(potentialRelationSchema) {
227
+ function isRelationSchema(potentialRelationSchema, options) {
217
228
  return (isObject(potentialRelationSchema) &&
218
229
  isString(potentialRelationSchema['label']) &&
219
230
  Array.isArray(potentialRelationSchema['fields']) &&
220
- potentialRelationSchema['fields'].every(isFieldSchema) &&
231
+ potentialRelationSchema['fields'].every((f) => isFieldSchema(f, options)) &&
221
232
  (isUndefined(potentialRelationSchema['relations']) ||
222
233
  (Array.isArray(potentialRelationSchema['relations']) &&
223
- potentialRelationSchema['relations'].every(isRelationSummary))));
234
+ potentialRelationSchema['relations'].every((r) => isRelationSummary(r, options)))));
224
235
  }
225
236
  /**
226
237
  * Checks if the input is an Api.RelationSchema object or the string '__self'.
227
238
  * @param potentialRelationSchema - The value to check.
228
239
  * @returns True if the value is a RelationSchema or '__self', false otherwise.
229
240
  */
230
- function isRelationSchemaOrSelf(potentialRelationSchemaOrSelf) {
231
- return isRelationSchema(potentialRelationSchemaOrSelf) || potentialRelationSchemaOrSelf === '__self';
241
+ function isRelationSchemaOrSelf(potentialRelationSchemaOrSelf, options) {
242
+ return isRelationSchema(potentialRelationSchemaOrSelf, options) || potentialRelationSchemaOrSelf === '__self';
232
243
  }
233
244
  /**
234
245
  * Checks if the input is an Api.FieldValueType.
@@ -251,13 +262,15 @@ function isSemantic(potentialSemantic) {
251
262
  * @param potentialFieldSchema - The value to check.
252
263
  * @returns True if the value is a FieldSchema, false otherwise.
253
264
  */
254
- function isFieldSchema(potentialFieldSchema) {
265
+ function isFieldSchema(potentialFieldSchema, options) {
266
+ const strict = options?.strict ?? true;
255
267
  return (isObject(potentialFieldSchema) &&
256
268
  isString(potentialFieldSchema['name']) &&
257
269
  isString(potentialFieldSchema['label']) &&
258
270
  isString(potentialFieldSchema['type']) &&
259
- isFieldValueType(potentialFieldSchema['type']) &&
260
- (isUndefined(potentialFieldSchema['semantic']) || isSemantic(potentialFieldSchema['semantic'])));
271
+ (strict ? isFieldValueType(potentialFieldSchema['type']) : true) &&
272
+ (isUndefined(potentialFieldSchema['semantic']) ||
273
+ (strict ? isSemantic(potentialFieldSchema['semantic']) : isString(potentialFieldSchema['semantic']))));
261
274
  }
262
275
 
263
276
  const referenceToStringLikeConfiguration = {
@@ -672,15 +685,15 @@ function findRelationByJSONPath(item, query) {
672
685
  current = previousSchema;
673
686
  }
674
687
  const result = applyToken(current, token);
675
- if (isObject(result) && isRelationSchema(result['schema'])) {
688
+ if (isObject(result) && isRelationSchema(result['schema'], { strict: false })) {
676
689
  schemas.push(result['schema']);
677
690
  }
678
691
  current = result;
679
692
  }
680
- if (isRelation(current)) {
693
+ if (isRelation(current, { strict: false })) {
681
694
  return current;
682
695
  }
683
- if (isReferenceRelation(current) || isRelationSummary(current)) {
696
+ if (isReferenceRelation(current, { strict: false }) || isRelationSummary(current, { strict: false })) {
684
697
  const latestSchema = schemas[schemas.length - 1];
685
698
  if (latestSchema === undefined) {
686
699
  throw new Error(`No schema found for relation ${current.label}`);
@@ -15,15 +15,15 @@ export function findRelationByJSONPath(item, query) {
15
15
  current = previousSchema;
16
16
  }
17
17
  const result = applyToken(current, token);
18
- if (Guards.isObject(result) && Guards.isRelationSchema(result['schema'])) {
18
+ if (Guards.isObject(result) && Guards.isRelationSchema(result['schema'], { strict: false })) {
19
19
  schemas.push(result['schema']);
20
20
  }
21
21
  current = result;
22
22
  }
23
- if (Guards.isRelation(current)) {
23
+ if (Guards.isRelation(current, { strict: false })) {
24
24
  return current;
25
25
  }
26
- if (Guards.isReferenceRelation(current) || Guards.isRelationSummary(current)) {
26
+ if (Guards.isReferenceRelation(current, { strict: false }) || Guards.isRelationSummary(current, { strict: false })) {
27
27
  const latestSchema = schemas[schemas.length - 1];
28
28
  if (latestSchema === undefined) {
29
29
  throw new Error(`No schema found for relation ${current.label}`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@unito/integration-api",
3
- "version": "7.1.0",
3
+ "version": "7.1.2",
4
4
  "description": "The Unito Integration API",
5
5
  "type": "module",
6
6
  "types": "./dist/src/index.d.ts",