Skip to content

Commit

Permalink
feat: CommonDaoCfg.validateOnLoad/Save
Browse files Browse the repository at this point in the history
Allows to disable validation on load/save
  • Loading branch information
kirillgroshkov committed Mar 25, 2024
1 parent e2b962b commit 7ce6508
Show file tree
Hide file tree
Showing 3 changed files with 365 additions and 313 deletions.
12 changes: 12 additions & 0 deletions src/commondao/common.dao.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,18 @@ export interface CommonDaoCfg<BM extends BaseDBEntity, DBM extends BaseDBEntity

excludeFromIndexes?: (keyof DBM)[]

/**
* 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.
* Setting it to true will set saveMethod to `insert` for save/saveBatch, which will
Expand Down
18 changes: 13 additions & 5 deletions src/commondao/common.dao.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ export class CommonDao<BM extends BaseDBEntity, DBM extends BaseDBEntity = BM> {
assignGeneratedIds: false,
useCreatedProperty: true,
useUpdatedProperty: true,
validateOnLoad: true,
validateOnSave: true,
logger: console,
...cfg,
hooks: {
Expand All @@ -104,7 +106,7 @@ export class CommonDao<BM extends BaseDBEntity, DBM extends BaseDBEntity = BM> {
const bm = this.cfg.hooks!.beforeCreate!(part)
// First assignIdCreatedUpdated, then validate!
this.assignIdCreatedUpdated(bm, opt)
return this.validateAndConvert(bm, this.cfg.bmSchema, opt)
return this.validateAndConvert(bm, this.cfg.bmSchema, undefined, opt)
}

// GET
Expand Down Expand Up @@ -721,7 +723,7 @@ export class CommonDao<BM extends BaseDBEntity, DBM extends BaseDBEntity = BM> {
// We compare with convertedBM, to account for cases when some extra property is assigned to bm,
// which should be removed post-validation, but it breaks the "equality check"
// 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)) {
// Skipping the save operation
return bm as BM
Expand Down Expand Up @@ -1077,7 +1079,7 @@ export class CommonDao<BM extends BaseDBEntity, DBM extends BaseDBEntity = BM> {
const bm = ((await this.cfg.hooks!.beforeDBMToBM?.(dbm)) || dbm) as Partial<BM>

// Validate/convert BM
return this.validateAndConvert(bm, this.cfg.bmSchema, opt)
return this.validateAndConvert(bm, this.cfg.bmSchema, 'load', opt)
}

async dbmsToBM(dbms: DBM[], opt: CommonDaoOptions = {}): Promise<BM[]> {
Expand All @@ -1094,7 +1096,7 @@ export class CommonDao<BM extends BaseDBEntity, DBM extends BaseDBEntity = BM> {
if (bm === undefined) return

// 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
return ((await this.cfg.hooks!.beforeBMToDBM?.(bm!)) || bm) as DBM
Expand Down Expand Up @@ -1138,6 +1140,7 @@ export class CommonDao<BM extends BaseDBEntity, DBM extends BaseDBEntity = BM> {
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 {
// Kirill 2021-10-18: I realized that there's little reason to keep removing null values
Expand All @@ -1152,7 +1155,12 @@ export class CommonDao<BM extends BaseDBEntity, DBM extends BaseDBEntity = BM> {
obj = _filterUndefinedValues(obj)

// Return as is if no schema is passed or if `skipConversion` is set
if (!schema || opt.skipConversion) {
if (
!schema ||
opt.skipConversion ||
(op === 'load' && !this.cfg.validateOnLoad) ||
(op === 'save' && !this.cfg.validateOnSave)
) {
return obj
}

Expand Down
Loading

0 comments on commit 7ce6508

Please sign in to comment.