jopi-toolkit 3.1.32 → 3.1.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/jk_app/common.d.ts +1 -0
- package/dist/jk_app/common.js +9 -2
- package/dist/jk_app/common.js.map +1 -1
- package/dist/jk_data/browserActions.d.ts +8 -0
- package/dist/jk_data/browserActions.js +9 -0
- package/dist/jk_data/browserActions.js.map +1 -0
- package/dist/jk_data/bundler_ifServer.d.ts +2 -0
- package/dist/jk_data/bundler_ifServer.js +2 -0
- package/dist/jk_data/bundler_ifServer.js.map +1 -0
- package/dist/jk_data/common.d.ts +8 -0
- package/dist/jk_data/common.js +2 -0
- package/dist/jk_data/common.js.map +1 -0
- package/dist/jk_data/core.d.ts +13 -0
- package/dist/jk_data/core.js +85 -0
- package/dist/jk_data/core.js.map +1 -0
- package/dist/jk_data/dataTableProxy.d.ts +21 -0
- package/dist/jk_data/dataTableProxy.js +49 -0
- package/dist/jk_data/dataTableProxy.js.map +1 -0
- package/dist/jk_data/ifServerSide.d.ts +0 -0
- package/dist/jk_data/ifServerSide.js +2 -0
- package/dist/jk_data/ifServerSide.js.map +1 -0
- package/dist/jk_data/index.d.ts +3 -55
- package/dist/jk_data/index.js +3 -105
- package/dist/jk_data/index.js.map +1 -1
- package/dist/jk_data/interfaces.d.ts +107 -0
- package/dist/jk_data/interfaces.js +2 -0
- package/dist/jk_data/interfaces.js.map +1 -0
- package/dist/jk_data/jBundler_common.d.ts +12 -0
- package/dist/jk_data/jBundler_common.js +2 -0
- package/dist/jk_data/jBundler_common.js.map +1 -0
- package/dist/jk_data/jBundler_ifBrowser.d.ts +2 -0
- package/dist/jk_data/jBundler_ifBrowser.js +2 -0
- package/dist/jk_data/jBundler_ifBrowser.js.map +1 -0
- package/dist/jk_data/jBundler_ifServer.1.d.ts +2 -0
- package/dist/jk_data/jBundler_ifServer.1.js +2 -0
- package/dist/jk_data/jBundler_ifServer.1.js.map +1 -0
- package/dist/jk_data/jBundler_ifServer.d.ts +2 -0
- package/dist/jk_data/jBundler_ifServer.js +2 -0
- package/dist/jk_data/jBundler_ifServer.js.map +1 -0
- package/dist/jk_data/proxy.d.ts +24 -0
- package/dist/jk_data/proxy.js +82 -0
- package/dist/jk_data/proxy.js.map +1 -0
- package/dist/jk_dates/index.d.ts +4 -0
- package/dist/jk_dates/index.js +11 -0
- package/dist/jk_dates/index.js.map +1 -0
- package/dist/jk_fs/jBundler_ifServer.d.ts +2 -0
- package/dist/jk_fs/jBundler_ifServer.js +11 -1
- package/dist/jk_fs/jBundler_ifServer.js.map +1 -1
- package/dist/jk_memcache/index.d.ts +1 -0
- package/dist/jk_memcache/index.js +1 -5
- package/dist/jk_memcache/index.js.map +1 -1
- package/dist/jk_schemas/index.d.ts +20 -16
- package/dist/jk_schemas/index.js +13 -15
- package/dist/jk_schemas/index.js.map +1 -1
- package/dist/jk_tools/index.d.ts +1 -0
- package/package.json +8 -3
- package/src/jk_app/common.ts +12 -2
- package/src/jk_data/core.ts +92 -0
- package/src/jk_data/index.ts +3 -166
- package/src/jk_data/interfaces.ts +140 -0
- package/src/jk_data/proxy.ts +102 -0
- package/src/jk_dates/index.ts +12 -0
- package/src/jk_fs/jBundler_ifServer.ts +11 -1
- package/src/jk_memcache/index.ts +4 -8
- package/src/jk_schemas/index.ts +29 -27
- package/src/jk_tools/index.ts +3 -1
- package/src/jk_compress/index.js +0 -1
- package/src/jk_compress/jBundler_ifServer.js +0 -10
- package/src/jk_data/index.js +0 -155
- package/src/jk_schemas/index.js +0 -330
package/src/jk_schemas/index.ts
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
|
-
// noinspection JSUnusedGlobalSymbols
|
|
2
|
-
|
|
3
|
-
import {generateUUIDv4} from "jopi-toolkit/jk_tools";
|
|
4
|
-
|
|
5
1
|
//region Validation
|
|
6
2
|
|
|
3
|
+
import type { Translatable } from "jopi-toolkit/jk_tools";
|
|
4
|
+
|
|
7
5
|
/**
|
|
8
6
|
* Throwing this error allows it to be caught
|
|
9
7
|
* when validating an object.
|
|
@@ -174,7 +172,7 @@ interface RegistryEntry {
|
|
|
174
172
|
|
|
175
173
|
export function registerSchema(schemaId: string|undefined, schema: Schema, meta?: any) {
|
|
176
174
|
if (!schemaId) {
|
|
177
|
-
throw new Error("jk_schemas - Schema id required
|
|
175
|
+
throw new Error("jk_schemas - Schema id required");
|
|
178
176
|
}
|
|
179
177
|
|
|
180
178
|
gRegistry[schemaId!] = {schema, meta};
|
|
@@ -251,17 +249,20 @@ class SchemaImpl<T extends SchemaDescriptor> implements Schema {
|
|
|
251
249
|
}
|
|
252
250
|
}
|
|
253
251
|
|
|
254
|
-
export
|
|
255
|
-
[field: string]: Field;
|
|
256
|
-
}
|
|
252
|
+
export type SchemaDescriptor = Record<string, Field>;
|
|
257
253
|
|
|
258
254
|
export interface SchemaMeta {
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
255
|
+
/**
|
|
256
|
+
* The title of this schema.
|
|
257
|
+
* Can be translated to languages.
|
|
258
|
+
*/
|
|
259
|
+
title?: Translatable;
|
|
260
|
+
description?: Translatable;
|
|
262
261
|
|
|
263
262
|
normalize?: (allValues: any, checkHelper: ValueCheckingHelper) => void;
|
|
264
263
|
validate?: (allValues: any, checkHelper: ValueCheckingHelper) => void;
|
|
264
|
+
|
|
265
|
+
[key: string]: any;
|
|
265
266
|
}
|
|
266
267
|
|
|
267
268
|
export interface SchemaInfo {
|
|
@@ -415,10 +416,11 @@ export interface ScFieldStore {
|
|
|
415
416
|
}
|
|
416
417
|
|
|
417
418
|
export interface ScField<T, Opt extends boolean> {
|
|
418
|
-
|
|
419
|
+
id: string;
|
|
419
420
|
type: string;
|
|
421
|
+
title?: Translatable;
|
|
420
422
|
|
|
421
|
-
description?:
|
|
423
|
+
description?: Translatable;
|
|
422
424
|
default?: T;
|
|
423
425
|
optional?: Opt;
|
|
424
426
|
|
|
@@ -456,7 +458,7 @@ export interface ScField<T, Opt extends boolean> {
|
|
|
456
458
|
export type Field = ScField<any, any>;
|
|
457
459
|
export type SchemaFieldInfos = Field;
|
|
458
460
|
|
|
459
|
-
type OnlyInfos<T> = Omit<T, "
|
|
461
|
+
type OnlyInfos<T> = Omit<T, "id" | "optional" | "type">;
|
|
460
462
|
|
|
461
463
|
//endregion
|
|
462
464
|
|
|
@@ -471,16 +473,16 @@ export interface ScString<Opt extends boolean = boolean> extends ScField<string,
|
|
|
471
473
|
maxLength?: number;
|
|
472
474
|
errorMessage_maxLength?: string;
|
|
473
475
|
|
|
474
|
-
placeholder?:
|
|
476
|
+
placeholder?: Translatable;
|
|
475
477
|
}
|
|
476
478
|
|
|
477
|
-
export function string<Opt extends boolean>(
|
|
479
|
+
export function string<Opt extends boolean>(id: string, optional: Opt, infos?: OnlyInfos<ScString<Opt>>): ScString<Opt> {
|
|
478
480
|
if (!optional) {
|
|
479
481
|
if (!infos) infos = {};
|
|
480
482
|
if (infos.minLength===undefined) infos.minLength = 1;
|
|
481
483
|
}
|
|
482
484
|
|
|
483
|
-
return {...infos,
|
|
485
|
+
return {...infos, id, optional, type: "string"};
|
|
484
486
|
}
|
|
485
487
|
|
|
486
488
|
byTypeValidator["string"] = (v,f) => {
|
|
@@ -514,8 +516,8 @@ export interface ScBoolean<Opt extends boolean = boolean> extends ScField<boolea
|
|
|
514
516
|
errorMessage_requireFalse?: string;
|
|
515
517
|
}
|
|
516
518
|
|
|
517
|
-
export function boolean<Opt extends boolean>(
|
|
518
|
-
return {...infos,
|
|
519
|
+
export function boolean<Opt extends boolean>(id: string, optional: Opt, infos?: OnlyInfos<ScBoolean<Opt>>): ScBoolean<Opt> {
|
|
520
|
+
return {...infos, id, optional, type: "boolean"};
|
|
519
521
|
}
|
|
520
522
|
|
|
521
523
|
byTypeValidator["boolean"] = (v, f) => {
|
|
@@ -574,8 +576,8 @@ export interface ScNumber<Opt extends boolean = boolean> extends ScField<number,
|
|
|
574
576
|
currency?: string;
|
|
575
577
|
}
|
|
576
578
|
|
|
577
|
-
export function number<Opt extends boolean>(
|
|
578
|
-
return {...infos,
|
|
579
|
+
export function number<Opt extends boolean>(id: string, optional: Opt, infos?: OnlyInfos<ScNumber<Opt>>): ScNumber<Opt> {
|
|
580
|
+
return {...infos, id, optional, type: "number"};
|
|
579
581
|
}
|
|
580
582
|
|
|
581
583
|
export function formatNumber(value: string, fieldNumber: ScNumber, defaultLocalFormat: string = "en-US", defaultCurrency: string = "USD") {
|
|
@@ -616,16 +618,16 @@ byTypeValidator["number"] = (v,f) => {
|
|
|
616
618
|
|
|
617
619
|
//region Currency
|
|
618
620
|
|
|
619
|
-
export function currency<Opt extends boolean>(
|
|
620
|
-
return number(
|
|
621
|
+
export function currency<Opt extends boolean>(id: string, optional: Opt, infos?: OnlyInfos<ScNumber<Opt>>): ScNumber<Opt> {
|
|
622
|
+
return number(id, optional, {...infos, displayType: "currency"})
|
|
621
623
|
}
|
|
622
624
|
|
|
623
625
|
//endregion
|
|
624
626
|
|
|
625
627
|
//region Percent
|
|
626
628
|
|
|
627
|
-
export function percent<Opt extends boolean>(
|
|
628
|
-
return number(
|
|
629
|
+
export function percent<Opt extends boolean>(id: string, optional: Opt, infos?: OnlyInfos<ScNumber<Opt>>): ScNumber<Opt> {
|
|
630
|
+
return number(id, optional, {...infos, displayType: "percent"})
|
|
629
631
|
}
|
|
630
632
|
|
|
631
633
|
|
|
@@ -650,8 +652,8 @@ export interface ScFile<Opt extends boolean> extends ScField<File[], Opt> {
|
|
|
650
652
|
errorMessage_maxFileSize?: string;
|
|
651
653
|
}
|
|
652
654
|
|
|
653
|
-
export function file<Opt extends boolean>(
|
|
654
|
-
return {...infos,
|
|
655
|
+
export function file<Opt extends boolean>(id: string, optional: Opt, infos?: OnlyInfos<ScFile<Opt>>): ScFile<Opt> {
|
|
656
|
+
return {...infos, id, optional, type: "file"};
|
|
655
657
|
}
|
|
656
658
|
|
|
657
659
|
//endregion
|
package/src/jk_tools/index.ts
CHANGED
package/src/jk_compress/index.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from "./jBundler_ifServer.ts";
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import { gunzipSync as n_gunzipSync, gzipSync as n_gzipSync } from "node:zlib";
|
|
2
|
-
import { isNodeJS } from "jopi-toolkit/jk_what";
|
|
3
|
-
function node_gunzipSync(data) {
|
|
4
|
-
return n_gunzipSync(Buffer.from(data));
|
|
5
|
-
}
|
|
6
|
-
function node_gzipSync(data) {
|
|
7
|
-
return n_gzipSync(Buffer.from(data));
|
|
8
|
-
}
|
|
9
|
-
export var gunzipSync = isNodeJS ? node_gunzipSync : Bun.gunzipSync;
|
|
10
|
-
export var gzipSync = isNodeJS ? node_gzipSync : Bun.gzipSync;
|
package/src/jk_data/index.js
DELETED
|
@@ -1,155 +0,0 @@
|
|
|
1
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
-
});
|
|
9
|
-
};
|
|
10
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
11
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
|
12
|
-
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
13
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
14
|
-
function step(op) {
|
|
15
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
16
|
-
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
17
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
18
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
19
|
-
switch (op[0]) {
|
|
20
|
-
case 0: case 1: t = op; break;
|
|
21
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
22
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
23
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
24
|
-
default:
|
|
25
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
26
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
27
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
28
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
29
|
-
if (t[2]) _.ops.pop();
|
|
30
|
-
_.trys.pop(); continue;
|
|
31
|
-
}
|
|
32
|
-
op = body.call(thisArg, _);
|
|
33
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
34
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
35
|
-
}
|
|
36
|
-
};
|
|
37
|
-
/**
|
|
38
|
-
* Filter the row content according to rules.
|
|
39
|
-
*/
|
|
40
|
-
export function simpleRowArrayFilter(rows, params) {
|
|
41
|
-
// > Apply filter.
|
|
42
|
-
if (params.filter) {
|
|
43
|
-
var f_1 = params.filter;
|
|
44
|
-
rows = rows.filter(function (r) {
|
|
45
|
-
if (f_1.field) {
|
|
46
|
-
var v = r[f_1.field];
|
|
47
|
-
if (v === undefined)
|
|
48
|
-
return false;
|
|
49
|
-
return String(v).includes(f_1.value);
|
|
50
|
-
}
|
|
51
|
-
else {
|
|
52
|
-
for (var _i = 0, _a = Object.values(r); _i < _a.length; _i++) {
|
|
53
|
-
var v = _a[_i];
|
|
54
|
-
if (v === undefined)
|
|
55
|
-
continue;
|
|
56
|
-
if (String(v).includes(f_1.value))
|
|
57
|
-
return true;
|
|
58
|
-
}
|
|
59
|
-
return false;
|
|
60
|
-
}
|
|
61
|
-
});
|
|
62
|
-
}
|
|
63
|
-
// > Apply sorting.
|
|
64
|
-
if (params.sorting && params.sorting.length) {
|
|
65
|
-
var sorting = params.sorting[0];
|
|
66
|
-
var sortField_1 = sorting.id;
|
|
67
|
-
var sortDesc_1 = sorting.desc;
|
|
68
|
-
rows = rows.sort(function (a, b) {
|
|
69
|
-
var av = a[sortField_1];
|
|
70
|
-
var bv = b[sortField_1];
|
|
71
|
-
if (av === undefined)
|
|
72
|
-
av = "";
|
|
73
|
-
if (bv === undefined)
|
|
74
|
-
bv = "";
|
|
75
|
-
var avIsNumber = typeof av === "number";
|
|
76
|
-
var bvIsNumber = typeof bv === "number";
|
|
77
|
-
if (avIsNumber && bvIsNumber) {
|
|
78
|
-
if (sortDesc_1) {
|
|
79
|
-
return bv - av;
|
|
80
|
-
}
|
|
81
|
-
else {
|
|
82
|
-
return av - bv;
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
else {
|
|
86
|
-
var avStr = String(av);
|
|
87
|
-
var bvStr = String(bv);
|
|
88
|
-
if (sortDesc_1) {
|
|
89
|
-
return bvStr.localeCompare(avStr);
|
|
90
|
-
}
|
|
91
|
-
else {
|
|
92
|
-
return avStr.localeCompare(bvStr);
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
});
|
|
96
|
-
}
|
|
97
|
-
var totalWithoutPagination = rows.length;
|
|
98
|
-
var offset = 0;
|
|
99
|
-
if (params.page) {
|
|
100
|
-
offset = params.page.pageIndex * params.page.pageSize;
|
|
101
|
-
rows = rows.slice(offset, offset + params.page.pageSize);
|
|
102
|
-
}
|
|
103
|
-
return { rows: rows, total: totalWithoutPagination, offset: offset };
|
|
104
|
-
}
|
|
105
|
-
var JTableDs_UseArray = /** @class */ (function () {
|
|
106
|
-
function JTableDs_UseArray(name, schema, rows) {
|
|
107
|
-
this.name = name;
|
|
108
|
-
this.schema = schema;
|
|
109
|
-
this.rows = rows;
|
|
110
|
-
}
|
|
111
|
-
JTableDs_UseArray.prototype.read = function (params) {
|
|
112
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
113
|
-
return __generator(this, function (_a) {
|
|
114
|
-
return [2 /*return*/, simpleRowArrayFilter(this.rows, params)];
|
|
115
|
-
});
|
|
116
|
-
});
|
|
117
|
-
};
|
|
118
|
-
return JTableDs_UseArray;
|
|
119
|
-
}());
|
|
120
|
-
export { JTableDs_UseArray };
|
|
121
|
-
var JTableDs_HttpProxy = /** @class */ (function () {
|
|
122
|
-
function JTableDs_HttpProxy(name, url, schema) {
|
|
123
|
-
this.name = name;
|
|
124
|
-
this.url = url;
|
|
125
|
-
this.schema = schema;
|
|
126
|
-
}
|
|
127
|
-
JTableDs_HttpProxy.prototype.read = function (params) {
|
|
128
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
129
|
-
var toSend, res, asJson;
|
|
130
|
-
return __generator(this, function (_a) {
|
|
131
|
-
switch (_a.label) {
|
|
132
|
-
case 0:
|
|
133
|
-
toSend = { dsName: this.name, read: params };
|
|
134
|
-
return [4 /*yield*/, fetch(this.url, {
|
|
135
|
-
method: "POST",
|
|
136
|
-
headers: { "Content-Type": "application/json" },
|
|
137
|
-
body: JSON.stringify(toSend)
|
|
138
|
-
})];
|
|
139
|
-
case 1:
|
|
140
|
-
res = _a.sent();
|
|
141
|
-
if (res.status !== 200) {
|
|
142
|
-
throw new Error("Error while reading data source \"".concat(this.name, "\""));
|
|
143
|
-
}
|
|
144
|
-
return [4 /*yield*/, res.json()];
|
|
145
|
-
case 2:
|
|
146
|
-
asJson = _a.sent();
|
|
147
|
-
return [2 /*return*/, asJson];
|
|
148
|
-
}
|
|
149
|
-
});
|
|
150
|
-
});
|
|
151
|
-
};
|
|
152
|
-
return JTableDs_HttpProxy;
|
|
153
|
-
}());
|
|
154
|
-
export { JTableDs_HttpProxy };
|
|
155
|
-
//endregion
|
package/src/jk_schemas/index.js
DELETED
|
@@ -1,330 +0,0 @@
|
|
|
1
|
-
// noinspection JSUnusedGlobalSymbols
|
|
2
|
-
var __extends = (this && this.__extends) || (function () {
|
|
3
|
-
var extendStatics = function (d, b) {
|
|
4
|
-
extendStatics = Object.setPrototypeOf ||
|
|
5
|
-
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
6
|
-
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
7
|
-
return extendStatics(d, b);
|
|
8
|
-
};
|
|
9
|
-
return function (d, b) {
|
|
10
|
-
if (typeof b !== "function" && b !== null)
|
|
11
|
-
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
12
|
-
extendStatics(d, b);
|
|
13
|
-
function __() { this.constructor = d; }
|
|
14
|
-
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
15
|
-
};
|
|
16
|
-
})();
|
|
17
|
-
var __assign = (this && this.__assign) || function () {
|
|
18
|
-
__assign = Object.assign || function(t) {
|
|
19
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
20
|
-
s = arguments[i];
|
|
21
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
22
|
-
t[p] = s[p];
|
|
23
|
-
}
|
|
24
|
-
return t;
|
|
25
|
-
};
|
|
26
|
-
return __assign.apply(this, arguments);
|
|
27
|
-
};
|
|
28
|
-
import { generateUUIDv4 } from "jopi-toolkit/jk_tools";
|
|
29
|
-
//region Validation
|
|
30
|
-
/**
|
|
31
|
-
* Throwing this error allows it to be caught
|
|
32
|
-
* when validating an object.
|
|
33
|
-
*/
|
|
34
|
-
var SchemaError = /** @class */ (function (_super) {
|
|
35
|
-
__extends(SchemaError, _super);
|
|
36
|
-
function SchemaError(errorMessage, errorCode) {
|
|
37
|
-
var _this = _super.call(this, "") || this;
|
|
38
|
-
_this.errorMessage = errorMessage;
|
|
39
|
-
_this.errorCode = errorCode;
|
|
40
|
-
return _this;
|
|
41
|
-
}
|
|
42
|
-
return SchemaError;
|
|
43
|
-
}(Error));
|
|
44
|
-
/**
|
|
45
|
-
* Declare an error when validating a schema.
|
|
46
|
-
* Must be called when validating or normalizing.
|
|
47
|
-
*/
|
|
48
|
-
export function declareError(message, errorCode) {
|
|
49
|
-
throw new SchemaError(message, errorCode);
|
|
50
|
-
}
|
|
51
|
-
export function validateSchema(data, schema) {
|
|
52
|
-
// Normalize the data.
|
|
53
|
-
// It's a step where we apply automatic corrections.
|
|
54
|
-
//
|
|
55
|
-
if (schema.schemaMeta.normalize) {
|
|
56
|
-
try {
|
|
57
|
-
schema.schemaMeta.normalize(data, gValueCheckingHelper);
|
|
58
|
-
}
|
|
59
|
-
catch (e) {
|
|
60
|
-
if (e instanceof SchemaError) {
|
|
61
|
-
return {
|
|
62
|
-
globalError: e.errorMessage || "Schema validation failed",
|
|
63
|
-
globalErrorCode: e.errorCode || "SCHEMA_VALIDATION_FAILED"
|
|
64
|
-
};
|
|
65
|
-
}
|
|
66
|
-
else {
|
|
67
|
-
throw e;
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
// >>> Check each field individually.
|
|
72
|
-
// Each time it will:
|
|
73
|
-
// - Normalize the value.
|
|
74
|
-
// - Check if optional + undefined.
|
|
75
|
-
// - Apply validator for the field type.
|
|
76
|
-
var fieldErrors;
|
|
77
|
-
for (var fieldName in schema.desc) {
|
|
78
|
-
var defaultErrorMessage = void 0;
|
|
79
|
-
try {
|
|
80
|
-
var field = schema.desc[fieldName];
|
|
81
|
-
var value = data[fieldName];
|
|
82
|
-
if (field.normalize) {
|
|
83
|
-
defaultErrorMessage = field.errorMessage_theValueIsInvalid;
|
|
84
|
-
field.normalize(value, data, gValueCheckingHelper);
|
|
85
|
-
}
|
|
86
|
-
if (!field.optional) {
|
|
87
|
-
if (value === undefined) {
|
|
88
|
-
if (field.errorMessage_isRequired) {
|
|
89
|
-
declareError(field.errorMessage_isRequired, "VALUE_REQUIRED");
|
|
90
|
-
}
|
|
91
|
-
else if (field.errorMessage_theValueIsInvalid) {
|
|
92
|
-
declareError(field.errorMessage_theValueIsInvalid, "VALUE_REQUIRED");
|
|
93
|
-
}
|
|
94
|
-
else {
|
|
95
|
-
declareError("Field ".concat(fieldName, " is required"), "VALUE_REQUIRED");
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
var typeValidator = byTypeValidator[field.type];
|
|
100
|
-
if (typeValidator) {
|
|
101
|
-
typeValidator(value, field);
|
|
102
|
-
}
|
|
103
|
-
if (field.validator) {
|
|
104
|
-
defaultErrorMessage = field.errorMessage_theValueIsInvalid;
|
|
105
|
-
field.validator(value, data, gValueCheckingHelper);
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
catch (e) {
|
|
109
|
-
if (e instanceof SchemaError) {
|
|
110
|
-
if (!fieldErrors)
|
|
111
|
-
fieldErrors = {};
|
|
112
|
-
fieldErrors[fieldName] = {
|
|
113
|
-
fieldName: fieldName,
|
|
114
|
-
message: e.errorMessage || defaultErrorMessage || "Field ".concat(fieldName, " is invalid"),
|
|
115
|
-
code: e.errorCode || "FIELD_VALIDATION_FAILED"
|
|
116
|
-
};
|
|
117
|
-
}
|
|
118
|
-
else {
|
|
119
|
-
throw e;
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
// >>> Validate the whole fields.
|
|
124
|
-
// Allow validating if values are ok with each others.
|
|
125
|
-
if (schema.schemaMeta.validate) {
|
|
126
|
-
try {
|
|
127
|
-
schema.schemaMeta.validate(data, gValueCheckingHelper);
|
|
128
|
-
}
|
|
129
|
-
catch (e) {
|
|
130
|
-
if (e instanceof SchemaError) {
|
|
131
|
-
return {
|
|
132
|
-
globalError: e.errorMessage || "Schema validation failed",
|
|
133
|
-
globalErrorCode: e.errorCode || "SCHEMA_VALIDATION_FAILED",
|
|
134
|
-
fields: fieldErrors
|
|
135
|
-
};
|
|
136
|
-
}
|
|
137
|
-
else {
|
|
138
|
-
throw e;
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
// No error ? --> undefined.
|
|
143
|
-
// Otherwise returns the errors.
|
|
144
|
-
//
|
|
145
|
-
if (!fieldErrors)
|
|
146
|
-
return undefined;
|
|
147
|
-
return { fields: fieldErrors };
|
|
148
|
-
}
|
|
149
|
-
var byTypeValidator = {};
|
|
150
|
-
/**
|
|
151
|
-
* A helper allowing to make field validation easier.
|
|
152
|
-
* Is sent to normalize and validate functions.
|
|
153
|
-
*/
|
|
154
|
-
var ValueCheckingHelper = /** @class */ (function () {
|
|
155
|
-
function ValueCheckingHelper() {
|
|
156
|
-
}
|
|
157
|
-
ValueCheckingHelper.prototype.declareError = function (message, errorCode) {
|
|
158
|
-
throw new SchemaError(message, errorCode);
|
|
159
|
-
};
|
|
160
|
-
return ValueCheckingHelper;
|
|
161
|
-
}());
|
|
162
|
-
var gValueCheckingHelper = new ValueCheckingHelper();
|
|
163
|
-
export function registerSchema(schemaId, schema, meta) {
|
|
164
|
-
if (!schemaId) {
|
|
165
|
-
throw new Error("jk_schemas - Schema id required. If you need an uid you can use: " + generateUUIDv4());
|
|
166
|
-
}
|
|
167
|
-
gRegistry[schemaId] = { schema: schema, meta: meta };
|
|
168
|
-
}
|
|
169
|
-
export function getSchemaMeta(schemaId) {
|
|
170
|
-
var entry = gRegistry[schemaId];
|
|
171
|
-
if (entry)
|
|
172
|
-
return entry.schema;
|
|
173
|
-
return undefined;
|
|
174
|
-
}
|
|
175
|
-
export function getSchema(schemaId) {
|
|
176
|
-
var entry = gRegistry[schemaId];
|
|
177
|
-
if (entry)
|
|
178
|
-
return entry.schema;
|
|
179
|
-
return undefined;
|
|
180
|
-
}
|
|
181
|
-
export function requireSchema(schemaId) {
|
|
182
|
-
var s = getSchema(schemaId);
|
|
183
|
-
if (!s) {
|
|
184
|
-
throw new Error("jk_schemas - Schema ".concat(schemaId, " not found"));
|
|
185
|
-
}
|
|
186
|
-
return s;
|
|
187
|
-
}
|
|
188
|
-
var gRegistry = {};
|
|
189
|
-
//endregion
|
|
190
|
-
//region Schema
|
|
191
|
-
export function schema(descriptor, meta) {
|
|
192
|
-
return new SchemaImpl(descriptor, meta || {});
|
|
193
|
-
}
|
|
194
|
-
var SchemaImpl = /** @class */ (function () {
|
|
195
|
-
function SchemaImpl(desc, schemaMeta) {
|
|
196
|
-
this.desc = desc;
|
|
197
|
-
this.schemaMeta = schemaMeta;
|
|
198
|
-
}
|
|
199
|
-
SchemaImpl.prototype.toJson = function () {
|
|
200
|
-
return toJson(this);
|
|
201
|
-
};
|
|
202
|
-
SchemaImpl.prototype.addDataNormalizer = function (f) {
|
|
203
|
-
if (!this.schemaMeta.normalize) {
|
|
204
|
-
this.schemaMeta.normalize = f;
|
|
205
|
-
}
|
|
206
|
-
var f1 = this.schemaMeta.normalize;
|
|
207
|
-
this.schemaMeta.normalize = function (values, helper) {
|
|
208
|
-
f1(values, helper);
|
|
209
|
-
f(values, helper);
|
|
210
|
-
};
|
|
211
|
-
return this;
|
|
212
|
-
};
|
|
213
|
-
SchemaImpl.prototype.addDataValidator = function (f) {
|
|
214
|
-
if (!this.schemaMeta.validate) {
|
|
215
|
-
this.schemaMeta.validate = f;
|
|
216
|
-
}
|
|
217
|
-
var f1 = this.schemaMeta.validate;
|
|
218
|
-
this.schemaMeta.validate = function (values, helper) {
|
|
219
|
-
f1(values, helper);
|
|
220
|
-
f(values, helper);
|
|
221
|
-
};
|
|
222
|
-
return this;
|
|
223
|
-
};
|
|
224
|
-
return SchemaImpl;
|
|
225
|
-
}());
|
|
226
|
-
export function toJson(schema) {
|
|
227
|
-
return schema;
|
|
228
|
-
}
|
|
229
|
-
export function string(title, optional, infos) {
|
|
230
|
-
if (!optional) {
|
|
231
|
-
if (!infos)
|
|
232
|
-
infos = {};
|
|
233
|
-
if (infos.minLength === undefined)
|
|
234
|
-
infos.minLength = 1;
|
|
235
|
-
}
|
|
236
|
-
return __assign(__assign({}, infos), { title: title, optional: optional, type: "string" });
|
|
237
|
-
}
|
|
238
|
-
byTypeValidator["string"] = function (v, f) {
|
|
239
|
-
if (typeof v !== "string") {
|
|
240
|
-
declareError(f.errorMessage_theValueIsInvalid || "Value must be a string", "INVALID_TYPE");
|
|
241
|
-
return;
|
|
242
|
-
}
|
|
243
|
-
var sf = f;
|
|
244
|
-
if ((sf.minLength !== undefined) && (v.length < sf.minLength)) {
|
|
245
|
-
declareError(sf.errorMessage_minLength || "Value must be at least ".concat(sf.minLength, " characters long"), "INVALID_LENGTH");
|
|
246
|
-
return;
|
|
247
|
-
}
|
|
248
|
-
if ((sf.maxLength !== undefined) && (v.length > sf.maxLength)) {
|
|
249
|
-
declareError(sf.errorMessage_maxLength || "Value must be less than ".concat(sf.maxLength, " characters long"), "INVALID_LENGTH");
|
|
250
|
-
return;
|
|
251
|
-
}
|
|
252
|
-
};
|
|
253
|
-
export function boolean(title, optional, infos) {
|
|
254
|
-
return __assign(__assign({}, infos), { title: title, optional: optional, type: "boolean" });
|
|
255
|
-
}
|
|
256
|
-
byTypeValidator["boolean"] = function (v, f) {
|
|
257
|
-
if (typeof v !== "boolean") {
|
|
258
|
-
declareError(f.errorMessage_theValueIsInvalid || "Value must be a boolean", "INVALID_TYPE");
|
|
259
|
-
}
|
|
260
|
-
var sf = f;
|
|
261
|
-
if (sf.requireTrue) {
|
|
262
|
-
if (v !== true) {
|
|
263
|
-
declareError(sf.errorMessage_requireTrue || "Value must be true", "INVALID_VALUE");
|
|
264
|
-
}
|
|
265
|
-
}
|
|
266
|
-
else if (sf.requireFalse) {
|
|
267
|
-
if (v !== false) {
|
|
268
|
-
declareError(sf.errorMessage_requireFalse || "Value must be false", "INVALID_VALUE");
|
|
269
|
-
}
|
|
270
|
-
}
|
|
271
|
-
};
|
|
272
|
-
export function number(title, optional, infos) {
|
|
273
|
-
return __assign(__assign({}, infos), { title: title, optional: optional, type: "number" });
|
|
274
|
-
}
|
|
275
|
-
export function formatNumber(value, fieldNumber, defaultLocalFormat, defaultCurrency) {
|
|
276
|
-
if (defaultLocalFormat === void 0) { defaultLocalFormat = "en-US"; }
|
|
277
|
-
if (defaultCurrency === void 0) { defaultCurrency = "USD"; }
|
|
278
|
-
var amount = parseFloat(value);
|
|
279
|
-
var localFormat = fieldNumber.localFormat || defaultLocalFormat;
|
|
280
|
-
switch (fieldNumber.displayType) {
|
|
281
|
-
case "currency":
|
|
282
|
-
return new Intl.NumberFormat(localFormat, {
|
|
283
|
-
style: "currency",
|
|
284
|
-
currency: fieldNumber.currency || defaultCurrency,
|
|
285
|
-
}).format(amount);
|
|
286
|
-
default:
|
|
287
|
-
return new Intl.NumberFormat(localFormat, { style: fieldNumber.displayType || "decimal" }).format(amount);
|
|
288
|
-
}
|
|
289
|
-
}
|
|
290
|
-
byTypeValidator["number"] = function (v, f) {
|
|
291
|
-
if (typeof v !== "number") {
|
|
292
|
-
declareError(f.errorMessage_theValueIsInvalid || "Value must be a number", "INVALID_TYPE");
|
|
293
|
-
}
|
|
294
|
-
var sf = f;
|
|
295
|
-
if ((sf.minValue !== undefined) && (v < sf.minValue)) {
|
|
296
|
-
declareError(sf.errorMessage_minValue || "Value must be at least ".concat(sf.minValue), "INVALID_LENGTH");
|
|
297
|
-
return;
|
|
298
|
-
}
|
|
299
|
-
if ((sf.maxValue !== undefined) && (v > sf.maxValue)) {
|
|
300
|
-
declareError(sf.errorMessage_maxValue || "Value must be less than ".concat(sf.maxValue), "INVALID_LENGTH");
|
|
301
|
-
return;
|
|
302
|
-
}
|
|
303
|
-
};
|
|
304
|
-
//endregion
|
|
305
|
-
//region Currency
|
|
306
|
-
export function currency(title, optional, infos) {
|
|
307
|
-
return number(title, optional, __assign(__assign({}, infos), { displayType: "currency" }));
|
|
308
|
-
}
|
|
309
|
-
//endregion
|
|
310
|
-
//region Percent
|
|
311
|
-
export function percent(title, optional, infos) {
|
|
312
|
-
return number(title, optional, __assign(__assign({}, infos), { displayType: "percent" }));
|
|
313
|
-
}
|
|
314
|
-
export function file(title, optional, infos) {
|
|
315
|
-
return __assign(__assign({}, infos), { title: title, optional: optional, type: "file" });
|
|
316
|
-
}
|
|
317
|
-
//endregion
|
|
318
|
-
//endregion
|
|
319
|
-
/*const MAKE_OPTIONAL = true;
|
|
320
|
-
//
|
|
321
|
-
const UserSchema1 = schema({
|
|
322
|
-
testOptional: string("testOptional", true),
|
|
323
|
-
testString: string("testString", MAKE_OPTIONAL),
|
|
324
|
-
testBool: boolean("testBool", MAKE_OPTIONAL),
|
|
325
|
-
testNumber: number("testNumber", MAKE_OPTIONAL),
|
|
326
|
-
testFile: file("testFile", MAKE_OPTIONAL)
|
|
327
|
-
})
|
|
328
|
-
|
|
329
|
-
type UserDataType1 = SchemaToType<typeof UserSchema1>;
|
|
330
|
-
let ud1: UserDataType1 = {};*/
|