Skip to content
This repository has been archived by the owner on Sep 12, 2023. It is now read-only.

Commit

Permalink
feat: move joi schema outside controller
Browse files Browse the repository at this point in the history
  • Loading branch information
igorissen committed Aug 4, 2023
1 parent f6fe479 commit 47b702d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
20 changes: 19 additions & 1 deletion src/lib/domain/models/audit-log.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { type AuditLogAction, type AuditLogClient, type AuditLogRole } from './models.definition';
import Joi from 'joi';
import {
type AuditLogAction,
AuditLogActionTypes,
type AuditLogClient, AuditLogClientTypes,
type AuditLogRole,
AuditLogRoleTypes
} from './models.definition';

export class AuditLog {
id?: string;
Expand All @@ -20,4 +27,15 @@ export class AuditLog {
this.role = role;
this.createdAt = createdAt;
}

static getValidatorSchema(): Joi.ObjectSchema<AuditLog> {
return Joi.object<AuditLog>({
targetUserId: Joi.string().required(),
userId: Joi.string().required(),
action: Joi.string().valid(...AuditLogActionTypes).required(),
occurredAt: Joi.string().isoDate().required(),
role: Joi.string().valid(...AuditLogRoleTypes).required(),
client: Joi.string().valid(...AuditLogClientTypes).required(),
});
}
}
13 changes: 13 additions & 0 deletions src/tests/unit/domain/models/audit-log.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import type Joi from 'joi';
import { describe, expectTypeOf, test } from 'vitest';
import { AuditLog } from '../../../../lib/domain/models/audit-log.ts';

describe('Unit | Domain | Models | AuditLog', () => {
test('returns a validation object schema of type AuditLog', function () {
// when
const validatorSchema = AuditLog.getValidatorSchema();

// then
expectTypeOf(validatorSchema).toEqualTypeOf<Joi.ObjectSchema<AuditLog>>();
});
});

0 comments on commit 47b702d

Please sign in to comment.