@twin.org/data-core 0.0.1-next.36 → 0.0.1-next.37
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 +36 -111
- package/dist/esm/index.mjs +36 -111
- package/dist/types/index.d.ts +1 -1
- package/dist/types/models/IJsonSchema.d.ts +2 -2
- package/dist/types/models/ISchemaValidationError.d.ts +5 -0
- package/dist/types/models/ISchemaValidationResult.d.ts +2 -2
- package/dist/types/utils/jsonSchemaHelper.d.ts +0 -10
- package/docs/changelog.md +12 -0
- package/docs/reference/classes/JsonSchemaHelper.md +4 -38
- package/docs/reference/index.md +1 -1
- package/docs/reference/interfaces/IDataTypeHandler.md +2 -2
- package/docs/reference/interfaces/ISchemaValidationResult.md +2 -2
- package/docs/reference/type-aliases/IJsonSchema.md +1 -1
- package/docs/reference/type-aliases/ISchemaValidationError.md +5 -0
- package/package.json +4 -4
- package/dist/types/models/ISchemaValidationErrors.d.ts +0 -7
- package/docs/reference/type-aliases/ISchemaValidationErrors.md +0 -5
package/dist/cjs/index.cjs
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
var core = require('@twin.org/core');
|
|
4
|
-
var browser = require('@hyperjump/browser');
|
|
5
|
-
var draft202012 = require('@hyperjump/json-schema/draft-2020-12');
|
|
6
4
|
var web = require('@twin.org/web');
|
|
5
|
+
var Ajv = require('ajv/dist/2020.js');
|
|
6
|
+
var addFormats = require('ajv-formats');
|
|
7
7
|
|
|
8
8
|
// Copyright 2024 IOTA Stiftung.
|
|
9
9
|
// SPDX-License-Identifier: Apache-2.0.
|
|
@@ -66,16 +66,6 @@ class JsonSchemaHelper {
|
|
|
66
66
|
* The schema version.
|
|
67
67
|
*/
|
|
68
68
|
static SCHEMA_VERSION = "https://json-schema.org/draft/2020-12/schema";
|
|
69
|
-
/**
|
|
70
|
-
* The private prefix for the type.
|
|
71
|
-
* @internal
|
|
72
|
-
*/
|
|
73
|
-
static _PRIVATE_PREFIX = "https://twindev.org/private/";
|
|
74
|
-
/**
|
|
75
|
-
* The private type.
|
|
76
|
-
* @internal
|
|
77
|
-
*/
|
|
78
|
-
static _PRIVATE_TYPE = "https://twindev.org/private/ValidationType";
|
|
79
69
|
/**
|
|
80
70
|
* The class name.
|
|
81
71
|
* @internal
|
|
@@ -89,77 +79,50 @@ class JsonSchemaHelper {
|
|
|
89
79
|
* @returns Result containing errors if there are any.
|
|
90
80
|
*/
|
|
91
81
|
static async validate(schema, data, additionalTypes) {
|
|
92
|
-
const
|
|
93
|
-
|
|
94
|
-
|
|
82
|
+
const ajv = new Ajv({
|
|
83
|
+
allowUnionTypes: true,
|
|
84
|
+
// Disable strict tuples as it causes issues with the schema validation when
|
|
85
|
+
// you have an array with fixed elements e.g. myType: [string, ...string[]]
|
|
86
|
+
// https://github.com/ajv-validator/ajv/issues/1417
|
|
87
|
+
strictTuples: false,
|
|
88
|
+
loadSchema: async (uri) => {
|
|
95
89
|
const subTypeHandler = DataTypeHandlerFactory.getIfExists(uri);
|
|
96
90
|
if (core.Is.function(subTypeHandler?.jsonSchema)) {
|
|
97
91
|
const subSchema = await subTypeHandler.jsonSchema();
|
|
98
92
|
if (core.Is.object(subSchema)) {
|
|
99
|
-
|
|
93
|
+
return subSchema;
|
|
100
94
|
}
|
|
101
95
|
}
|
|
102
|
-
|
|
103
|
-
try
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
}
|
|
113
|
-
catch { }
|
|
96
|
+
try {
|
|
97
|
+
// We don't have the type in our local data types, so we try to fetch it from the web
|
|
98
|
+
return web.FetchHelper.fetchJson(JsonSchemaHelper._CLASS_NAME, uri, web.HttpMethod.GET, undefined, {
|
|
99
|
+
// Cache for an hour
|
|
100
|
+
cacheTtlMs: 3600000
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
catch {
|
|
104
|
+
// Failed to load remotely so return an empty object
|
|
105
|
+
// so the schema validation doesn't completely fail
|
|
106
|
+
return {};
|
|
114
107
|
}
|
|
115
|
-
// Failed to load remotely so return an empty object
|
|
116
|
-
// so the schema validation doesn't completely fail
|
|
117
|
-
loadedSchema ??= {};
|
|
118
|
-
return {
|
|
119
|
-
status: 200,
|
|
120
|
-
statusText: "OK",
|
|
121
|
-
ok: true,
|
|
122
|
-
url: uri,
|
|
123
|
-
headers: new Headers({
|
|
124
|
-
"Content-Type": "application/schema+json"
|
|
125
|
-
}),
|
|
126
|
-
json: Promise.resolve(loadedSchema)
|
|
127
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
128
|
-
};
|
|
129
108
|
}
|
|
130
|
-
};
|
|
131
|
-
|
|
132
|
-
browser.addUriSchemePlugin("http", httpSchemePlugin);
|
|
109
|
+
});
|
|
110
|
+
addFormats(ajv);
|
|
133
111
|
// Add the additional types provided by the user
|
|
134
112
|
if (core.Is.objectValue(additionalTypes)) {
|
|
135
113
|
for (const key in additionalTypes) {
|
|
136
|
-
additionalTypes[key]
|
|
137
|
-
additionalTypes[key].$schema ?? JsonSchemaHelper.SCHEMA_VERSION;
|
|
138
|
-
const additionalType = key.startsWith("http")
|
|
139
|
-
? key
|
|
140
|
-
: `${JsonSchemaHelper._PRIVATE_PREFIX}${key}`;
|
|
141
|
-
draft202012.registerSchema(additionalTypes[key], additionalType);
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
schema.$schema = schema.$schema ?? JsonSchemaHelper.SCHEMA_VERSION;
|
|
145
|
-
draft202012.registerSchema(schema, JsonSchemaHelper._PRIVATE_TYPE);
|
|
146
|
-
const output = await draft202012.validate(JsonSchemaHelper._PRIVATE_TYPE, data, "DETAILED");
|
|
147
|
-
await this.cleanupOutput(output);
|
|
148
|
-
await this.formatErrors(output, data);
|
|
149
|
-
draft202012.unregisterSchema(JsonSchemaHelper._PRIVATE_TYPE);
|
|
150
|
-
// Remove the additional types provided by the user
|
|
151
|
-
if (core.Is.objectValue(additionalTypes)) {
|
|
152
|
-
for (const key in additionalTypes) {
|
|
153
|
-
const additionalType = key.startsWith("http")
|
|
154
|
-
? key
|
|
155
|
-
: `${JsonSchemaHelper._PRIVATE_PREFIX}${key}`;
|
|
156
|
-
draft202012.unregisterSchema(additionalType);
|
|
114
|
+
ajv.addSchema(additionalTypes[key], key);
|
|
157
115
|
}
|
|
158
116
|
}
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
117
|
+
const compiled = await ajv.compileAsync(schema);
|
|
118
|
+
const result = await compiled(data);
|
|
119
|
+
const output = {
|
|
120
|
+
result
|
|
162
121
|
};
|
|
122
|
+
if (!output.result) {
|
|
123
|
+
output.error = compiled.errors;
|
|
124
|
+
}
|
|
125
|
+
return output;
|
|
163
126
|
}
|
|
164
127
|
/**
|
|
165
128
|
* Get the property type from a schema.
|
|
@@ -168,12 +131,9 @@ class JsonSchemaHelper {
|
|
|
168
131
|
* @returns The types of the property.
|
|
169
132
|
*/
|
|
170
133
|
static getPropertyType(schema, propertyName) {
|
|
171
|
-
if (core.Is.boolean(schema)) {
|
|
172
|
-
return undefined;
|
|
173
|
-
}
|
|
174
134
|
if (schema.type === "object" && core.Is.objectValue(schema.properties)) {
|
|
175
135
|
const propertySchema = schema.properties[propertyName];
|
|
176
|
-
if (
|
|
136
|
+
if (core.Is.object(propertySchema)) {
|
|
177
137
|
if (core.Is.stringValue(propertySchema.$ref)) {
|
|
178
138
|
return propertySchema.$ref;
|
|
179
139
|
}
|
|
@@ -239,41 +199,6 @@ class JsonSchemaHelper {
|
|
|
239
199
|
additionalProperties: false
|
|
240
200
|
};
|
|
241
201
|
}
|
|
242
|
-
/**
|
|
243
|
-
* Cleanup the errors from the schema validation.
|
|
244
|
-
* @param outputUnit The errors to format.
|
|
245
|
-
* @param data The data that was validated.
|
|
246
|
-
* @returns The formatted errors.
|
|
247
|
-
*/
|
|
248
|
-
static async formatErrors(outputUnit, data) {
|
|
249
|
-
if (outputUnit.keyword === "https://json-schema.org/keyword/required") {
|
|
250
|
-
outputUnit.message = `The property '${outputUnit.instanceLocation}' is required but was not found.`;
|
|
251
|
-
}
|
|
252
|
-
else {
|
|
253
|
-
outputUnit.message = `"${outputUnit.instanceLocation}" fails schema constraint ${outputUnit.absoluteKeywordLocation}`;
|
|
254
|
-
}
|
|
255
|
-
if (core.Is.arrayValue(outputUnit.errors)) {
|
|
256
|
-
for (const subError of outputUnit.errors) {
|
|
257
|
-
await this.formatErrors(subError, data);
|
|
258
|
-
}
|
|
259
|
-
}
|
|
260
|
-
}
|
|
261
|
-
/**
|
|
262
|
-
* Cleanup the errors from the schema validation.
|
|
263
|
-
* @param outputUnit The errors to format.
|
|
264
|
-
* @returns The formatted errors.
|
|
265
|
-
* @internal
|
|
266
|
-
*/
|
|
267
|
-
static async cleanupOutput(outputUnit) {
|
|
268
|
-
if (outputUnit.absoluteKeywordLocation?.startsWith(JsonSchemaHelper._PRIVATE_TYPE)) {
|
|
269
|
-
outputUnit.absoluteKeywordLocation = outputUnit.absoluteKeywordLocation.replace(JsonSchemaHelper._PRIVATE_TYPE, "");
|
|
270
|
-
}
|
|
271
|
-
if (core.Is.arrayValue(outputUnit.errors)) {
|
|
272
|
-
for (const subError of outputUnit.errors) {
|
|
273
|
-
await this.cleanupOutput(subError);
|
|
274
|
-
}
|
|
275
|
-
}
|
|
276
|
-
}
|
|
277
202
|
}
|
|
278
203
|
|
|
279
204
|
// Copyright 2024 IOTA Stiftung.
|
|
@@ -317,14 +242,14 @@ class DataTypeHelper {
|
|
|
317
242
|
const schema = await handler.jsonSchema();
|
|
318
243
|
if (core.Is.object(schema)) {
|
|
319
244
|
const validationResult = await JsonSchemaHelper.validate(schema, data);
|
|
320
|
-
if (core.Is.arrayValue(validationResult.
|
|
245
|
+
if (core.Is.arrayValue(validationResult.error)) {
|
|
321
246
|
validationFailures.push({
|
|
322
247
|
property: propertyName,
|
|
323
248
|
reason: "validation.schema.failedValidation",
|
|
324
249
|
properties: {
|
|
325
250
|
value: data,
|
|
326
|
-
schemaErrors: validationResult.
|
|
327
|
-
message: validationResult.
|
|
251
|
+
schemaErrors: validationResult.error,
|
|
252
|
+
message: validationResult.error.map(e => e.message).join("\n")
|
|
328
253
|
}
|
|
329
254
|
});
|
|
330
255
|
}
|
package/dist/esm/index.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Factory, Urn, Is, StringHelper } from '@twin.org/core';
|
|
2
|
-
import { addUriSchemePlugin, acceptableMediaTypes } from '@hyperjump/browser';
|
|
3
|
-
import { registerSchema, validate, unregisterSchema } from '@hyperjump/json-schema/draft-2020-12';
|
|
4
2
|
import { FetchHelper, HttpMethod } from '@twin.org/web';
|
|
3
|
+
import Ajv from 'ajv/dist/2020.js';
|
|
4
|
+
import addFormats from 'ajv-formats';
|
|
5
5
|
|
|
6
6
|
// Copyright 2024 IOTA Stiftung.
|
|
7
7
|
// SPDX-License-Identifier: Apache-2.0.
|
|
@@ -64,16 +64,6 @@ class JsonSchemaHelper {
|
|
|
64
64
|
* The schema version.
|
|
65
65
|
*/
|
|
66
66
|
static SCHEMA_VERSION = "https://json-schema.org/draft/2020-12/schema";
|
|
67
|
-
/**
|
|
68
|
-
* The private prefix for the type.
|
|
69
|
-
* @internal
|
|
70
|
-
*/
|
|
71
|
-
static _PRIVATE_PREFIX = "https://twindev.org/private/";
|
|
72
|
-
/**
|
|
73
|
-
* The private type.
|
|
74
|
-
* @internal
|
|
75
|
-
*/
|
|
76
|
-
static _PRIVATE_TYPE = "https://twindev.org/private/ValidationType";
|
|
77
67
|
/**
|
|
78
68
|
* The class name.
|
|
79
69
|
* @internal
|
|
@@ -87,77 +77,50 @@ class JsonSchemaHelper {
|
|
|
87
77
|
* @returns Result containing errors if there are any.
|
|
88
78
|
*/
|
|
89
79
|
static async validate(schema, data, additionalTypes) {
|
|
90
|
-
const
|
|
91
|
-
|
|
92
|
-
|
|
80
|
+
const ajv = new Ajv({
|
|
81
|
+
allowUnionTypes: true,
|
|
82
|
+
// Disable strict tuples as it causes issues with the schema validation when
|
|
83
|
+
// you have an array with fixed elements e.g. myType: [string, ...string[]]
|
|
84
|
+
// https://github.com/ajv-validator/ajv/issues/1417
|
|
85
|
+
strictTuples: false,
|
|
86
|
+
loadSchema: async (uri) => {
|
|
93
87
|
const subTypeHandler = DataTypeHandlerFactory.getIfExists(uri);
|
|
94
88
|
if (Is.function(subTypeHandler?.jsonSchema)) {
|
|
95
89
|
const subSchema = await subTypeHandler.jsonSchema();
|
|
96
90
|
if (Is.object(subSchema)) {
|
|
97
|
-
|
|
91
|
+
return subSchema;
|
|
98
92
|
}
|
|
99
93
|
}
|
|
100
|
-
|
|
101
|
-
try
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
}
|
|
111
|
-
catch { }
|
|
94
|
+
try {
|
|
95
|
+
// We don't have the type in our local data types, so we try to fetch it from the web
|
|
96
|
+
return FetchHelper.fetchJson(JsonSchemaHelper._CLASS_NAME, uri, HttpMethod.GET, undefined, {
|
|
97
|
+
// Cache for an hour
|
|
98
|
+
cacheTtlMs: 3600000
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
catch {
|
|
102
|
+
// Failed to load remotely so return an empty object
|
|
103
|
+
// so the schema validation doesn't completely fail
|
|
104
|
+
return {};
|
|
112
105
|
}
|
|
113
|
-
// Failed to load remotely so return an empty object
|
|
114
|
-
// so the schema validation doesn't completely fail
|
|
115
|
-
loadedSchema ??= {};
|
|
116
|
-
return {
|
|
117
|
-
status: 200,
|
|
118
|
-
statusText: "OK",
|
|
119
|
-
ok: true,
|
|
120
|
-
url: uri,
|
|
121
|
-
headers: new Headers({
|
|
122
|
-
"Content-Type": "application/schema+json"
|
|
123
|
-
}),
|
|
124
|
-
json: Promise.resolve(loadedSchema)
|
|
125
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
126
|
-
};
|
|
127
106
|
}
|
|
128
|
-
};
|
|
129
|
-
|
|
130
|
-
addUriSchemePlugin("http", httpSchemePlugin);
|
|
107
|
+
});
|
|
108
|
+
addFormats(ajv);
|
|
131
109
|
// Add the additional types provided by the user
|
|
132
110
|
if (Is.objectValue(additionalTypes)) {
|
|
133
111
|
for (const key in additionalTypes) {
|
|
134
|
-
additionalTypes[key]
|
|
135
|
-
additionalTypes[key].$schema ?? JsonSchemaHelper.SCHEMA_VERSION;
|
|
136
|
-
const additionalType = key.startsWith("http")
|
|
137
|
-
? key
|
|
138
|
-
: `${JsonSchemaHelper._PRIVATE_PREFIX}${key}`;
|
|
139
|
-
registerSchema(additionalTypes[key], additionalType);
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
schema.$schema = schema.$schema ?? JsonSchemaHelper.SCHEMA_VERSION;
|
|
143
|
-
registerSchema(schema, JsonSchemaHelper._PRIVATE_TYPE);
|
|
144
|
-
const output = await validate(JsonSchemaHelper._PRIVATE_TYPE, data, "DETAILED");
|
|
145
|
-
await this.cleanupOutput(output);
|
|
146
|
-
await this.formatErrors(output, data);
|
|
147
|
-
unregisterSchema(JsonSchemaHelper._PRIVATE_TYPE);
|
|
148
|
-
// Remove the additional types provided by the user
|
|
149
|
-
if (Is.objectValue(additionalTypes)) {
|
|
150
|
-
for (const key in additionalTypes) {
|
|
151
|
-
const additionalType = key.startsWith("http")
|
|
152
|
-
? key
|
|
153
|
-
: `${JsonSchemaHelper._PRIVATE_PREFIX}${key}`;
|
|
154
|
-
unregisterSchema(additionalType);
|
|
112
|
+
ajv.addSchema(additionalTypes[key], key);
|
|
155
113
|
}
|
|
156
114
|
}
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
115
|
+
const compiled = await ajv.compileAsync(schema);
|
|
116
|
+
const result = await compiled(data);
|
|
117
|
+
const output = {
|
|
118
|
+
result
|
|
160
119
|
};
|
|
120
|
+
if (!output.result) {
|
|
121
|
+
output.error = compiled.errors;
|
|
122
|
+
}
|
|
123
|
+
return output;
|
|
161
124
|
}
|
|
162
125
|
/**
|
|
163
126
|
* Get the property type from a schema.
|
|
@@ -166,12 +129,9 @@ class JsonSchemaHelper {
|
|
|
166
129
|
* @returns The types of the property.
|
|
167
130
|
*/
|
|
168
131
|
static getPropertyType(schema, propertyName) {
|
|
169
|
-
if (Is.boolean(schema)) {
|
|
170
|
-
return undefined;
|
|
171
|
-
}
|
|
172
132
|
if (schema.type === "object" && Is.objectValue(schema.properties)) {
|
|
173
133
|
const propertySchema = schema.properties[propertyName];
|
|
174
|
-
if (
|
|
134
|
+
if (Is.object(propertySchema)) {
|
|
175
135
|
if (Is.stringValue(propertySchema.$ref)) {
|
|
176
136
|
return propertySchema.$ref;
|
|
177
137
|
}
|
|
@@ -237,41 +197,6 @@ class JsonSchemaHelper {
|
|
|
237
197
|
additionalProperties: false
|
|
238
198
|
};
|
|
239
199
|
}
|
|
240
|
-
/**
|
|
241
|
-
* Cleanup the errors from the schema validation.
|
|
242
|
-
* @param outputUnit The errors to format.
|
|
243
|
-
* @param data The data that was validated.
|
|
244
|
-
* @returns The formatted errors.
|
|
245
|
-
*/
|
|
246
|
-
static async formatErrors(outputUnit, data) {
|
|
247
|
-
if (outputUnit.keyword === "https://json-schema.org/keyword/required") {
|
|
248
|
-
outputUnit.message = `The property '${outputUnit.instanceLocation}' is required but was not found.`;
|
|
249
|
-
}
|
|
250
|
-
else {
|
|
251
|
-
outputUnit.message = `"${outputUnit.instanceLocation}" fails schema constraint ${outputUnit.absoluteKeywordLocation}`;
|
|
252
|
-
}
|
|
253
|
-
if (Is.arrayValue(outputUnit.errors)) {
|
|
254
|
-
for (const subError of outputUnit.errors) {
|
|
255
|
-
await this.formatErrors(subError, data);
|
|
256
|
-
}
|
|
257
|
-
}
|
|
258
|
-
}
|
|
259
|
-
/**
|
|
260
|
-
* Cleanup the errors from the schema validation.
|
|
261
|
-
* @param outputUnit The errors to format.
|
|
262
|
-
* @returns The formatted errors.
|
|
263
|
-
* @internal
|
|
264
|
-
*/
|
|
265
|
-
static async cleanupOutput(outputUnit) {
|
|
266
|
-
if (outputUnit.absoluteKeywordLocation?.startsWith(JsonSchemaHelper._PRIVATE_TYPE)) {
|
|
267
|
-
outputUnit.absoluteKeywordLocation = outputUnit.absoluteKeywordLocation.replace(JsonSchemaHelper._PRIVATE_TYPE, "");
|
|
268
|
-
}
|
|
269
|
-
if (Is.arrayValue(outputUnit.errors)) {
|
|
270
|
-
for (const subError of outputUnit.errors) {
|
|
271
|
-
await this.cleanupOutput(subError);
|
|
272
|
-
}
|
|
273
|
-
}
|
|
274
|
-
}
|
|
275
200
|
}
|
|
276
201
|
|
|
277
202
|
// Copyright 2024 IOTA Stiftung.
|
|
@@ -315,14 +240,14 @@ class DataTypeHelper {
|
|
|
315
240
|
const schema = await handler.jsonSchema();
|
|
316
241
|
if (Is.object(schema)) {
|
|
317
242
|
const validationResult = await JsonSchemaHelper.validate(schema, data);
|
|
318
|
-
if (Is.arrayValue(validationResult.
|
|
243
|
+
if (Is.arrayValue(validationResult.error)) {
|
|
319
244
|
validationFailures.push({
|
|
320
245
|
property: propertyName,
|
|
321
246
|
reason: "validation.schema.failedValidation",
|
|
322
247
|
properties: {
|
|
323
248
|
value: data,
|
|
324
|
-
schemaErrors: validationResult.
|
|
325
|
-
message: validationResult.
|
|
249
|
+
schemaErrors: validationResult.error,
|
|
250
|
+
message: validationResult.error.map(e => e.message).join("\n")
|
|
326
251
|
}
|
|
327
252
|
});
|
|
328
253
|
}
|
package/dist/types/index.d.ts
CHANGED
|
@@ -3,7 +3,7 @@ export * from "./factories/identifierHandlerFactory";
|
|
|
3
3
|
export * from "./models/IDataTypeHandler";
|
|
4
4
|
export * from "./models/IIdentifierHandler";
|
|
5
5
|
export * from "./models/IJsonSchema";
|
|
6
|
-
export * from "./models/
|
|
6
|
+
export * from "./models/ISchemaValidationError";
|
|
7
7
|
export * from "./models/ISchemaValidationResult";
|
|
8
8
|
export * from "./models/validationMode";
|
|
9
9
|
export * from "./utils/dataTypeHelper";
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { SchemaObject } from "ajv/dist/2020";
|
|
2
2
|
/**
|
|
3
3
|
* Default schema type.
|
|
4
4
|
*/
|
|
5
|
-
export type IJsonSchema =
|
|
5
|
+
export type IJsonSchema = SchemaObject;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { ISchemaValidationError } from "./ISchemaValidationError";
|
|
2
2
|
/**
|
|
3
3
|
* Validation result.
|
|
4
4
|
*/
|
|
@@ -10,5 +10,5 @@ export interface ISchemaValidationResult {
|
|
|
10
10
|
/**
|
|
11
11
|
* The error.
|
|
12
12
|
*/
|
|
13
|
-
|
|
13
|
+
error?: ISchemaValidationError;
|
|
14
14
|
}
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { type OutputUnit } from "@hyperjump/json-schema/draft-2020-12";
|
|
2
1
|
import type { IEntitySchema } from "@twin.org/entity";
|
|
3
2
|
import type { IJsonSchema } from "../models/IJsonSchema";
|
|
4
3
|
import type { ISchemaValidationResult } from "../models/ISchemaValidationResult";
|
|
@@ -34,13 +33,4 @@ export declare class JsonSchemaHelper {
|
|
|
34
33
|
* @returns The JSON schema for the entity.
|
|
35
34
|
*/
|
|
36
35
|
static entitySchemaToJsonSchema(entitySchema: IEntitySchema | undefined, baseDomain?: string): IJsonSchema;
|
|
37
|
-
/**
|
|
38
|
-
* Cleanup the errors from the schema validation.
|
|
39
|
-
* @param outputUnit The errors to format.
|
|
40
|
-
* @param data The data that was validated.
|
|
41
|
-
* @returns The formatted errors.
|
|
42
|
-
*/
|
|
43
|
-
static formatErrors<T>(outputUnit: OutputUnit & {
|
|
44
|
-
message?: string;
|
|
45
|
-
}, data: T): Promise<void>;
|
|
46
36
|
}
|
package/docs/changelog.md
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
1
1
|
# @twin.org/data-core - Changelog
|
|
2
2
|
|
|
3
|
+
## [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)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
### Features
|
|
7
|
+
|
|
8
|
+
* use updated JSON schema specs ([465223a](https://github.com/twinfoundation/data/commit/465223a9e9c24af546480ef084327a78fa366eaa))
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### Bug Fixes
|
|
12
|
+
|
|
13
|
+
* remove undici reference ([d77721e](https://github.com/twinfoundation/data/commit/d77721e21d23c7a6750c2f5cac8104851dfaa6d7))
|
|
14
|
+
|
|
3
15
|
## [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)
|
|
4
16
|
|
|
5
17
|
|
|
@@ -38,7 +38,7 @@ Validates data against the schema.
|
|
|
38
38
|
|
|
39
39
|
##### schema
|
|
40
40
|
|
|
41
|
-
`
|
|
41
|
+
`SchemaObject`
|
|
42
42
|
|
|
43
43
|
The schema to validate the data with.
|
|
44
44
|
|
|
@@ -70,7 +70,7 @@ Get the property type from a schema.
|
|
|
70
70
|
|
|
71
71
|
##### schema
|
|
72
72
|
|
|
73
|
-
`
|
|
73
|
+
`SchemaObject`
|
|
74
74
|
|
|
75
75
|
The schema to extract the types from.
|
|
76
76
|
|
|
@@ -90,7 +90,7 @@ The types of the property.
|
|
|
90
90
|
|
|
91
91
|
### entitySchemaToJsonSchema()
|
|
92
92
|
|
|
93
|
-
> `static` **entitySchemaToJsonSchema**(`entitySchema`, `baseDomain?`): `
|
|
93
|
+
> `static` **entitySchemaToJsonSchema**(`entitySchema`, `baseDomain?`): `SchemaObject`
|
|
94
94
|
|
|
95
95
|
Convert an entity schema to JSON schema e.g https://example.com/schemas/.
|
|
96
96
|
|
|
@@ -110,40 +110,6 @@ The base domain for local schemas e.g. https://example.com/
|
|
|
110
110
|
|
|
111
111
|
#### Returns
|
|
112
112
|
|
|
113
|
-
`
|
|
113
|
+
`SchemaObject`
|
|
114
114
|
|
|
115
115
|
The JSON schema for the entity.
|
|
116
|
-
|
|
117
|
-
***
|
|
118
|
-
|
|
119
|
-
### formatErrors()
|
|
120
|
-
|
|
121
|
-
> `static` **formatErrors**\<`T`\>(`outputUnit`, `data`): `Promise`\<`void`\>
|
|
122
|
-
|
|
123
|
-
Cleanup the errors from the schema validation.
|
|
124
|
-
|
|
125
|
-
#### Type Parameters
|
|
126
|
-
|
|
127
|
-
##### T
|
|
128
|
-
|
|
129
|
-
`T`
|
|
130
|
-
|
|
131
|
-
#### Parameters
|
|
132
|
-
|
|
133
|
-
##### outputUnit
|
|
134
|
-
|
|
135
|
-
`OutputUnit` & `object`
|
|
136
|
-
|
|
137
|
-
The errors to format.
|
|
138
|
-
|
|
139
|
-
##### data
|
|
140
|
-
|
|
141
|
-
`T`
|
|
142
|
-
|
|
143
|
-
The data that was validated.
|
|
144
|
-
|
|
145
|
-
#### Returns
|
|
146
|
-
|
|
147
|
-
`Promise`\<`void`\>
|
|
148
|
-
|
|
149
|
-
The formatted errors.
|
package/docs/reference/index.md
CHANGED
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
## Type Aliases
|
|
15
15
|
|
|
16
16
|
- [IJsonSchema](type-aliases/IJsonSchema.md)
|
|
17
|
-
- [
|
|
17
|
+
- [ISchemaValidationError](type-aliases/ISchemaValidationError.md)
|
|
18
18
|
- [ValidationMode](type-aliases/ValidationMode.md)
|
|
19
19
|
|
|
20
20
|
## Variables
|
|
@@ -30,13 +30,13 @@ The default value for the item to use when constructing a new object.
|
|
|
30
30
|
|
|
31
31
|
### jsonSchema()?
|
|
32
32
|
|
|
33
|
-
> `optional` **jsonSchema**(): `Promise`\<`undefined` \| `
|
|
33
|
+
> `optional` **jsonSchema**(): `Promise`\<`undefined` \| `SchemaObject`\>
|
|
34
34
|
|
|
35
35
|
Get the JSON schema for the data type.
|
|
36
36
|
|
|
37
37
|
#### Returns
|
|
38
38
|
|
|
39
|
-
`Promise`\<`undefined` \| `
|
|
39
|
+
`Promise`\<`undefined` \| `SchemaObject`\>
|
|
40
40
|
|
|
41
41
|
The JSON schema for the data type.
|
|
42
42
|
|
|
@@ -12,8 +12,8 @@ The result.
|
|
|
12
12
|
|
|
13
13
|
***
|
|
14
14
|
|
|
15
|
-
###
|
|
15
|
+
### error?
|
|
16
16
|
|
|
17
|
-
> `optional` **
|
|
17
|
+
> `optional` **error**: [`ISchemaValidationError`](../type-aliases/ISchemaValidationError.md)
|
|
18
18
|
|
|
19
19
|
The error.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@twin.org/data-core",
|
|
3
|
-
"version": "0.0.1-next.
|
|
3
|
+
"version": "0.0.1-next.37",
|
|
4
4
|
"description": "Definitions and helpers for using with data and schemas",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -14,12 +14,12 @@
|
|
|
14
14
|
"node": ">=20.0.0"
|
|
15
15
|
},
|
|
16
16
|
"dependencies": {
|
|
17
|
-
"@hyperjump/browser": "1.3.0",
|
|
18
|
-
"@hyperjump/json-schema": "1.15.0",
|
|
19
17
|
"@twin.org/core": "next",
|
|
20
18
|
"@twin.org/entity": "next",
|
|
21
19
|
"@twin.org/nameof": "next",
|
|
22
|
-
"@twin.org/web": "next"
|
|
20
|
+
"@twin.org/web": "next",
|
|
21
|
+
"ajv": "8.17.1",
|
|
22
|
+
"ajv-formats": "3.0.1"
|
|
23
23
|
},
|
|
24
24
|
"main": "./dist/cjs/index.cjs",
|
|
25
25
|
"module": "./dist/esm/index.mjs",
|