@naturalcycles/db-lib 10.7.0 → 10.8.0
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.
|
@@ -962,7 +962,14 @@ export class CommonDao {
|
|
|
962
962
|
const objectName = table;
|
|
963
963
|
let error;
|
|
964
964
|
let convertedValue;
|
|
965
|
-
if (
|
|
965
|
+
if (this.cfg.validateBM) {
|
|
966
|
+
const [err, value] = this.cfg.validateBM(obj, {
|
|
967
|
+
itemName: table,
|
|
968
|
+
});
|
|
969
|
+
error = err;
|
|
970
|
+
convertedValue = value;
|
|
971
|
+
}
|
|
972
|
+
else if (schema instanceof ZodType) {
|
|
966
973
|
// Zod schema
|
|
967
974
|
const vr = zSafeValidate(obj, schema);
|
|
968
975
|
error = vr.error;
|
|
@@ -977,14 +984,7 @@ export class CommonDao {
|
|
|
977
984
|
}
|
|
978
985
|
else {
|
|
979
986
|
// Joi
|
|
980
|
-
const start = localTime.nowUnixMillis();
|
|
981
987
|
const vr = getValidationResult(obj, schema, objectName);
|
|
982
|
-
const tookMillis = localTime.nowUnixMillis() - start;
|
|
983
|
-
this.cfg.onValidationTime?.({
|
|
984
|
-
tookMillis,
|
|
985
|
-
table,
|
|
986
|
-
obj,
|
|
987
|
-
});
|
|
988
988
|
error = vr.error;
|
|
989
989
|
convertedValue = vr.value;
|
|
990
990
|
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { ValidationFunction } from '@naturalcycles/js-lib';
|
|
1
2
|
import type { ErrorMode } from '@naturalcycles/js-lib/error';
|
|
2
3
|
import type { CommonLogger } from '@naturalcycles/js-lib/log';
|
|
3
4
|
import type { BaseDBEntity, NumberOfMilliseconds, Promisable, UnixTimestamp } from '@naturalcycles/js-lib/types';
|
|
@@ -105,6 +106,15 @@ export interface CommonDaoCfg<BM extends BaseDBEntity, DBM extends BaseDBEntity
|
|
|
105
106
|
* Joi, AjvSchema or ZodSchema is supported.
|
|
106
107
|
*/
|
|
107
108
|
bmSchema?: ObjectSchema<BM> | AjvSchema<BM> | ZodType<BM>;
|
|
109
|
+
/**
|
|
110
|
+
* Experimental alternative to bmSchema.
|
|
111
|
+
* "Bring your own validation function".
|
|
112
|
+
* It removes the knowledge from CommonDao about the validation library used
|
|
113
|
+
* and abstracts it away.
|
|
114
|
+
*
|
|
115
|
+
* @experimental
|
|
116
|
+
*/
|
|
117
|
+
validateBM?: ValidationFunction<BM, any>;
|
|
108
118
|
excludeFromIndexes?: (keyof DBM)[];
|
|
109
119
|
/**
|
|
110
120
|
* Defaults to true.
|
|
@@ -116,12 +126,6 @@ export interface CommonDaoCfg<BM extends BaseDBEntity, DBM extends BaseDBEntity
|
|
|
116
126
|
* If set to false - save (write) operations will skip validation (and conversion).
|
|
117
127
|
*/
|
|
118
128
|
validateOnSave?: boolean;
|
|
119
|
-
/**
|
|
120
|
-
* The hook allows to get a callback, to instrument "validation time".
|
|
121
|
-
*
|
|
122
|
-
* @experimental
|
|
123
|
-
*/
|
|
124
|
-
onValidationTime?: (data: OnValidationTimeData) => void;
|
|
125
129
|
/**
|
|
126
130
|
* Defaults to false.
|
|
127
131
|
* Setting it to true will set saveMethod to `insert` for save/saveBatch, which will
|
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@naturalcycles/db-lib",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "10.
|
|
4
|
+
"version": "10.8.0",
|
|
5
5
|
"dependencies": {
|
|
6
6
|
"@naturalcycles/js-lib": "^15",
|
|
7
7
|
"@naturalcycles/nodejs-lib": "^15"
|
|
8
8
|
},
|
|
9
9
|
"devDependencies": {
|
|
10
|
-
"@naturalcycles/dev-lib": "
|
|
10
|
+
"@naturalcycles/dev-lib": "18.4.2"
|
|
11
11
|
},
|
|
12
12
|
"files": [
|
|
13
13
|
"dist",
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { ValidationFunction } from '@naturalcycles/js-lib'
|
|
1
2
|
import type { ErrorMode } from '@naturalcycles/js-lib/error'
|
|
2
3
|
import type { CommonLogger } from '@naturalcycles/js-lib/log'
|
|
3
4
|
import type {
|
|
@@ -132,6 +133,16 @@ export interface CommonDaoCfg<
|
|
|
132
133
|
*/
|
|
133
134
|
bmSchema?: ObjectSchema<BM> | AjvSchema<BM> | ZodType<BM>
|
|
134
135
|
|
|
136
|
+
/**
|
|
137
|
+
* Experimental alternative to bmSchema.
|
|
138
|
+
* "Bring your own validation function".
|
|
139
|
+
* It removes the knowledge from CommonDao about the validation library used
|
|
140
|
+
* and abstracts it away.
|
|
141
|
+
*
|
|
142
|
+
* @experimental
|
|
143
|
+
*/
|
|
144
|
+
validateBM?: ValidationFunction<BM, any>
|
|
145
|
+
|
|
135
146
|
excludeFromIndexes?: (keyof DBM)[]
|
|
136
147
|
|
|
137
148
|
/**
|
|
@@ -146,13 +157,6 @@ export interface CommonDaoCfg<
|
|
|
146
157
|
*/
|
|
147
158
|
validateOnSave?: boolean
|
|
148
159
|
|
|
149
|
-
/**
|
|
150
|
-
* The hook allows to get a callback, to instrument "validation time".
|
|
151
|
-
*
|
|
152
|
-
* @experimental
|
|
153
|
-
*/
|
|
154
|
-
onValidationTime?: (data: OnValidationTimeData) => void
|
|
155
|
-
|
|
156
160
|
/**
|
|
157
161
|
* Defaults to false.
|
|
158
162
|
* Setting it to true will set saveMethod to `insert` for save/saveBatch, which will
|
|
@@ -1240,7 +1240,13 @@ export class CommonDao<BM extends BaseDBEntity, DBM extends BaseDBEntity = BM, I
|
|
|
1240
1240
|
let error: JoiValidationError | AjvValidationError | ZodValidationError | undefined
|
|
1241
1241
|
let convertedValue: any
|
|
1242
1242
|
|
|
1243
|
-
if (
|
|
1243
|
+
if (this.cfg.validateBM) {
|
|
1244
|
+
const [err, value] = this.cfg.validateBM(obj as any as BM, {
|
|
1245
|
+
itemName: table,
|
|
1246
|
+
})
|
|
1247
|
+
error = err
|
|
1248
|
+
convertedValue = value
|
|
1249
|
+
} else if (schema instanceof ZodType) {
|
|
1244
1250
|
// Zod schema
|
|
1245
1251
|
const vr = zSafeValidate(obj as T, schema)
|
|
1246
1252
|
error = vr.error
|
|
@@ -1254,16 +1260,7 @@ export class CommonDao<BM extends BaseDBEntity, DBM extends BaseDBEntity = BM, I
|
|
|
1254
1260
|
})
|
|
1255
1261
|
} else {
|
|
1256
1262
|
// Joi
|
|
1257
|
-
const start = localTime.nowUnixMillis()
|
|
1258
1263
|
const vr = getValidationResult(obj, schema, objectName)
|
|
1259
|
-
const tookMillis = localTime.nowUnixMillis() - start
|
|
1260
|
-
|
|
1261
|
-
this.cfg.onValidationTime?.({
|
|
1262
|
-
tookMillis,
|
|
1263
|
-
table,
|
|
1264
|
-
obj,
|
|
1265
|
-
})
|
|
1266
|
-
|
|
1267
1264
|
error = vr.error
|
|
1268
1265
|
convertedValue = vr.value
|
|
1269
1266
|
}
|