@naturalcycles/db-lib 10.10.0 → 10.11.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.
|
@@ -954,20 +954,22 @@ export class CommonDao {
|
|
|
954
954
|
// and they can be annoying with snapshot tests
|
|
955
955
|
obj = _filterUndefinedValues(obj);
|
|
956
956
|
// Return as is if no schema is passed or if `skipConversion` is set
|
|
957
|
-
if (!schema ||
|
|
957
|
+
if ((!schema && !this.cfg.validateBM) ||
|
|
958
958
|
opt.skipValidation ||
|
|
959
959
|
(op === 'load' && !this.cfg.validateOnLoad) ||
|
|
960
960
|
(op === 'save' && !this.cfg.validateOnSave)) {
|
|
961
961
|
return obj;
|
|
962
962
|
}
|
|
963
|
-
|
|
964
|
-
const table = opt.table || this.cfg.table;
|
|
965
|
-
const objectName = table;
|
|
963
|
+
const inputName = opt.table || this.cfg.table;
|
|
966
964
|
let error;
|
|
967
965
|
let convertedValue;
|
|
968
966
|
if (this.cfg.validateBM) {
|
|
969
967
|
const [err, value] = this.cfg.validateBM(obj, {
|
|
970
|
-
|
|
968
|
+
// Passing `mutateInput` through allows to do opt-in mutation
|
|
969
|
+
// for individual operations, e.g `someDao.save(myObj, { mutateInput: true })`,
|
|
970
|
+
// while still keeping safe non-mutating behavior by default
|
|
971
|
+
mutateInput: opt.mutateInput,
|
|
972
|
+
inputName,
|
|
971
973
|
});
|
|
972
974
|
error = err;
|
|
973
975
|
convertedValue = value;
|
|
@@ -981,14 +983,14 @@ export class CommonDao {
|
|
|
981
983
|
else if (schema instanceof AjvSchema) {
|
|
982
984
|
// Ajv schema
|
|
983
985
|
const [err, value] = schema.getValidationResult(obj, {
|
|
984
|
-
|
|
986
|
+
inputName,
|
|
985
987
|
});
|
|
986
988
|
error = err;
|
|
987
989
|
convertedValue = value;
|
|
988
990
|
}
|
|
989
991
|
else {
|
|
990
992
|
// Joi
|
|
991
|
-
const [err, value] = getValidationResult(obj, schema,
|
|
993
|
+
const [err, value] = getValidationResult(obj, schema, inputName);
|
|
992
994
|
error = err;
|
|
993
995
|
convertedValue = value;
|
|
994
996
|
}
|
|
@@ -104,6 +104,8 @@ export interface CommonDaoCfg<BM extends BaseDBEntity, DBM extends BaseDBEntity
|
|
|
104
104
|
table: string;
|
|
105
105
|
/**
|
|
106
106
|
* Joi, AjvSchema or ZodSchema is supported.
|
|
107
|
+
*
|
|
108
|
+
* @deprecated - use `validateBM` instead.
|
|
107
109
|
*/
|
|
108
110
|
bmSchema?: ObjectSchema<BM> | AjvSchema<BM> | ZodType<BM>;
|
|
109
111
|
/**
|
|
@@ -190,10 +192,22 @@ export interface CommonDaoOptions extends CommonDBOptions {
|
|
|
190
192
|
/**
|
|
191
193
|
* Defaults to false.
|
|
192
194
|
*
|
|
193
|
-
* If set to true - will disable validation
|
|
194
|
-
* One possible use case of doing this is - performance (as validation/
|
|
195
|
+
* If set to true - will disable validation.
|
|
196
|
+
* One possible use case of doing this is - performance (as validation/transformation takes time, especially with Joi).
|
|
195
197
|
*/
|
|
196
198
|
skipValidation?: boolean;
|
|
199
|
+
/**
|
|
200
|
+
* Default to false.
|
|
201
|
+
*
|
|
202
|
+
* False ensures that the input is not mutated by the Validation function (`validateBM`).
|
|
203
|
+
*
|
|
204
|
+
* True ensures the opposite - that the Validation function will mutate the input object
|
|
205
|
+
* if it needs to apply transformations, such as:
|
|
206
|
+
* - stripping unknown properties
|
|
207
|
+
* - converting types (e.g. string to number)
|
|
208
|
+
* - applying transformations (which as string trim, toLowerCase, etc)
|
|
209
|
+
*/
|
|
210
|
+
mutateInput?: boolean;
|
|
197
211
|
/**
|
|
198
212
|
* @default false
|
|
199
213
|
*/
|
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.11.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": "19.
|
|
10
|
+
"@naturalcycles/dev-lib": "19.22.0"
|
|
11
11
|
},
|
|
12
12
|
"files": [
|
|
13
13
|
"dist",
|
|
@@ -130,6 +130,8 @@ export interface CommonDaoCfg<
|
|
|
130
130
|
|
|
131
131
|
/**
|
|
132
132
|
* Joi, AjvSchema or ZodSchema is supported.
|
|
133
|
+
*
|
|
134
|
+
* @deprecated - use `validateBM` instead.
|
|
133
135
|
*/
|
|
134
136
|
bmSchema?: ObjectSchema<BM> | AjvSchema<BM> | ZodType<BM>
|
|
135
137
|
|
|
@@ -233,11 +235,24 @@ export interface CommonDaoOptions extends CommonDBOptions {
|
|
|
233
235
|
/**
|
|
234
236
|
* Defaults to false.
|
|
235
237
|
*
|
|
236
|
-
* If set to true - will disable validation
|
|
237
|
-
* One possible use case of doing this is - performance (as validation/
|
|
238
|
+
* If set to true - will disable validation.
|
|
239
|
+
* One possible use case of doing this is - performance (as validation/transformation takes time, especially with Joi).
|
|
238
240
|
*/
|
|
239
241
|
skipValidation?: boolean
|
|
240
242
|
|
|
243
|
+
/**
|
|
244
|
+
* Default to false.
|
|
245
|
+
*
|
|
246
|
+
* False ensures that the input is not mutated by the Validation function (`validateBM`).
|
|
247
|
+
*
|
|
248
|
+
* True ensures the opposite - that the Validation function will mutate the input object
|
|
249
|
+
* if it needs to apply transformations, such as:
|
|
250
|
+
* - stripping unknown properties
|
|
251
|
+
* - converting types (e.g. string to number)
|
|
252
|
+
* - applying transformations (which as string trim, toLowerCase, etc)
|
|
253
|
+
*/
|
|
254
|
+
mutateInput?: boolean
|
|
255
|
+
|
|
241
256
|
/**
|
|
242
257
|
* @default false
|
|
243
258
|
*/
|
|
@@ -1227,7 +1227,7 @@ export class CommonDao<BM extends BaseDBEntity, DBM extends BaseDBEntity = BM, I
|
|
|
1227
1227
|
|
|
1228
1228
|
// Return as is if no schema is passed or if `skipConversion` is set
|
|
1229
1229
|
if (
|
|
1230
|
-
!schema ||
|
|
1230
|
+
(!schema && !this.cfg.validateBM) ||
|
|
1231
1231
|
opt.skipValidation ||
|
|
1232
1232
|
(op === 'load' && !this.cfg.validateOnLoad) ||
|
|
1233
1233
|
(op === 'save' && !this.cfg.validateOnSave)
|
|
@@ -1235,16 +1235,17 @@ export class CommonDao<BM extends BaseDBEntity, DBM extends BaseDBEntity = BM, I
|
|
|
1235
1235
|
return obj
|
|
1236
1236
|
}
|
|
1237
1237
|
|
|
1238
|
-
|
|
1239
|
-
const table = opt.table || this.cfg.table
|
|
1240
|
-
const objectName = table
|
|
1241
|
-
|
|
1238
|
+
const inputName = opt.table || this.cfg.table
|
|
1242
1239
|
let error: JoiValidationError | AjvValidationError | ZodValidationError | null | undefined
|
|
1243
1240
|
let convertedValue: any
|
|
1244
1241
|
|
|
1245
1242
|
if (this.cfg.validateBM) {
|
|
1246
1243
|
const [err, value] = this.cfg.validateBM(obj as any as BM, {
|
|
1247
|
-
|
|
1244
|
+
// Passing `mutateInput` through allows to do opt-in mutation
|
|
1245
|
+
// for individual operations, e.g `someDao.save(myObj, { mutateInput: true })`,
|
|
1246
|
+
// while still keeping safe non-mutating behavior by default
|
|
1247
|
+
mutateInput: opt.mutateInput,
|
|
1248
|
+
inputName,
|
|
1248
1249
|
})
|
|
1249
1250
|
error = err
|
|
1250
1251
|
convertedValue = value
|
|
@@ -1256,13 +1257,13 @@ export class CommonDao<BM extends BaseDBEntity, DBM extends BaseDBEntity = BM, I
|
|
|
1256
1257
|
} else if (schema instanceof AjvSchema) {
|
|
1257
1258
|
// Ajv schema
|
|
1258
1259
|
const [err, value] = schema.getValidationResult(obj as T, {
|
|
1259
|
-
|
|
1260
|
+
inputName,
|
|
1260
1261
|
})
|
|
1261
1262
|
error = err
|
|
1262
1263
|
convertedValue = value
|
|
1263
1264
|
} else {
|
|
1264
1265
|
// Joi
|
|
1265
|
-
const [err, value] = getValidationResult(obj, schema,
|
|
1266
|
+
const [err, value] = getValidationResult(obj, schema, inputName)
|
|
1266
1267
|
error = err
|
|
1267
1268
|
convertedValue = value
|
|
1268
1269
|
}
|