@twin.org/data-core 0.0.1-next.32 → 0.0.1-next.33

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.
@@ -49,7 +49,11 @@ const ValidationMode = {
49
49
  /**
50
50
  * Use either validation mode.
51
51
  */
52
- Either: "either"
52
+ Either: "either",
53
+ /**
54
+ * Use both validation modes.
55
+ */
56
+ Both: "both"
53
57
  };
54
58
 
55
59
  // Copyright 2024 IOTA Stiftung.
@@ -209,23 +213,30 @@ class DataTypeHelper {
209
213
  * @param dataType The data type to validate.
210
214
  * @param data The data to validate.
211
215
  * @param validationFailures The list of validation failures to add to.
212
- * @param validationMode The validation mode to use, defaults to either.
216
+ * @param options Optional options for validation.
217
+ * @param options.failOnMissingType If true, will fail validation if the data type is missing, defaults to false.
218
+ * @param options.validationMode The validation mode to use, defaults to either.
213
219
  * @returns True if the data was valid.
214
220
  */
215
- static async validate(propertyName, dataType, data, validationFailures, validationMode) {
221
+ static async validate(propertyName, dataType, data, validationFailures, options) {
222
+ let isValid = true;
216
223
  if (core.Is.stringValue(dataType)) {
217
224
  const handler = DataTypeHandlerFactory.getIfExists(dataType);
218
225
  if (handler) {
219
- validationMode = validationMode ?? ValidationMode.Either;
226
+ const validationMode = options?.validationMode ?? ValidationMode.Either;
220
227
  // If we have a validate function use that as it is more specific
221
228
  // and will produce better error messages
229
+ let hasValidated = false;
222
230
  if ((validationMode === ValidationMode.Validate ||
231
+ validationMode === ValidationMode.Both ||
223
232
  validationMode === ValidationMode.Either) &&
224
233
  core.Is.function(handler.validate)) {
225
- return handler.validate(propertyName, data, validationFailures);
234
+ isValid = await handler.validate(propertyName, data, validationFailures);
235
+ hasValidated = true;
226
236
  }
227
- else if ((validationMode === ValidationMode.JsonSchema ||
228
- validationMode === ValidationMode.Either) &&
237
+ if ((validationMode === ValidationMode.JsonSchema ||
238
+ (validationMode === ValidationMode.Either && !hasValidated) ||
239
+ validationMode === ValidationMode.Both) &&
229
240
  core.Is.function(handler.jsonSchema)) {
230
241
  // Otherwise use the JSON schema if there is one
231
242
  const schema = await handler.jsonSchema();
@@ -242,13 +253,25 @@ class DataTypeHelper {
242
253
  }
243
254
  });
244
255
  }
245
- return validationResult.result;
256
+ if (!validationResult.result) {
257
+ isValid = false;
258
+ }
246
259
  }
247
260
  }
248
261
  }
262
+ else if (options?.failOnMissingType ?? false) {
263
+ // If we don't have a handler for a specific type and we are failing on missing type
264
+ validationFailures.push({
265
+ property: propertyName,
266
+ reason: "validation.schema.missingType",
267
+ properties: {
268
+ dataType
269
+ }
270
+ });
271
+ isValid = false;
272
+ }
249
273
  }
250
- // Return true by default if no other mechanism for validation is available
251
- return true;
274
+ return isValid;
252
275
  }
253
276
  }
254
277
 
@@ -47,7 +47,11 @@ const ValidationMode = {
47
47
  /**
48
48
  * Use either validation mode.
49
49
  */
50
- Either: "either"
50
+ Either: "either",
51
+ /**
52
+ * Use both validation modes.
53
+ */
54
+ Both: "both"
51
55
  };
52
56
 
53
57
  // Copyright 2024 IOTA Stiftung.
@@ -207,23 +211,30 @@ class DataTypeHelper {
207
211
  * @param dataType The data type to validate.
208
212
  * @param data The data to validate.
209
213
  * @param validationFailures The list of validation failures to add to.
210
- * @param validationMode The validation mode to use, defaults to either.
214
+ * @param options Optional options for validation.
215
+ * @param options.failOnMissingType If true, will fail validation if the data type is missing, defaults to false.
216
+ * @param options.validationMode The validation mode to use, defaults to either.
211
217
  * @returns True if the data was valid.
212
218
  */
213
- static async validate(propertyName, dataType, data, validationFailures, validationMode) {
219
+ static async validate(propertyName, dataType, data, validationFailures, options) {
220
+ let isValid = true;
214
221
  if (Is.stringValue(dataType)) {
215
222
  const handler = DataTypeHandlerFactory.getIfExists(dataType);
216
223
  if (handler) {
217
- validationMode = validationMode ?? ValidationMode.Either;
224
+ const validationMode = options?.validationMode ?? ValidationMode.Either;
218
225
  // If we have a validate function use that as it is more specific
219
226
  // and will produce better error messages
227
+ let hasValidated = false;
220
228
  if ((validationMode === ValidationMode.Validate ||
229
+ validationMode === ValidationMode.Both ||
221
230
  validationMode === ValidationMode.Either) &&
222
231
  Is.function(handler.validate)) {
223
- return handler.validate(propertyName, data, validationFailures);
232
+ isValid = await handler.validate(propertyName, data, validationFailures);
233
+ hasValidated = true;
224
234
  }
225
- else if ((validationMode === ValidationMode.JsonSchema ||
226
- validationMode === ValidationMode.Either) &&
235
+ if ((validationMode === ValidationMode.JsonSchema ||
236
+ (validationMode === ValidationMode.Either && !hasValidated) ||
237
+ validationMode === ValidationMode.Both) &&
227
238
  Is.function(handler.jsonSchema)) {
228
239
  // Otherwise use the JSON schema if there is one
229
240
  const schema = await handler.jsonSchema();
@@ -240,13 +251,25 @@ class DataTypeHelper {
240
251
  }
241
252
  });
242
253
  }
243
- return validationResult.result;
254
+ if (!validationResult.result) {
255
+ isValid = false;
256
+ }
244
257
  }
245
258
  }
246
259
  }
260
+ else if (options?.failOnMissingType ?? false) {
261
+ // If we don't have a handler for a specific type and we are failing on missing type
262
+ validationFailures.push({
263
+ property: propertyName,
264
+ reason: "validation.schema.missingType",
265
+ properties: {
266
+ dataType
267
+ }
268
+ });
269
+ isValid = false;
270
+ }
247
271
  }
248
- // Return true by default if no other mechanism for validation is available
249
- return true;
272
+ return isValid;
250
273
  }
251
274
  }
252
275
 
@@ -14,6 +14,10 @@ export declare const ValidationMode: {
14
14
  * Use either validation mode.
15
15
  */
16
16
  readonly Either: "either";
17
+ /**
18
+ * Use both validation modes.
19
+ */
20
+ readonly Both: "both";
17
21
  };
18
22
  /**
19
23
  * Validation modes for validating data types.
@@ -10,8 +10,13 @@ export declare class DataTypeHelper {
10
10
  * @param dataType The data type to validate.
11
11
  * @param data The data to validate.
12
12
  * @param validationFailures The list of validation failures to add to.
13
- * @param validationMode The validation mode to use, defaults to either.
13
+ * @param options Optional options for validation.
14
+ * @param options.failOnMissingType If true, will fail validation if the data type is missing, defaults to false.
15
+ * @param options.validationMode The validation mode to use, defaults to either.
14
16
  * @returns True if the data was valid.
15
17
  */
16
- static validate(propertyName: string, dataType: string | undefined, data: unknown, validationFailures: IValidationFailure[], validationMode?: ValidationMode): Promise<boolean>;
18
+ static validate(propertyName: string, dataType: string | undefined, data: unknown, validationFailures: IValidationFailure[], options?: {
19
+ validationMode?: ValidationMode;
20
+ failOnMissingType?: boolean;
21
+ }): Promise<boolean>;
17
22
  }
package/docs/changelog.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # @twin.org/data-core - Changelog
2
2
 
3
+ ## [0.0.1-next.33](https://github.com/twinfoundation/data/compare/data-core-v0.0.1-next.32...data-core-v0.0.1-next.33) (2025-06-02)
4
+
5
+
6
+ ### Features
7
+
8
+ * add fail on missing type option and both mode ([e8b9702](https://github.com/twinfoundation/data/commit/e8b97029a04b646497ff0e55b9610291e58ae92a))
9
+
10
+
11
+ ### Bug Fixes
12
+
13
+ * tests using context ([577b3bb](https://github.com/twinfoundation/data/commit/577b3bbb661eafbf6d3fd157133c106732e8eb3d))
14
+
3
15
  ## [0.0.1-next.32](https://github.com/twinfoundation/data/compare/data-core-v0.0.1-next.31...data-core-v0.0.1-next.32) (2025-05-28)
4
16
 
5
17
 
@@ -16,7 +16,7 @@ Class to help with data types.
16
16
 
17
17
  ### validate()
18
18
 
19
- > `static` **validate**(`propertyName`, `dataType`, `data`, `validationFailures`, `validationMode?`): `Promise`\<`boolean`\>
19
+ > `static` **validate**(`propertyName`, `dataType`, `data`, `validationFailures`, `options?`): `Promise`\<`boolean`\>
20
20
 
21
21
  Validate a data type.
22
22
 
@@ -46,12 +46,22 @@ The data to validate.
46
46
 
47
47
  The list of validation failures to add to.
48
48
 
49
- ##### validationMode?
49
+ ##### options?
50
+
51
+ Optional options for validation.
52
+
53
+ ###### validationMode?
50
54
 
51
55
  [`ValidationMode`](../type-aliases/ValidationMode.md)
52
56
 
53
57
  The validation mode to use, defaults to either.
54
58
 
59
+ ###### failOnMissingType?
60
+
61
+ `boolean`
62
+
63
+ If true, will fail validation if the data type is missing, defaults to false.
64
+
55
65
  #### Returns
56
66
 
57
67
  `Promise`\<`boolean`\>
@@ -23,3 +23,9 @@ Use the JSON Schema methods of the data type.
23
23
  > `readonly` **Either**: `"either"` = `"either"`
24
24
 
25
25
  Use either validation mode.
26
+
27
+ ### Both
28
+
29
+ > `readonly` **Both**: `"both"` = `"both"`
30
+
31
+ Use both validation modes.
package/locales/en.json CHANGED
@@ -5,7 +5,8 @@
5
5
  "keyAlreadyExists": "The key already exists"
6
6
  },
7
7
  "schema": {
8
- "failedValidation": "The JSON schema failed validation, {message}"
8
+ "failedValidation": "The JSON schema failed validation, {message}",
9
+ "missingType": "Failed to validate as there is no handler for type \"{dataType}\""
9
10
  }
10
11
  }
11
12
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@twin.org/data-core",
3
- "version": "0.0.1-next.32",
3
+ "version": "0.0.1-next.33",
4
4
  "description": "Definitions and helpers for using with data and schemas",
5
5
  "repository": {
6
6
  "type": "git",