@naturalcycles/db-lib
Advanced tools
Comparing version 9.8.0 to 9.9.0
@@ -152,3 +152,4 @@ /// <reference types="node" /> | ||
*/ | ||
validateAndConvert<T>(obj: Partial<T>, schema: ObjectSchema<T> | AjvSchema<T> | ZodSchema<T> | undefined, opt?: CommonDaoOptions): any; | ||
validateAndConvert<T>(obj: Partial<T>, schema: ObjectSchema<T> | AjvSchema<T> | ZodSchema<T> | undefined, op?: 'load' | 'save', // this is to skip validation if validateOnLoad/Save is false | ||
opt?: CommonDaoOptions): any; | ||
getTableSchema(): Promise<JsonSchemaRootObject<DBM>>; | ||
@@ -155,0 +156,0 @@ createTable(schema: JsonSchemaObject<DBM>, opt?: CommonDaoCreateOptions): Promise<void>; |
@@ -30,2 +30,4 @@ "use strict"; | ||
useUpdatedProperty: true, | ||
validateOnLoad: true, | ||
validateOnSave: true, | ||
logger: console, | ||
@@ -53,3 +55,3 @@ ...cfg, | ||
this.assignIdCreatedUpdated(bm, opt); | ||
return this.validateAndConvert(bm, this.cfg.bmSchema, opt); | ||
return this.validateAndConvert(bm, this.cfg.bmSchema, undefined, opt); | ||
} | ||
@@ -250,3 +252,2 @@ async getById(id, opt = {}) { | ||
opt.skipValidation = opt.skipValidation !== false; // default true | ||
opt.skipConversion = opt.skipConversion !== false; // default true | ||
opt.errorMode ||= js_lib_1.ErrorMode.SUPPRESS; | ||
@@ -290,3 +291,2 @@ const partialQuery = !!q._selectedFieldNames; | ||
opt.skipValidation = opt.skipValidation !== false; // default true | ||
opt.skipConversion = opt.skipConversion !== false; // default true | ||
opt.errorMode ||= js_lib_1.ErrorMode.SUPPRESS; | ||
@@ -333,3 +333,2 @@ const partialQuery = !!q._selectedFieldNames; | ||
opt.skipValidation = opt.skipValidation !== false; // default true | ||
opt.skipConversion = opt.skipConversion !== false; // default true | ||
opt.errorMode ||= js_lib_1.ErrorMode.SUPPRESS; | ||
@@ -365,3 +364,2 @@ const partialQuery = !!q._selectedFieldNames; | ||
opt.skipValidation = opt.skipValidation !== false; // default true | ||
opt.skipConversion = opt.skipConversion !== false; // default true | ||
opt.errorMode ||= js_lib_1.ErrorMode.SUPPRESS; | ||
@@ -546,3 +544,3 @@ const stream = this.cfg.db.streamQuery(q, opt); | ||
// Post-validation the equality check should work as intended | ||
const convertedBM = this.validateAndConvert(bm, this.cfg.bmSchema, opt); | ||
const convertedBM = this.validateAndConvert(bm, this.cfg.bmSchema, 'save', opt); | ||
if ((0, js_lib_1._deepJsonEquals)(convertedBM, opt.skipIfEquals)) { | ||
@@ -692,3 +690,2 @@ // Skipping the save operation | ||
opt.skipValidation ??= true; | ||
opt.skipConversion ??= true; | ||
opt.errorMode ||= js_lib_1.ErrorMode.SUPPRESS; | ||
@@ -828,3 +825,3 @@ if (this.cfg.immutable && !opt.allowMutability && !opt.saveMethod) { | ||
// Validate/convert BM | ||
return this.validateAndConvert(bm, this.cfg.bmSchema, opt); | ||
return this.validateAndConvert(bm, this.cfg.bmSchema, 'load', opt); | ||
} | ||
@@ -838,3 +835,3 @@ async dbmsToBM(dbms, opt = {}) { | ||
// bm gets assigned to the new reference | ||
bm = this.validateAndConvert(bm, this.cfg.bmSchema, opt); | ||
bm = this.validateAndConvert(bm, this.cfg.bmSchema, 'save', opt); | ||
// BM > DBM | ||
@@ -870,3 +867,4 @@ return ((await this.cfg.hooks.beforeBMToDBM?.(bm)) || bm); | ||
*/ | ||
validateAndConvert(obj, schema, opt = {}) { | ||
validateAndConvert(obj, schema, op, // this is to skip validation if validateOnLoad/Save is false | ||
opt = {}) { | ||
// Kirill 2021-10-18: I realized that there's little reason to keep removing null values | ||
@@ -883,3 +881,6 @@ // So, from now on we'll preserve them | ||
// Return as is if no schema is passed or if `skipConversion` is set | ||
if (!schema || opt.skipConversion) { | ||
if (!schema || | ||
opt.skipValidation || | ||
(op === 'load' && !this.cfg.validateOnLoad) || | ||
(op === 'save' && !this.cfg.validateOnSave)) { | ||
return obj; | ||
@@ -912,3 +913,3 @@ } | ||
// If we care about validation and there's an error | ||
if (error && !opt.skipValidation) { | ||
if (error) { | ||
const processedError = this.cfg.hooks.onValidationError(error); | ||
@@ -915,0 +916,0 @@ if (processedError) |
@@ -105,2 +105,12 @@ import { BaseDBEntity, CommonLogger, ErrorMode, Promisable, ZodError, ZodSchema } from '@naturalcycles/js-lib'; | ||
/** | ||
* Defaults to true. | ||
* If set to false - load (read) operations will skip validation (and conversion). | ||
*/ | ||
validateOnLoad?: boolean; | ||
/** | ||
* Defaults to true. | ||
* If set to false - save (write) operations will skip validation (and conversion). | ||
*/ | ||
validateOnSave?: boolean; | ||
/** | ||
* Defaults to false. | ||
@@ -167,19 +177,11 @@ * Setting it to true will set saveMethod to `insert` for save/saveBatch, which will | ||
/** | ||
* If true - will ignore the validation result, but will STILL DO the validation step, which will DO conversion | ||
* (according to Joi schema). | ||
* Defaults to false. | ||
* | ||
* Set skipConversion=true (or raw=true) to bypass conversion step as well (e.g for performance reasons). | ||
* | ||
* @default false | ||
* If set to true - will disable validation (and conversion). | ||
* One possible use case of doing this is - performance (as validation/conversion takes time, especially with Joi). | ||
*/ | ||
skipValidation?: boolean; | ||
/** | ||
* If true - will SKIP the joi validation AND conversion steps alltogether. To improve performance of DAO. | ||
* | ||
* @default false | ||
*/ | ||
skipConversion?: boolean; | ||
/** | ||
* @default false | ||
*/ | ||
preserveUpdatedCreated?: boolean; | ||
@@ -247,6 +249,2 @@ /** | ||
/** | ||
* @default true (for streams) | ||
*/ | ||
skipConversion?: boolean; | ||
/** | ||
* @default ErrorMode.SUPPRESS for returning ReadableStream, because .pipe() has no concept of "error propagation" | ||
@@ -253,0 +251,0 @@ * @default ErrorMode.SUPPRESS for .forEach() streams as well, but overridable |
@@ -43,3 +43,3 @@ { | ||
}, | ||
"version": "9.8.0", | ||
"version": "9.9.0", | ||
"description": "Lowest Common Denominator API to supported Databases", | ||
@@ -46,0 +46,0 @@ "keywords": [ |
@@ -133,2 +133,14 @@ import { | ||
/** | ||
* Defaults to true. | ||
* If set to false - load (read) operations will skip validation (and conversion). | ||
*/ | ||
validateOnLoad?: boolean | ||
/** | ||
* Defaults to true. | ||
* If set to false - save (write) operations will skip validation (and conversion). | ||
*/ | ||
validateOnSave?: boolean | ||
/** | ||
* Defaults to false. | ||
@@ -207,8 +219,6 @@ * Setting it to true will set saveMethod to `insert` for save/saveBatch, which will | ||
/** | ||
* If true - will ignore the validation result, but will STILL DO the validation step, which will DO conversion | ||
* (according to Joi schema). | ||
* Defaults to false. | ||
* | ||
* Set skipConversion=true (or raw=true) to bypass conversion step as well (e.g for performance reasons). | ||
* | ||
* @default false | ||
* If set to true - will disable validation (and conversion). | ||
* One possible use case of doing this is - performance (as validation/conversion takes time, especially with Joi). | ||
*/ | ||
@@ -218,11 +228,4 @@ skipValidation?: boolean | ||
/** | ||
* If true - will SKIP the joi validation AND conversion steps alltogether. To improve performance of DAO. | ||
* | ||
* @default false | ||
*/ | ||
skipConversion?: boolean | ||
/** | ||
* @default false | ||
*/ | ||
preserveUpdatedCreated?: boolean | ||
@@ -309,7 +312,2 @@ | ||
/** | ||
* @default true (for streams) | ||
*/ | ||
skipConversion?: boolean | ||
/** | ||
* @default ErrorMode.SUPPRESS for returning ReadableStream, because .pipe() has no concept of "error propagation" | ||
@@ -316,0 +314,0 @@ * @default ErrorMode.SUPPRESS for .forEach() streams as well, but overridable |
@@ -84,2 +84,4 @@ import { Transform } from 'node:stream' | ||
useUpdatedProperty: true, | ||
validateOnLoad: true, | ||
validateOnSave: true, | ||
logger: console, | ||
@@ -108,3 +110,3 @@ ...cfg, | ||
this.assignIdCreatedUpdated(bm, opt) | ||
return this.validateAndConvert(bm, this.cfg.bmSchema, opt) | ||
return this.validateAndConvert(bm, this.cfg.bmSchema, undefined, opt) | ||
} | ||
@@ -358,3 +360,2 @@ | ||
opt.skipValidation = opt.skipValidation !== false // default true | ||
opt.skipConversion = opt.skipConversion !== false // default true | ||
opt.errorMode ||= ErrorMode.SUPPRESS | ||
@@ -409,3 +410,2 @@ | ||
opt.skipValidation = opt.skipValidation !== false // default true | ||
opt.skipConversion = opt.skipConversion !== false // default true | ||
opt.errorMode ||= ErrorMode.SUPPRESS | ||
@@ -459,3 +459,2 @@ | ||
opt.skipValidation = opt.skipValidation !== false // default true | ||
opt.skipConversion = opt.skipConversion !== false // default true | ||
opt.errorMode ||= ErrorMode.SUPPRESS | ||
@@ -499,3 +498,2 @@ | ||
opt.skipValidation = opt.skipValidation !== false // default true | ||
opt.skipConversion = opt.skipConversion !== false // default true | ||
opt.errorMode ||= ErrorMode.SUPPRESS | ||
@@ -730,3 +728,3 @@ | ||
// Post-validation the equality check should work as intended | ||
const convertedBM = this.validateAndConvert(bm as Partial<BM>, this.cfg.bmSchema, opt) | ||
const convertedBM = this.validateAndConvert(bm as Partial<BM>, this.cfg.bmSchema, 'save', opt) | ||
if (_deepJsonEquals(convertedBM, opt.skipIfEquals)) { | ||
@@ -910,3 +908,2 @@ // Skipping the save operation | ||
opt.skipValidation ??= true | ||
opt.skipConversion ??= true | ||
opt.errorMode ||= ErrorMode.SUPPRESS | ||
@@ -1088,3 +1085,3 @@ | ||
// Validate/convert BM | ||
return this.validateAndConvert(bm, this.cfg.bmSchema, opt) | ||
return this.validateAndConvert(bm, this.cfg.bmSchema, 'load', opt) | ||
} | ||
@@ -1106,3 +1103,3 @@ | ||
// bm gets assigned to the new reference | ||
bm = this.validateAndConvert(bm, this.cfg.bmSchema, opt) | ||
bm = this.validateAndConvert(bm, this.cfg.bmSchema, 'save', opt) | ||
@@ -1151,2 +1148,3 @@ // BM > DBM | ||
schema: ObjectSchema<T> | AjvSchema<T> | ZodSchema<T> | undefined, | ||
op?: 'load' | 'save', // this is to skip validation if validateOnLoad/Save is false | ||
opt: CommonDaoOptions = {}, | ||
@@ -1166,3 +1164,8 @@ ): any { | ||
// Return as is if no schema is passed or if `skipConversion` is set | ||
if (!schema || opt.skipConversion) { | ||
if ( | ||
!schema || | ||
opt.skipValidation || | ||
(op === 'load' && !this.cfg.validateOnLoad) || | ||
(op === 'save' && !this.cfg.validateOnSave) | ||
) { | ||
return obj | ||
@@ -1198,3 +1201,3 @@ } | ||
// If we care about validation and there's an error | ||
if (error && !opt.skipValidation) { | ||
if (error) { | ||
const processedError = this.cfg.hooks!.onValidationError!(error) | ||
@@ -1201,0 +1204,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
10969
417592