@twin.org/data-core 0.0.1-next.9 → 0.0.1
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/cjs/index.cjs +35 -12
- package/dist/esm/index.mjs +35 -12
- package/dist/types/index.d.ts +1 -0
- package/dist/types/models/IDataTypeHandler.d.ts +7 -3
- package/dist/types/models/IJsonSchema.d.ts +5 -0
- package/dist/types/models/validationMode.d.ts +4 -0
- package/dist/types/utils/dataTypeHelper.d.ts +7 -2
- package/dist/types/utils/jsonSchemaHelper.d.ts +5 -5
- package/docs/changelog.md +93 -1
- package/docs/reference/classes/DataTypeHelper.md +29 -9
- package/docs/reference/classes/JsonSchemaHelper.md +28 -14
- package/docs/reference/index.md +1 -0
- package/docs/reference/interfaces/IDataTypeHandler.md +24 -8
- package/docs/reference/interfaces/IIdentifierHandler.md +9 -3
- package/docs/reference/type-aliases/IJsonSchema.md +5 -0
- package/docs/reference/type-aliases/ISchemaValidationError.md +1 -1
- package/docs/reference/type-aliases/ValidationMode.md +1 -1
- package/docs/reference/variables/ValidationMode.md +6 -0
- package/locales/en.json +2 -1
- package/package.json +7 -8
package/dist/cjs/index.cjs
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
var core = require('@twin.org/core');
|
|
4
4
|
var web = require('@twin.org/web');
|
|
5
|
-
var Ajv = require('ajv');
|
|
5
|
+
var Ajv = require('ajv/dist/2020.js');
|
|
6
6
|
var addFormats = require('ajv-formats');
|
|
7
7
|
|
|
8
8
|
// Copyright 2024 IOTA Stiftung.
|
|
@@ -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
|
|
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,
|
|
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
|
-
|
|
234
|
+
isValid = await handler.validate(propertyName, data, validationFailures);
|
|
235
|
+
hasValidated = true;
|
|
226
236
|
}
|
|
227
|
-
|
|
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();
|
|
@@ -238,17 +249,29 @@ class DataTypeHelper {
|
|
|
238
249
|
properties: {
|
|
239
250
|
value: data,
|
|
240
251
|
schemaErrors: validationResult.error,
|
|
241
|
-
message: validationResult.error.map(e => e.message).join("
|
|
252
|
+
message: validationResult.error.map(e => e.message).join("\n")
|
|
242
253
|
}
|
|
243
254
|
});
|
|
244
255
|
}
|
|
245
|
-
|
|
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
|
-
|
|
251
|
-
return true;
|
|
274
|
+
return isValid;
|
|
252
275
|
}
|
|
253
276
|
}
|
|
254
277
|
|
package/dist/esm/index.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Factory, Urn, Is, StringHelper } from '@twin.org/core';
|
|
2
2
|
import { FetchHelper, HttpMethod } from '@twin.org/web';
|
|
3
|
-
import Ajv from 'ajv';
|
|
3
|
+
import Ajv from 'ajv/dist/2020.js';
|
|
4
4
|
import addFormats from 'ajv-formats';
|
|
5
5
|
|
|
6
6
|
// Copyright 2024 IOTA Stiftung.
|
|
@@ -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
|
|
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,
|
|
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
|
-
|
|
232
|
+
isValid = await handler.validate(propertyName, data, validationFailures);
|
|
233
|
+
hasValidated = true;
|
|
224
234
|
}
|
|
225
|
-
|
|
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();
|
|
@@ -236,17 +247,29 @@ class DataTypeHelper {
|
|
|
236
247
|
properties: {
|
|
237
248
|
value: data,
|
|
238
249
|
schemaErrors: validationResult.error,
|
|
239
|
-
message: validationResult.error.map(e => e.message).join("
|
|
250
|
+
message: validationResult.error.map(e => e.message).join("\n")
|
|
240
251
|
}
|
|
241
252
|
});
|
|
242
253
|
}
|
|
243
|
-
|
|
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
|
-
|
|
249
|
-
return true;
|
|
272
|
+
return isValid;
|
|
250
273
|
}
|
|
251
274
|
}
|
|
252
275
|
|
package/dist/types/index.d.ts
CHANGED
|
@@ -2,6 +2,7 @@ export * from "./factories/dataTypeHandlerFactory";
|
|
|
2
2
|
export * from "./factories/identifierHandlerFactory";
|
|
3
3
|
export * from "./models/IDataTypeHandler";
|
|
4
4
|
export * from "./models/IIdentifierHandler";
|
|
5
|
+
export * from "./models/IJsonSchema";
|
|
5
6
|
export * from "./models/ISchemaValidationError";
|
|
6
7
|
export * from "./models/ISchemaValidationResult";
|
|
7
8
|
export * from "./models/validationMode";
|
|
@@ -1,22 +1,26 @@
|
|
|
1
1
|
import type { IValidationFailure } from "@twin.org/core";
|
|
2
|
-
import type {
|
|
2
|
+
import type { IJsonSchema } from "./IJsonSchema";
|
|
3
3
|
/**
|
|
4
4
|
* Interface describing a type which can handle a specific data type.
|
|
5
5
|
*/
|
|
6
6
|
export interface IDataTypeHandler {
|
|
7
|
+
/**
|
|
8
|
+
* The context for the type.
|
|
9
|
+
*/
|
|
10
|
+
context: string;
|
|
7
11
|
/**
|
|
8
12
|
* The type for the item.
|
|
9
13
|
*/
|
|
10
14
|
type: string;
|
|
11
15
|
/**
|
|
12
|
-
* The default value for the item.
|
|
16
|
+
* The default value for the item to use when constructing a new object.
|
|
13
17
|
*/
|
|
14
18
|
defaultValue?: unknown;
|
|
15
19
|
/**
|
|
16
20
|
* Get the JSON schema for the data type.
|
|
17
21
|
* @returns The JSON schema for the data type.
|
|
18
22
|
*/
|
|
19
|
-
jsonSchema?(): Promise<
|
|
23
|
+
jsonSchema?(): Promise<IJsonSchema | undefined>;
|
|
20
24
|
/**
|
|
21
25
|
* A method for validating the data type.
|
|
22
26
|
* @param propertyName The name of the property being validated.
|
|
@@ -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
|
|
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[],
|
|
18
|
+
static validate(propertyName: string, dataType: string | undefined, data: unknown, validationFailures: IValidationFailure[], options?: {
|
|
19
|
+
validationMode?: ValidationMode;
|
|
20
|
+
failOnMissingType?: boolean;
|
|
21
|
+
}): Promise<boolean>;
|
|
17
22
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { IEntitySchema } from "@twin.org/entity";
|
|
2
|
-
import type {
|
|
2
|
+
import type { IJsonSchema } from "../models/IJsonSchema";
|
|
3
3
|
import type { ISchemaValidationResult } from "../models/ISchemaValidationResult";
|
|
4
4
|
/**
|
|
5
5
|
* A helper for JSON schemas.
|
|
@@ -16,8 +16,8 @@ export declare class JsonSchemaHelper {
|
|
|
16
16
|
* @param additionalTypes Additional types to add for reference, not already in DataTypeHandlerFactory.
|
|
17
17
|
* @returns Result containing errors if there are any.
|
|
18
18
|
*/
|
|
19
|
-
static validate<T = unknown>(schema:
|
|
20
|
-
[id: string]:
|
|
19
|
+
static validate<T = unknown>(schema: IJsonSchema, data: T, additionalTypes?: {
|
|
20
|
+
[id: string]: IJsonSchema;
|
|
21
21
|
}): Promise<ISchemaValidationResult>;
|
|
22
22
|
/**
|
|
23
23
|
* Get the property type from a schema.
|
|
@@ -25,12 +25,12 @@ export declare class JsonSchemaHelper {
|
|
|
25
25
|
* @param propertyName The name of the property to get the type for.
|
|
26
26
|
* @returns The types of the property.
|
|
27
27
|
*/
|
|
28
|
-
static getPropertyType(schema:
|
|
28
|
+
static getPropertyType(schema: IJsonSchema, propertyName: string): string | undefined;
|
|
29
29
|
/**
|
|
30
30
|
* Convert an entity schema to JSON schema e.g https://example.com/schemas/.
|
|
31
31
|
* @param entitySchema The entity schema to convert.
|
|
32
32
|
* @param baseDomain The base domain for local schemas e.g. https://example.com/
|
|
33
33
|
* @returns The JSON schema for the entity.
|
|
34
34
|
*/
|
|
35
|
-
static entitySchemaToJsonSchema(entitySchema: IEntitySchema | undefined, baseDomain?: string):
|
|
35
|
+
static entitySchemaToJsonSchema(entitySchema: IEntitySchema | undefined, baseDomain?: string): IJsonSchema;
|
|
36
36
|
}
|
package/docs/changelog.md
CHANGED
|
@@ -1,5 +1,97 @@
|
|
|
1
1
|
# @twin.org/data-core - Changelog
|
|
2
2
|
|
|
3
|
-
##
|
|
3
|
+
## 0.0.1 (2025-07-03)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
### Features
|
|
7
|
+
|
|
8
|
+
* add document cache access methods ([dbf1e36](https://github.com/twinfoundation/data/commit/dbf1e36d176c5f428f8c52628fb5a1ff7a6a174a))
|
|
9
|
+
* add fail on missing type option and both mode ([e8b9702](https://github.com/twinfoundation/data/commit/e8b97029a04b646497ff0e55b9610291e58ae92a))
|
|
10
|
+
* expand Json LD Keyword ([70632d1](https://github.com/twinfoundation/data/commit/70632d1e11ad85cf3c57e118476b125a673f1681))
|
|
11
|
+
* use fully qualified names for data type lookups ([b7b5c74](https://github.com/twinfoundation/data/commit/b7b5c746b0180a87baa976f6a7a76cedd53d8ff7))
|
|
12
|
+
* use shared store mechanism ([#3](https://github.com/twinfoundation/data/issues/3)) ([33eb221](https://github.com/twinfoundation/data/commit/33eb221ccec2b4a79549c06e9a04225009b93a46))
|
|
13
|
+
* use updated JSON schema specs ([465223a](https://github.com/twinfoundation/data/commit/465223a9e9c24af546480ef084327a78fa366eaa))
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
### Bug Fixes
|
|
17
|
+
|
|
18
|
+
* interface name ([6e49322](https://github.com/twinfoundation/data/commit/6e49322ec1797417220ec9e529bb124f4717f489))
|
|
19
|
+
* remove undici reference ([d77721e](https://github.com/twinfoundation/data/commit/d77721e21d23c7a6750c2f5cac8104851dfaa6d7))
|
|
20
|
+
* tests using context ([577b3bb](https://github.com/twinfoundation/data/commit/577b3bbb661eafbf6d3fd157133c106732e8eb3d))
|
|
21
|
+
|
|
22
|
+
## [0.0.1-next.37](https://github.com/twinfoundation/data/compare/data-core-v0.0.1-next.36...data-core-v0.0.1-next.37) (2025-06-11)
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
### Features
|
|
26
|
+
|
|
27
|
+
* use updated JSON schema specs ([465223a](https://github.com/twinfoundation/data/commit/465223a9e9c24af546480ef084327a78fa366eaa))
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
### Bug Fixes
|
|
31
|
+
|
|
32
|
+
* remove undici reference ([d77721e](https://github.com/twinfoundation/data/commit/d77721e21d23c7a6750c2f5cac8104851dfaa6d7))
|
|
33
|
+
|
|
34
|
+
## [0.0.1-next.36](https://github.com/twinfoundation/data/compare/data-core-v0.0.1-next.35...data-core-v0.0.1-next.36) (2025-06-10)
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
### Features
|
|
38
|
+
|
|
39
|
+
* expand Json LD Keyword ([70632d1](https://github.com/twinfoundation/data/commit/70632d1e11ad85cf3c57e118476b125a673f1681))
|
|
40
|
+
|
|
41
|
+
## [0.0.1-next.35](https://github.com/twinfoundation/data/compare/data-core-v0.0.1-next.34...data-core-v0.0.1-next.35) (2025-06-03)
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
### Miscellaneous Chores
|
|
45
|
+
|
|
46
|
+
* **data-core:** Synchronize repo versions
|
|
47
|
+
|
|
48
|
+
## [0.0.1-next.34](https://github.com/twinfoundation/data/compare/data-core-v0.0.1-next.33...data-core-v0.0.1-next.34) (2025-06-02)
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
### Miscellaneous Chores
|
|
52
|
+
|
|
53
|
+
* **data-core:** Synchronize repo versions
|
|
54
|
+
|
|
55
|
+
## [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)
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
### Features
|
|
59
|
+
|
|
60
|
+
* add fail on missing type option and both mode ([e8b9702](https://github.com/twinfoundation/data/commit/e8b97029a04b646497ff0e55b9610291e58ae92a))
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
### Bug Fixes
|
|
64
|
+
|
|
65
|
+
* tests using context ([577b3bb](https://github.com/twinfoundation/data/commit/577b3bbb661eafbf6d3fd157133c106732e8eb3d))
|
|
66
|
+
|
|
67
|
+
## [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)
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
### Features
|
|
71
|
+
|
|
72
|
+
* use fully qualified names for data type lookups ([b7b5c74](https://github.com/twinfoundation/data/commit/b7b5c746b0180a87baa976f6a7a76cedd53d8ff7))
|
|
73
|
+
|
|
74
|
+
## [0.0.1-next.31](https://github.com/twinfoundation/data/compare/data-core-v0.0.1-next.30...data-core-v0.0.1-next.31) (2025-05-08)
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
### Miscellaneous Chores
|
|
78
|
+
|
|
79
|
+
* **data-core:** Synchronize repo versions
|
|
80
|
+
|
|
81
|
+
## [0.0.1-next.30](https://github.com/twinfoundation/data/compare/data-core-v0.0.1-next.29...data-core-v0.0.1-next.30) (2025-04-17)
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
### Features
|
|
85
|
+
|
|
86
|
+
* use shared store mechanism ([#3](https://github.com/twinfoundation/data/issues/3)) ([33eb221](https://github.com/twinfoundation/data/commit/33eb221ccec2b4a79549c06e9a04225009b93a46))
|
|
87
|
+
|
|
88
|
+
## [0.0.1-next.29](https://github.com/twinfoundation/data/compare/data-core-v0.0.1-next.28...data-core-v0.0.1-next.29) (2025-03-28)
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
### Features
|
|
92
|
+
|
|
93
|
+
* add document cache access methods ([dbf1e36](https://github.com/twinfoundation/data/commit/dbf1e36d176c5f428f8c52628fb5a1ff7a6a174a))
|
|
94
|
+
|
|
95
|
+
## v0.0.1-next.28
|
|
4
96
|
|
|
5
97
|
- Initial Release
|
|
@@ -4,44 +4,64 @@ Class to help with data types.
|
|
|
4
4
|
|
|
5
5
|
## Constructors
|
|
6
6
|
|
|
7
|
-
###
|
|
7
|
+
### Constructor
|
|
8
8
|
|
|
9
|
-
> **new DataTypeHelper**():
|
|
9
|
+
> **new DataTypeHelper**(): `DataTypeHelper`
|
|
10
10
|
|
|
11
11
|
#### Returns
|
|
12
12
|
|
|
13
|
-
|
|
13
|
+
`DataTypeHelper`
|
|
14
14
|
|
|
15
15
|
## Methods
|
|
16
16
|
|
|
17
17
|
### validate()
|
|
18
18
|
|
|
19
|
-
> `static` **validate**(`propertyName`, `dataType`, `data`, `validationFailures`, `
|
|
19
|
+
> `static` **validate**(`propertyName`, `dataType`, `data`, `validationFailures`, `options?`): `Promise`\<`boolean`\>
|
|
20
20
|
|
|
21
21
|
Validate a data type.
|
|
22
22
|
|
|
23
23
|
#### Parameters
|
|
24
24
|
|
|
25
|
-
|
|
25
|
+
##### propertyName
|
|
26
|
+
|
|
27
|
+
`string`
|
|
26
28
|
|
|
27
29
|
The name of the property being validated to use in error messages.
|
|
28
30
|
|
|
29
|
-
|
|
31
|
+
##### dataType
|
|
30
32
|
|
|
31
33
|
The data type to validate.
|
|
32
34
|
|
|
33
|
-
|
|
35
|
+
`undefined` | `string`
|
|
36
|
+
|
|
37
|
+
##### data
|
|
38
|
+
|
|
39
|
+
`unknown`
|
|
34
40
|
|
|
35
41
|
The data to validate.
|
|
36
42
|
|
|
37
|
-
|
|
43
|
+
##### validationFailures
|
|
44
|
+
|
|
45
|
+
`IValidationFailure`[]
|
|
38
46
|
|
|
39
47
|
The list of validation failures to add to.
|
|
40
48
|
|
|
41
|
-
|
|
49
|
+
##### options?
|
|
50
|
+
|
|
51
|
+
Optional options for validation.
|
|
52
|
+
|
|
53
|
+
###### validationMode?
|
|
54
|
+
|
|
55
|
+
[`ValidationMode`](../type-aliases/ValidationMode.md)
|
|
42
56
|
|
|
43
57
|
The validation mode to use, defaults to either.
|
|
44
58
|
|
|
59
|
+
###### failOnMissingType?
|
|
60
|
+
|
|
61
|
+
`boolean`
|
|
62
|
+
|
|
63
|
+
If true, will fail validation if the data type is missing, defaults to false.
|
|
64
|
+
|
|
45
65
|
#### Returns
|
|
46
66
|
|
|
47
67
|
`Promise`\<`boolean`\>
|
|
@@ -4,13 +4,13 @@ A helper for JSON schemas.
|
|
|
4
4
|
|
|
5
5
|
## Constructors
|
|
6
6
|
|
|
7
|
-
###
|
|
7
|
+
### Constructor
|
|
8
8
|
|
|
9
|
-
> **new JsonSchemaHelper**():
|
|
9
|
+
> **new JsonSchemaHelper**(): `JsonSchemaHelper`
|
|
10
10
|
|
|
11
11
|
#### Returns
|
|
12
12
|
|
|
13
|
-
|
|
13
|
+
`JsonSchemaHelper`
|
|
14
14
|
|
|
15
15
|
## Properties
|
|
16
16
|
|
|
@@ -24,25 +24,31 @@ The schema version.
|
|
|
24
24
|
|
|
25
25
|
### validate()
|
|
26
26
|
|
|
27
|
-
> `static` **validate**\<`T`\>(`schema`, `data`, `additionalTypes
|
|
27
|
+
> `static` **validate**\<`T`\>(`schema`, `data`, `additionalTypes?`): `Promise`\<[`ISchemaValidationResult`](../interfaces/ISchemaValidationResult.md)\>
|
|
28
28
|
|
|
29
29
|
Validates data against the schema.
|
|
30
30
|
|
|
31
31
|
#### Type Parameters
|
|
32
32
|
|
|
33
|
-
|
|
33
|
+
##### T
|
|
34
|
+
|
|
35
|
+
`T` = `unknown`
|
|
34
36
|
|
|
35
37
|
#### Parameters
|
|
36
38
|
|
|
37
|
-
|
|
39
|
+
##### schema
|
|
40
|
+
|
|
41
|
+
`SchemaObject`
|
|
38
42
|
|
|
39
43
|
The schema to validate the data with.
|
|
40
44
|
|
|
41
|
-
|
|
45
|
+
##### data
|
|
46
|
+
|
|
47
|
+
`T`
|
|
42
48
|
|
|
43
49
|
The data to be validated.
|
|
44
50
|
|
|
45
|
-
|
|
51
|
+
##### additionalTypes?
|
|
46
52
|
|
|
47
53
|
Additional types to add for reference, not already in DataTypeHandlerFactory.
|
|
48
54
|
|
|
@@ -62,11 +68,15 @@ Get the property type from a schema.
|
|
|
62
68
|
|
|
63
69
|
#### Parameters
|
|
64
70
|
|
|
65
|
-
|
|
71
|
+
##### schema
|
|
72
|
+
|
|
73
|
+
`SchemaObject`
|
|
66
74
|
|
|
67
75
|
The schema to extract the types from.
|
|
68
76
|
|
|
69
|
-
|
|
77
|
+
##### propertyName
|
|
78
|
+
|
|
79
|
+
`string`
|
|
70
80
|
|
|
71
81
|
The name of the property to get the type for.
|
|
72
82
|
|
|
@@ -80,22 +90,26 @@ The types of the property.
|
|
|
80
90
|
|
|
81
91
|
### entitySchemaToJsonSchema()
|
|
82
92
|
|
|
83
|
-
> `static` **entitySchemaToJsonSchema**(`entitySchema`, `baseDomain
|
|
93
|
+
> `static` **entitySchemaToJsonSchema**(`entitySchema`, `baseDomain?`): `SchemaObject`
|
|
84
94
|
|
|
85
95
|
Convert an entity schema to JSON schema e.g https://example.com/schemas/.
|
|
86
96
|
|
|
87
97
|
#### Parameters
|
|
88
98
|
|
|
89
|
-
|
|
99
|
+
##### entitySchema
|
|
90
100
|
|
|
91
101
|
The entity schema to convert.
|
|
92
102
|
|
|
93
|
-
|
|
103
|
+
`undefined` | `IEntitySchema`\<`unknown`\>
|
|
104
|
+
|
|
105
|
+
##### baseDomain?
|
|
106
|
+
|
|
107
|
+
`string`
|
|
94
108
|
|
|
95
109
|
The base domain for local schemas e.g. https://example.com/
|
|
96
110
|
|
|
97
111
|
#### Returns
|
|
98
112
|
|
|
99
|
-
`
|
|
113
|
+
`SchemaObject`
|
|
100
114
|
|
|
101
115
|
The JSON schema for the entity.
|
package/docs/reference/index.md
CHANGED
|
@@ -4,6 +4,14 @@ Interface describing a type which can handle a specific data type.
|
|
|
4
4
|
|
|
5
5
|
## Properties
|
|
6
6
|
|
|
7
|
+
### context
|
|
8
|
+
|
|
9
|
+
> **context**: `string`
|
|
10
|
+
|
|
11
|
+
The context for the type.
|
|
12
|
+
|
|
13
|
+
***
|
|
14
|
+
|
|
7
15
|
### type
|
|
8
16
|
|
|
9
17
|
> **type**: `string`
|
|
@@ -16,19 +24,19 @@ The type for the item.
|
|
|
16
24
|
|
|
17
25
|
> `optional` **defaultValue**: `unknown`
|
|
18
26
|
|
|
19
|
-
The default value for the item.
|
|
27
|
+
The default value for the item to use when constructing a new object.
|
|
20
28
|
|
|
21
29
|
## Methods
|
|
22
30
|
|
|
23
31
|
### jsonSchema()?
|
|
24
32
|
|
|
25
|
-
> `optional` **jsonSchema**(): `Promise`\<`undefined` \| `
|
|
33
|
+
> `optional` **jsonSchema**(): `Promise`\<`undefined` \| `SchemaObject`\>
|
|
26
34
|
|
|
27
35
|
Get the JSON schema for the data type.
|
|
28
36
|
|
|
29
37
|
#### Returns
|
|
30
38
|
|
|
31
|
-
`Promise`\<`undefined` \| `
|
|
39
|
+
`Promise`\<`undefined` \| `SchemaObject`\>
|
|
32
40
|
|
|
33
41
|
The JSON schema for the data type.
|
|
34
42
|
|
|
@@ -36,25 +44,33 @@ The JSON schema for the data type.
|
|
|
36
44
|
|
|
37
45
|
### validate()?
|
|
38
46
|
|
|
39
|
-
> `optional` **validate**(`propertyName`, `value`, `failures`, `container
|
|
47
|
+
> `optional` **validate**(`propertyName`, `value`, `failures`, `container?`): `Promise`\<`boolean`\>
|
|
40
48
|
|
|
41
49
|
A method for validating the data type.
|
|
42
50
|
|
|
43
51
|
#### Parameters
|
|
44
52
|
|
|
45
|
-
|
|
53
|
+
##### propertyName
|
|
54
|
+
|
|
55
|
+
`string`
|
|
46
56
|
|
|
47
57
|
The name of the property being validated.
|
|
48
58
|
|
|
49
|
-
|
|
59
|
+
##### value
|
|
60
|
+
|
|
61
|
+
`unknown`
|
|
50
62
|
|
|
51
63
|
The value to validate.
|
|
52
64
|
|
|
53
|
-
|
|
65
|
+
##### failures
|
|
66
|
+
|
|
67
|
+
`IValidationFailure`[]
|
|
54
68
|
|
|
55
69
|
List of failures to add to.
|
|
56
70
|
|
|
57
|
-
|
|
71
|
+
##### container?
|
|
72
|
+
|
|
73
|
+
`unknown`
|
|
58
74
|
|
|
59
75
|
The object which contains this one.
|
|
60
76
|
|
|
@@ -20,15 +20,21 @@ A method for validating the identifier.
|
|
|
20
20
|
|
|
21
21
|
#### Parameters
|
|
22
22
|
|
|
23
|
-
|
|
23
|
+
##### propertyName
|
|
24
|
+
|
|
25
|
+
`string`
|
|
24
26
|
|
|
25
27
|
The name of the property being validated.
|
|
26
28
|
|
|
27
|
-
|
|
29
|
+
##### value
|
|
30
|
+
|
|
31
|
+
`unknown`
|
|
28
32
|
|
|
29
33
|
The value to validate.
|
|
30
34
|
|
|
31
|
-
|
|
35
|
+
##### failures
|
|
36
|
+
|
|
37
|
+
`IValidationFailure`[]
|
|
32
38
|
|
|
33
39
|
List of failures to add to.
|
|
34
40
|
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
# Type Alias: ValidationMode
|
|
2
2
|
|
|
3
|
-
> **ValidationMode
|
|
3
|
+
> **ValidationMode** = *typeof* [`ValidationMode`](../variables/ValidationMode.md)\[keyof *typeof* [`ValidationMode`](../variables/ValidationMode.md)\]
|
|
4
4
|
|
|
5
5
|
Validation modes for validating data types.
|
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
|
|
3
|
+
"version": "0.0.1",
|
|
4
4
|
"description": "Definitions and helpers for using with data and schemas",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -14,11 +14,10 @@
|
|
|
14
14
|
"node": ">=20.0.0"
|
|
15
15
|
},
|
|
16
16
|
"dependencies": {
|
|
17
|
-
"@twin.org/core": "
|
|
18
|
-
"@twin.org/entity": "
|
|
19
|
-
"@twin.org/nameof": "
|
|
20
|
-
"@twin.org/web": "
|
|
21
|
-
"@types/json-schema": "7.0.15",
|
|
17
|
+
"@twin.org/core": "^0.0.1",
|
|
18
|
+
"@twin.org/entity": "^0.0.1",
|
|
19
|
+
"@twin.org/nameof": "^0.0.1",
|
|
20
|
+
"@twin.org/web": "^0.0.1",
|
|
22
21
|
"ajv": "8.17.1",
|
|
23
22
|
"ajv-formats": "3.0.1"
|
|
24
23
|
},
|
|
@@ -27,9 +26,9 @@
|
|
|
27
26
|
"types": "./dist/types/index.d.ts",
|
|
28
27
|
"exports": {
|
|
29
28
|
".": {
|
|
29
|
+
"types": "./dist/types/index.d.ts",
|
|
30
30
|
"require": "./dist/cjs/index.cjs",
|
|
31
|
-
"import": "./dist/esm/index.mjs"
|
|
32
|
-
"types": "./dist/types/index.d.ts"
|
|
31
|
+
"import": "./dist/esm/index.mjs"
|
|
33
32
|
}
|
|
34
33
|
},
|
|
35
34
|
"files": [
|