@twin.org/data-core 0.0.1-next.3
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/LICENSE +201 -0
- package/README.md +21 -0
- package/dist/cjs/index.cjs +255 -0
- package/dist/esm/index.mjs +249 -0
- package/dist/types/factories/dataTypeHandlerFactory.d.ts +6 -0
- package/dist/types/factories/identifierHandlerFactory.d.ts +6 -0
- package/dist/types/index.d.ts +9 -0
- package/dist/types/models/IDataTypeHandler.d.ts +29 -0
- package/dist/types/models/IIdentifierHandler.d.ts +18 -0
- package/dist/types/models/ISchemaValidationError.d.ts +5 -0
- package/dist/types/models/ISchemaValidationResult.d.ts +14 -0
- package/dist/types/models/validationMode.d.ts +21 -0
- package/dist/types/utils/dataTypeHelper.d.ts +17 -0
- package/dist/types/utils/jsonSchemaHelper.d.ts +36 -0
- package/docs/changelog.md +5 -0
- package/docs/examples.md +1 -0
- package/docs/reference/classes/DataTypeHelper.md +49 -0
- package/docs/reference/classes/JsonSchemaHelper.md +101 -0
- package/docs/reference/index.md +23 -0
- package/docs/reference/interfaces/IDataTypeHandler.md +65 -0
- package/docs/reference/interfaces/IIdentifierHandler.md +39 -0
- package/docs/reference/interfaces/ISchemaValidationResult.md +19 -0
- package/docs/reference/type-aliases/ISchemaValidationError.md +5 -0
- package/docs/reference/type-aliases/ValidationMode.md +5 -0
- package/docs/reference/variables/DataTypeHandlerFactory.md +5 -0
- package/docs/reference/variables/IdentifierHandlerFactory.md +5 -0
- package/docs/reference/variables/ValidationMode.md +25 -0
- package/locales/en.json +12 -0
- package/package.json +68 -0
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
import { Factory, Urn, Is, StringHelper } from '@twin.org/core';
|
|
2
|
+
import { FetchHelper, HttpMethod } from '@twin.org/web';
|
|
3
|
+
import Ajv from 'ajv';
|
|
4
|
+
import addFormats from 'ajv-formats';
|
|
5
|
+
|
|
6
|
+
// Copyright 2024 IOTA Stiftung.
|
|
7
|
+
// SPDX-License-Identifier: Apache-2.0.
|
|
8
|
+
/**
|
|
9
|
+
* Factory for creating handlers for data types.
|
|
10
|
+
*/
|
|
11
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
12
|
+
const DataTypeHandlerFactory = Factory.createFactory("data-type");
|
|
13
|
+
|
|
14
|
+
// Copyright 2024 IOTA Stiftung.
|
|
15
|
+
// SPDX-License-Identifier: Apache-2.0.
|
|
16
|
+
/**
|
|
17
|
+
* Factory for creating handlers for identifiers.
|
|
18
|
+
*/
|
|
19
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
20
|
+
const IdentifierHandlerFactory = Factory.createFactory("namespace", false, (names, uri) => {
|
|
21
|
+
Urn.guard("IdentifierHandlerFactory", "uri", uri);
|
|
22
|
+
const urn = Urn.fromValidString(uri);
|
|
23
|
+
const urnParts = urn.parts();
|
|
24
|
+
for (let i = urnParts.length - 1; i >= 0; i--) {
|
|
25
|
+
const wholeNamespace = urnParts.slice(i).join(":");
|
|
26
|
+
if (names.includes(wholeNamespace)) {
|
|
27
|
+
return wholeNamespace;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
// Copyright 2024 IOTA Stiftung.
|
|
33
|
+
// SPDX-License-Identifier: Apache-2.0.
|
|
34
|
+
/**
|
|
35
|
+
* Validation modes for validating data types.
|
|
36
|
+
*/
|
|
37
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
38
|
+
const ValidationMode = {
|
|
39
|
+
/**
|
|
40
|
+
* Use the validation method of the data type.
|
|
41
|
+
*/
|
|
42
|
+
Validate: "validate",
|
|
43
|
+
/**
|
|
44
|
+
* Use the JSON Schema methods of the data type.
|
|
45
|
+
*/
|
|
46
|
+
JsonSchema: "json-schema",
|
|
47
|
+
/**
|
|
48
|
+
* Use either validation mode.
|
|
49
|
+
*/
|
|
50
|
+
Either: "either"
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
// Copyright 2024 IOTA Stiftung.
|
|
54
|
+
// SPDX-License-Identifier: Apache-2.0.
|
|
55
|
+
/**
|
|
56
|
+
* A helper for JSON schemas.
|
|
57
|
+
*/
|
|
58
|
+
class JsonSchemaHelper {
|
|
59
|
+
/**
|
|
60
|
+
* The schema version.
|
|
61
|
+
*/
|
|
62
|
+
static SCHEMA_VERSION = "https://json-schema.org/draft/2020-12/schema";
|
|
63
|
+
/**
|
|
64
|
+
* The class name.
|
|
65
|
+
* @internal
|
|
66
|
+
*/
|
|
67
|
+
static _CLASS_NAME = "JsonSchemaHelper";
|
|
68
|
+
/**
|
|
69
|
+
* Validates data against the schema.
|
|
70
|
+
* @param schema The schema to validate the data with.
|
|
71
|
+
* @param data The data to be validated.
|
|
72
|
+
* @param additionalTypes Additional types to add for reference, not already in DataTypeHandlerFactory.
|
|
73
|
+
* @returns Result containing errors if there are any.
|
|
74
|
+
*/
|
|
75
|
+
static async validate(schema, data, additionalTypes) {
|
|
76
|
+
const ajv = new Ajv({
|
|
77
|
+
allowUnionTypes: true,
|
|
78
|
+
loadSchema: async (uri) => {
|
|
79
|
+
const subTypeHandler = DataTypeHandlerFactory.getIfExists(uri);
|
|
80
|
+
if (Is.function(subTypeHandler?.jsonSchema)) {
|
|
81
|
+
const subSchema = await subTypeHandler.jsonSchema();
|
|
82
|
+
if (Is.object(subSchema)) {
|
|
83
|
+
return subSchema;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
try {
|
|
87
|
+
// We don't have the type in our local data types, so we try to fetch it from the web
|
|
88
|
+
return FetchHelper.fetchJson(JsonSchemaHelper._CLASS_NAME, uri, HttpMethod.GET, undefined, {
|
|
89
|
+
// Cache for an hour
|
|
90
|
+
cacheTtlMs: 3600000
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
catch {
|
|
94
|
+
// Failed to load remotely so return an empty object
|
|
95
|
+
// so the schema validation doesn't completely fail
|
|
96
|
+
return {};
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
});
|
|
100
|
+
addFormats(ajv);
|
|
101
|
+
// Add the additional types provided by the user
|
|
102
|
+
if (Is.objectValue(additionalTypes)) {
|
|
103
|
+
for (const key in additionalTypes) {
|
|
104
|
+
ajv.addSchema(additionalTypes[key], key);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
const compiled = await ajv.compileAsync(schema);
|
|
108
|
+
const result = await compiled(data);
|
|
109
|
+
const output = {
|
|
110
|
+
result
|
|
111
|
+
};
|
|
112
|
+
if (!output.result) {
|
|
113
|
+
output.error = compiled.errors;
|
|
114
|
+
}
|
|
115
|
+
return output;
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Get the property type from a schema.
|
|
119
|
+
* @param schema The schema to extract the types from.
|
|
120
|
+
* @param propertyName The name of the property to get the type for.
|
|
121
|
+
* @returns The types of the property.
|
|
122
|
+
*/
|
|
123
|
+
static getPropertyType(schema, propertyName) {
|
|
124
|
+
if (schema.type === "object" && Is.objectValue(schema.properties)) {
|
|
125
|
+
const propertySchema = schema.properties[propertyName];
|
|
126
|
+
if (Is.object(propertySchema)) {
|
|
127
|
+
if (Is.stringValue(propertySchema.$ref)) {
|
|
128
|
+
return propertySchema.$ref;
|
|
129
|
+
}
|
|
130
|
+
return propertySchema.type;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Convert an entity schema to JSON schema e.g https://example.com/schemas/.
|
|
136
|
+
* @param entitySchema The entity schema to convert.
|
|
137
|
+
* @param baseDomain The base domain for local schemas e.g. https://example.com/
|
|
138
|
+
* @returns The JSON schema for the entity.
|
|
139
|
+
*/
|
|
140
|
+
static entitySchemaToJsonSchema(entitySchema, baseDomain) {
|
|
141
|
+
let domain = StringHelper.trimTrailingSlashes(baseDomain ?? "");
|
|
142
|
+
if (domain.length > 0) {
|
|
143
|
+
domain += "/";
|
|
144
|
+
}
|
|
145
|
+
const properties = {};
|
|
146
|
+
const required = [];
|
|
147
|
+
if (Is.arrayValue(entitySchema?.properties)) {
|
|
148
|
+
for (const propertySchema of entitySchema.properties) {
|
|
149
|
+
const jsonPropertySchema = {
|
|
150
|
+
type: propertySchema.type,
|
|
151
|
+
description: propertySchema.description,
|
|
152
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
153
|
+
examples: propertySchema.examples
|
|
154
|
+
};
|
|
155
|
+
if (Is.stringValue(propertySchema.itemType) && propertySchema.type === "array") {
|
|
156
|
+
if (propertySchema.itemType === "object") {
|
|
157
|
+
jsonPropertySchema.items = {
|
|
158
|
+
$ref: propertySchema.itemTypeRef?.startsWith("http")
|
|
159
|
+
? propertySchema.itemTypeRef
|
|
160
|
+
: `${domain}${propertySchema.itemTypeRef}`
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
else {
|
|
164
|
+
jsonPropertySchema.items = {
|
|
165
|
+
type: propertySchema.itemType
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
else if (propertySchema.type === "object") {
|
|
170
|
+
delete jsonPropertySchema.type;
|
|
171
|
+
jsonPropertySchema.$ref = propertySchema.itemTypeRef?.startsWith("http")
|
|
172
|
+
? propertySchema.itemTypeRef
|
|
173
|
+
: `${domain}${propertySchema.itemTypeRef}`;
|
|
174
|
+
}
|
|
175
|
+
properties[propertySchema.property] = jsonPropertySchema;
|
|
176
|
+
if (!propertySchema.optional) {
|
|
177
|
+
required.push(propertySchema.property);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
return {
|
|
182
|
+
$schema: JsonSchemaHelper.SCHEMA_VERSION,
|
|
183
|
+
$id: `${domain}${entitySchema?.type}`,
|
|
184
|
+
title: entitySchema?.type,
|
|
185
|
+
type: entitySchema ? "object" : "null",
|
|
186
|
+
description: entitySchema?.options?.description,
|
|
187
|
+
required,
|
|
188
|
+
properties,
|
|
189
|
+
additionalProperties: false
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
// Copyright 2024 IOTA Stiftung.
|
|
195
|
+
// SPDX-License-Identifier: Apache-2.0.
|
|
196
|
+
/**
|
|
197
|
+
* Class to help with data types.
|
|
198
|
+
*/
|
|
199
|
+
class DataTypeHelper {
|
|
200
|
+
/**
|
|
201
|
+
* Validate a data type.
|
|
202
|
+
* @param propertyName The name of the property being validated to use in error messages.
|
|
203
|
+
* @param dataType The data type to validate.
|
|
204
|
+
* @param data The data to validate.
|
|
205
|
+
* @param validationFailures The list of validation failures to add to.
|
|
206
|
+
* @param validationMode The validation mode to use, defaults to either.
|
|
207
|
+
* @returns True if the data was valid.
|
|
208
|
+
*/
|
|
209
|
+
static async validate(propertyName, dataType, data, validationFailures, validationMode) {
|
|
210
|
+
if (Is.stringValue(dataType)) {
|
|
211
|
+
const handler = DataTypeHandlerFactory.getIfExists(dataType);
|
|
212
|
+
if (handler) {
|
|
213
|
+
validationMode = validationMode ?? ValidationMode.Either;
|
|
214
|
+
// If we have a validate function use that as it is more specific
|
|
215
|
+
// and will produce better error messages
|
|
216
|
+
if ((validationMode === ValidationMode.Validate ||
|
|
217
|
+
validationMode === ValidationMode.Either) &&
|
|
218
|
+
Is.function(handler.validate)) {
|
|
219
|
+
return handler.validate(propertyName, data, validationFailures);
|
|
220
|
+
}
|
|
221
|
+
else if ((validationMode === ValidationMode.JsonSchema ||
|
|
222
|
+
validationMode === ValidationMode.Either) &&
|
|
223
|
+
Is.function(handler.jsonSchema)) {
|
|
224
|
+
// Otherwise use the JSON schema if there is one
|
|
225
|
+
const schema = await handler.jsonSchema();
|
|
226
|
+
if (Is.object(schema)) {
|
|
227
|
+
const validationResult = await JsonSchemaHelper.validate(schema, data);
|
|
228
|
+
if (Is.arrayValue(validationResult.error)) {
|
|
229
|
+
validationFailures.push({
|
|
230
|
+
property: propertyName,
|
|
231
|
+
reason: "validation.schema.failedValidation",
|
|
232
|
+
properties: {
|
|
233
|
+
value: data,
|
|
234
|
+
schemaErrors: validationResult.error,
|
|
235
|
+
message: validationResult.error.map(e => e.message).join(", ")
|
|
236
|
+
}
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
return validationResult.result;
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
// Return true by default if no other mechanism for validation is available
|
|
245
|
+
return true;
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
export { DataTypeHandlerFactory, DataTypeHelper, IdentifierHandlerFactory, JsonSchemaHelper, ValidationMode };
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export * from "./factories/dataTypeHandlerFactory";
|
|
2
|
+
export * from "./factories/identifierHandlerFactory";
|
|
3
|
+
export * from "./models/IDataTypeHandler";
|
|
4
|
+
export * from "./models/IIdentifierHandler";
|
|
5
|
+
export * from "./models/ISchemaValidationError";
|
|
6
|
+
export * from "./models/ISchemaValidationResult";
|
|
7
|
+
export * from "./models/validationMode";
|
|
8
|
+
export * from "./utils/dataTypeHelper";
|
|
9
|
+
export * from "./utils/jsonSchemaHelper";
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { IValidationFailure } from "@twin.org/core";
|
|
2
|
+
import type { JSONSchema7 } from "json-schema";
|
|
3
|
+
/**
|
|
4
|
+
* Interface describing a type which can handle a specific data type.
|
|
5
|
+
*/
|
|
6
|
+
export interface IDataTypeHandler {
|
|
7
|
+
/**
|
|
8
|
+
* The type for the item.
|
|
9
|
+
*/
|
|
10
|
+
type: string;
|
|
11
|
+
/**
|
|
12
|
+
* The default value for the item.
|
|
13
|
+
*/
|
|
14
|
+
defaultValue?: unknown;
|
|
15
|
+
/**
|
|
16
|
+
* Get the JSON schema for the data type.
|
|
17
|
+
* @returns The JSON schema for the data type.
|
|
18
|
+
*/
|
|
19
|
+
jsonSchema?(): Promise<JSONSchema7 | undefined>;
|
|
20
|
+
/**
|
|
21
|
+
* A method for validating the data type.
|
|
22
|
+
* @param propertyName The name of the property being validated.
|
|
23
|
+
* @param value The value to validate.
|
|
24
|
+
* @param failures List of failures to add to.
|
|
25
|
+
* @param container The object which contains this one.
|
|
26
|
+
* @returns True if the item is valid.
|
|
27
|
+
*/
|
|
28
|
+
validate?(propertyName: string, value: unknown, failures: IValidationFailure[], container?: unknown): Promise<boolean>;
|
|
29
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { IValidationFailure } from "@twin.org/core";
|
|
2
|
+
/**
|
|
3
|
+
* Interface describing a type which can handle a specific urn namespace.
|
|
4
|
+
*/
|
|
5
|
+
export interface IIdentifierHandler {
|
|
6
|
+
/**
|
|
7
|
+
* The namespace for the identifier.
|
|
8
|
+
*/
|
|
9
|
+
namespace: string;
|
|
10
|
+
/**
|
|
11
|
+
* A method for validating the identifier.
|
|
12
|
+
* @param propertyName The name of the property being validated.
|
|
13
|
+
* @param value The value to validate.
|
|
14
|
+
* @param failures List of failures to add to.
|
|
15
|
+
* @returns True if the item is valid.
|
|
16
|
+
*/
|
|
17
|
+
validate(propertyName: string, value: unknown, failures: IValidationFailure[]): boolean;
|
|
18
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { ISchemaValidationError } from "./ISchemaValidationError";
|
|
2
|
+
/**
|
|
3
|
+
* Validation result.
|
|
4
|
+
*/
|
|
5
|
+
export interface ISchemaValidationResult {
|
|
6
|
+
/**
|
|
7
|
+
* The result.
|
|
8
|
+
*/
|
|
9
|
+
result: boolean;
|
|
10
|
+
/**
|
|
11
|
+
* The error.
|
|
12
|
+
*/
|
|
13
|
+
error?: ISchemaValidationError;
|
|
14
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Validation modes for validating data types.
|
|
3
|
+
*/
|
|
4
|
+
export declare const ValidationMode: {
|
|
5
|
+
/**
|
|
6
|
+
* Use the validation method of the data type.
|
|
7
|
+
*/
|
|
8
|
+
readonly Validate: "validate";
|
|
9
|
+
/**
|
|
10
|
+
* Use the JSON Schema methods of the data type.
|
|
11
|
+
*/
|
|
12
|
+
readonly JsonSchema: "json-schema";
|
|
13
|
+
/**
|
|
14
|
+
* Use either validation mode.
|
|
15
|
+
*/
|
|
16
|
+
readonly Either: "either";
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* Validation modes for validating data types.
|
|
20
|
+
*/
|
|
21
|
+
export type ValidationMode = (typeof ValidationMode)[keyof typeof ValidationMode];
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { type IValidationFailure } from "@twin.org/core";
|
|
2
|
+
import { ValidationMode } from "../models/validationMode";
|
|
3
|
+
/**
|
|
4
|
+
* Class to help with data types.
|
|
5
|
+
*/
|
|
6
|
+
export declare class DataTypeHelper {
|
|
7
|
+
/**
|
|
8
|
+
* Validate a data type.
|
|
9
|
+
* @param propertyName The name of the property being validated to use in error messages.
|
|
10
|
+
* @param dataType The data type to validate.
|
|
11
|
+
* @param data The data to validate.
|
|
12
|
+
* @param validationFailures The list of validation failures to add to.
|
|
13
|
+
* @param validationMode The validation mode to use, defaults to either.
|
|
14
|
+
* @returns True if the data was valid.
|
|
15
|
+
*/
|
|
16
|
+
static validate(propertyName: string, dataType: string | undefined, data: unknown, validationFailures: IValidationFailure[], validationMode?: ValidationMode): Promise<boolean>;
|
|
17
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { IEntitySchema } from "@twin.org/entity";
|
|
2
|
+
import type { JSONSchema7 } from "json-schema";
|
|
3
|
+
import type { ISchemaValidationResult } from "../models/ISchemaValidationResult";
|
|
4
|
+
/**
|
|
5
|
+
* A helper for JSON schemas.
|
|
6
|
+
*/
|
|
7
|
+
export declare class JsonSchemaHelper {
|
|
8
|
+
/**
|
|
9
|
+
* The schema version.
|
|
10
|
+
*/
|
|
11
|
+
static readonly SCHEMA_VERSION = "https://json-schema.org/draft/2020-12/schema";
|
|
12
|
+
/**
|
|
13
|
+
* Validates data against the schema.
|
|
14
|
+
* @param schema The schema to validate the data with.
|
|
15
|
+
* @param data The data to be validated.
|
|
16
|
+
* @param additionalTypes Additional types to add for reference, not already in DataTypeHandlerFactory.
|
|
17
|
+
* @returns Result containing errors if there are any.
|
|
18
|
+
*/
|
|
19
|
+
static validate<T = unknown>(schema: JSONSchema7, data: T, additionalTypes?: {
|
|
20
|
+
[id: string]: JSONSchema7;
|
|
21
|
+
}): Promise<ISchemaValidationResult>;
|
|
22
|
+
/**
|
|
23
|
+
* Get the property type from a schema.
|
|
24
|
+
* @param schema The schema to extract the types from.
|
|
25
|
+
* @param propertyName The name of the property to get the type for.
|
|
26
|
+
* @returns The types of the property.
|
|
27
|
+
*/
|
|
28
|
+
static getPropertyType(schema: JSONSchema7, propertyName: string): string | undefined;
|
|
29
|
+
/**
|
|
30
|
+
* Convert an entity schema to JSON schema e.g https://example.com/schemas/.
|
|
31
|
+
* @param entitySchema The entity schema to convert.
|
|
32
|
+
* @param baseDomain The base domain for local schemas e.g. https://example.com/
|
|
33
|
+
* @returns The JSON schema for the entity.
|
|
34
|
+
*/
|
|
35
|
+
static entitySchemaToJsonSchema(entitySchema: IEntitySchema | undefined, baseDomain?: string): JSONSchema7;
|
|
36
|
+
}
|
package/docs/examples.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# @twin.org/data-core - Examples
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# Class: DataTypeHelper
|
|
2
|
+
|
|
3
|
+
Class to help with data types.
|
|
4
|
+
|
|
5
|
+
## Constructors
|
|
6
|
+
|
|
7
|
+
### new DataTypeHelper()
|
|
8
|
+
|
|
9
|
+
> **new DataTypeHelper**(): [`DataTypeHelper`](DataTypeHelper.md)
|
|
10
|
+
|
|
11
|
+
#### Returns
|
|
12
|
+
|
|
13
|
+
[`DataTypeHelper`](DataTypeHelper.md)
|
|
14
|
+
|
|
15
|
+
## Methods
|
|
16
|
+
|
|
17
|
+
### validate()
|
|
18
|
+
|
|
19
|
+
> `static` **validate**(`propertyName`, `dataType`, `data`, `validationFailures`, `validationMode`?): `Promise`\<`boolean`\>
|
|
20
|
+
|
|
21
|
+
Validate a data type.
|
|
22
|
+
|
|
23
|
+
#### Parameters
|
|
24
|
+
|
|
25
|
+
• **propertyName**: `string`
|
|
26
|
+
|
|
27
|
+
The name of the property being validated to use in error messages.
|
|
28
|
+
|
|
29
|
+
• **dataType**: `undefined` \| `string`
|
|
30
|
+
|
|
31
|
+
The data type to validate.
|
|
32
|
+
|
|
33
|
+
• **data**: `unknown`
|
|
34
|
+
|
|
35
|
+
The data to validate.
|
|
36
|
+
|
|
37
|
+
• **validationFailures**: `IValidationFailure`[]
|
|
38
|
+
|
|
39
|
+
The list of validation failures to add to.
|
|
40
|
+
|
|
41
|
+
• **validationMode?**: [`ValidationMode`](../type-aliases/ValidationMode.md)
|
|
42
|
+
|
|
43
|
+
The validation mode to use, defaults to either.
|
|
44
|
+
|
|
45
|
+
#### Returns
|
|
46
|
+
|
|
47
|
+
`Promise`\<`boolean`\>
|
|
48
|
+
|
|
49
|
+
True if the data was valid.
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
# Class: JsonSchemaHelper
|
|
2
|
+
|
|
3
|
+
A helper for JSON schemas.
|
|
4
|
+
|
|
5
|
+
## Constructors
|
|
6
|
+
|
|
7
|
+
### new JsonSchemaHelper()
|
|
8
|
+
|
|
9
|
+
> **new JsonSchemaHelper**(): [`JsonSchemaHelper`](JsonSchemaHelper.md)
|
|
10
|
+
|
|
11
|
+
#### Returns
|
|
12
|
+
|
|
13
|
+
[`JsonSchemaHelper`](JsonSchemaHelper.md)
|
|
14
|
+
|
|
15
|
+
## Properties
|
|
16
|
+
|
|
17
|
+
### SCHEMA\_VERSION
|
|
18
|
+
|
|
19
|
+
> `readonly` `static` **SCHEMA\_VERSION**: `"https://json-schema.org/draft/2020-12/schema"` = `"https://json-schema.org/draft/2020-12/schema"`
|
|
20
|
+
|
|
21
|
+
The schema version.
|
|
22
|
+
|
|
23
|
+
## Methods
|
|
24
|
+
|
|
25
|
+
### validate()
|
|
26
|
+
|
|
27
|
+
> `static` **validate**\<`T`\>(`schema`, `data`, `additionalTypes`?): `Promise`\<[`ISchemaValidationResult`](../interfaces/ISchemaValidationResult.md)\>
|
|
28
|
+
|
|
29
|
+
Validates data against the schema.
|
|
30
|
+
|
|
31
|
+
#### Type Parameters
|
|
32
|
+
|
|
33
|
+
• **T** = `unknown`
|
|
34
|
+
|
|
35
|
+
#### Parameters
|
|
36
|
+
|
|
37
|
+
• **schema**: `JSONSchema7`
|
|
38
|
+
|
|
39
|
+
The schema to validate the data with.
|
|
40
|
+
|
|
41
|
+
• **data**: `T`
|
|
42
|
+
|
|
43
|
+
The data to be validated.
|
|
44
|
+
|
|
45
|
+
• **additionalTypes?**
|
|
46
|
+
|
|
47
|
+
Additional types to add for reference, not already in DataTypeHandlerFactory.
|
|
48
|
+
|
|
49
|
+
#### Returns
|
|
50
|
+
|
|
51
|
+
`Promise`\<[`ISchemaValidationResult`](../interfaces/ISchemaValidationResult.md)\>
|
|
52
|
+
|
|
53
|
+
Result containing errors if there are any.
|
|
54
|
+
|
|
55
|
+
***
|
|
56
|
+
|
|
57
|
+
### getPropertyType()
|
|
58
|
+
|
|
59
|
+
> `static` **getPropertyType**(`schema`, `propertyName`): `undefined` \| `string`
|
|
60
|
+
|
|
61
|
+
Get the property type from a schema.
|
|
62
|
+
|
|
63
|
+
#### Parameters
|
|
64
|
+
|
|
65
|
+
• **schema**: `JSONSchema7`
|
|
66
|
+
|
|
67
|
+
The schema to extract the types from.
|
|
68
|
+
|
|
69
|
+
• **propertyName**: `string`
|
|
70
|
+
|
|
71
|
+
The name of the property to get the type for.
|
|
72
|
+
|
|
73
|
+
#### Returns
|
|
74
|
+
|
|
75
|
+
`undefined` \| `string`
|
|
76
|
+
|
|
77
|
+
The types of the property.
|
|
78
|
+
|
|
79
|
+
***
|
|
80
|
+
|
|
81
|
+
### entitySchemaToJsonSchema()
|
|
82
|
+
|
|
83
|
+
> `static` **entitySchemaToJsonSchema**(`entitySchema`, `baseDomain`?): `JSONSchema7`
|
|
84
|
+
|
|
85
|
+
Convert an entity schema to JSON schema e.g https://example.com/schemas/.
|
|
86
|
+
|
|
87
|
+
#### Parameters
|
|
88
|
+
|
|
89
|
+
• **entitySchema**: `undefined` \| `IEntitySchema`\<`unknown`\>
|
|
90
|
+
|
|
91
|
+
The entity schema to convert.
|
|
92
|
+
|
|
93
|
+
• **baseDomain?**: `string`
|
|
94
|
+
|
|
95
|
+
The base domain for local schemas e.g. https://example.com/
|
|
96
|
+
|
|
97
|
+
#### Returns
|
|
98
|
+
|
|
99
|
+
`JSONSchema7`
|
|
100
|
+
|
|
101
|
+
The JSON schema for the entity.
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# @twin.org/data-core
|
|
2
|
+
|
|
3
|
+
## Classes
|
|
4
|
+
|
|
5
|
+
- [DataTypeHelper](classes/DataTypeHelper.md)
|
|
6
|
+
- [JsonSchemaHelper](classes/JsonSchemaHelper.md)
|
|
7
|
+
|
|
8
|
+
## Interfaces
|
|
9
|
+
|
|
10
|
+
- [IDataTypeHandler](interfaces/IDataTypeHandler.md)
|
|
11
|
+
- [IIdentifierHandler](interfaces/IIdentifierHandler.md)
|
|
12
|
+
- [ISchemaValidationResult](interfaces/ISchemaValidationResult.md)
|
|
13
|
+
|
|
14
|
+
## Type Aliases
|
|
15
|
+
|
|
16
|
+
- [ISchemaValidationError](type-aliases/ISchemaValidationError.md)
|
|
17
|
+
- [ValidationMode](type-aliases/ValidationMode.md)
|
|
18
|
+
|
|
19
|
+
## Variables
|
|
20
|
+
|
|
21
|
+
- [DataTypeHandlerFactory](variables/DataTypeHandlerFactory.md)
|
|
22
|
+
- [IdentifierHandlerFactory](variables/IdentifierHandlerFactory.md)
|
|
23
|
+
- [ValidationMode](variables/ValidationMode.md)
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
# Interface: IDataTypeHandler
|
|
2
|
+
|
|
3
|
+
Interface describing a type which can handle a specific data type.
|
|
4
|
+
|
|
5
|
+
## Properties
|
|
6
|
+
|
|
7
|
+
### type
|
|
8
|
+
|
|
9
|
+
> **type**: `string`
|
|
10
|
+
|
|
11
|
+
The type for the item.
|
|
12
|
+
|
|
13
|
+
***
|
|
14
|
+
|
|
15
|
+
### defaultValue?
|
|
16
|
+
|
|
17
|
+
> `optional` **defaultValue**: `unknown`
|
|
18
|
+
|
|
19
|
+
The default value for the item.
|
|
20
|
+
|
|
21
|
+
## Methods
|
|
22
|
+
|
|
23
|
+
### jsonSchema()?
|
|
24
|
+
|
|
25
|
+
> `optional` **jsonSchema**(): `Promise`\<`undefined` \| `JSONSchema7`\>
|
|
26
|
+
|
|
27
|
+
Get the JSON schema for the data type.
|
|
28
|
+
|
|
29
|
+
#### Returns
|
|
30
|
+
|
|
31
|
+
`Promise`\<`undefined` \| `JSONSchema7`\>
|
|
32
|
+
|
|
33
|
+
The JSON schema for the data type.
|
|
34
|
+
|
|
35
|
+
***
|
|
36
|
+
|
|
37
|
+
### validate()?
|
|
38
|
+
|
|
39
|
+
> `optional` **validate**(`propertyName`, `value`, `failures`, `container`?): `Promise`\<`boolean`\>
|
|
40
|
+
|
|
41
|
+
A method for validating the data type.
|
|
42
|
+
|
|
43
|
+
#### Parameters
|
|
44
|
+
|
|
45
|
+
• **propertyName**: `string`
|
|
46
|
+
|
|
47
|
+
The name of the property being validated.
|
|
48
|
+
|
|
49
|
+
• **value**: `unknown`
|
|
50
|
+
|
|
51
|
+
The value to validate.
|
|
52
|
+
|
|
53
|
+
• **failures**: `IValidationFailure`[]
|
|
54
|
+
|
|
55
|
+
List of failures to add to.
|
|
56
|
+
|
|
57
|
+
• **container?**: `unknown`
|
|
58
|
+
|
|
59
|
+
The object which contains this one.
|
|
60
|
+
|
|
61
|
+
#### Returns
|
|
62
|
+
|
|
63
|
+
`Promise`\<`boolean`\>
|
|
64
|
+
|
|
65
|
+
True if the item is valid.
|