Typed Schemas
MODIFY
src/server/rest/v1/validator/AuthValidation.ts
src/server/rest/v1/validator/AuthValidation.ts
import { HttpCheckEulaRequest, HttpEulaRequest, HttpLoginRequest, HttpRegisterUserRequest, HttpResendVerificationMailRequest, HttpResetPasswordRequest, HttpVerifyEmailRequest } from '../../../../types/requests/HttpUserRequest';
+import Schema from './Schema';
import SchemaValidator from './SchemaValidator';
import fs from 'fs';
import global from '../../../../types/GlobalType';
export default class AuthValidator extends SchemaValidator {
private static _instance: AuthValidator | undefined;
- private _authSignIn: any;
- private _authSignOn: any;
- private _authResetPassword: any;
- private _authCheckEula: any;
- private _authVerifyEmail: any;
- private _authResendVerificationEmail: any;
- private _authEula: any;
+ private authSignIn: Schema;
+ private authSignOn: Schema;
+ private authResetPassword: Schema;
+ private authCheckEula: Schema;
+ private authVerifyEmail: Schema;
+ private authResendVerificationEmail: Schema;
+ private authEula: Schema;
private constructor() {
super('AuthValidator');
- this._authSignIn = JSON.parse(fs.readFileSync(`${global.appRoot}/assets/server/rest/v1/schemas/auth/auth-signin.json`, 'utf8'));
- this._authSignOn = JSON.parse(fs.readFileSync(`${global.appRoot}/assets/server/rest/v1/schemas/auth/auth-signon.json`, 'utf8'));
- this._authResetPassword = JSON.parse(fs.readFileSync(`${global.appRoot}/assets/server/rest/v1/schemas/auth/auth-reset-password.json`, 'utf8'));
- this._authCheckEula = JSON.parse(fs.readFileSync(`${global.appRoot}/assets/server/rest/v1/schemas/auth/auth-check-eula.json`, 'utf8'));
- this._authVerifyEmail = JSON.parse(fs.readFileSync(`${global.appRoot}/assets/server/rest/v1/schemas/auth/auth-verify-email.json`, 'utf8'));
- this._authResendVerificationEmail = JSON.parse(fs.readFileSync(`${global.appRoot}/assets/server/rest/v1/schemas/auth/auth-resend-verification-email.json`, 'utf8'));
- this._authEula = JSON.parse(fs.readFileSync(`${global.appRoot}/assets/server/rest/v1/schemas/auth/auth-eula.json`, 'utf8'));
+ this.authSignIn = JSON.parse(fs.readFileSync(`${global.appRoot}/assets/server/rest/v1/schemas/auth/auth-signin.json`, 'utf8'));
+ this.authSignOn = JSON.parse(fs.readFileSync(`${global.appRoot}/assets/server/rest/v1/schemas/auth/auth-signon.json`, 'utf8'));
+ this.authResetPassword = JSON.parse(fs.readFileSync(`${global.appRoot}/assets/server/rest/v1/schemas/auth/auth-reset-password.json`, 'utf8'));
+ this.authCheckEula = JSON.parse(fs.readFileSync(`${global.appRoot}/assets/server/rest/v1/schemas/auth/auth-check-eula.json`, 'utf8'));
+ this.authVerifyEmail = JSON.parse(fs.readFileSync(`${global.appRoot}/assets/server/rest/v1/schemas/auth/auth-verify-email.json`, 'utf8'));
+ this.authResendVerificationEmail = JSON.parse(fs.readFileSync(`${global.appRoot}/assets/server/rest/v1/schemas/auth/auth-resend-verification-email.json`, 'utf8'));
+ this.authEula = JSON.parse(fs.readFileSync(`${global.appRoot}/assets/server/rest/v1/schemas/auth/auth-eula.json`, 'utf8'));
}
public static getInstance(): AuthValidator {
@@ -34,43 +35,43 @@ export default class AuthValidator extends SchemaValidator {
public validateAuthSignIn(data: HttpLoginRequest): HttpLoginRequest {
// Validate schema
- this.validate(this._authSignIn, data);
+ this.validate(this.authSignIn, data);
return data;
}
public validateAuthSignOn(data: HttpRegisterUserRequest): Partial<HttpRegisterUserRequest> {
// Validate schema
- this.validate(this._authSignOn, data);
+ this.validate(this.authSignOn, data);
return data;
}
public validateAuthResetPassword(data: HttpResetPasswordRequest): Partial<HttpResetPasswordRequest> {
// Validate schema
- this.validate(this._authResetPassword, data);
+ this.validate(this.authResetPassword, data);
return data;
}
public validateAuthCheckEula(data: any): Partial<HttpCheckEulaRequest> {
// Validate schema
- this.validate(this._authCheckEula, data);
+ this.validate(this.authCheckEula, data);
return data;
}
public validateAuthVerifyEmail(data: any): Partial<HttpVerifyEmailRequest> {
// Validate schema
- this.validate(this._authVerifyEmail, data);
+ this.validate(this.authVerifyEmail, data);
return data;
}
public validateAuthResendVerificationEmail(data: any): Partial<HttpResendVerificationMailRequest> {
// Validate schema
- this.validate(this._authResendVerificationEmail, data);
+ this.validate(this.authResendVerificationEmail, data);
return data;
}
public validateAuthEula(data: any): Partial<HttpEulaRequest> {
// Validate schema
- this.validate(this._authEula, data);
+ this.validate(this.authEula, data);
return data;
}
}

ADD
src/server/rest/v1/validator/Schema.ts
+export default interface Schema {
+ [properties: string]: unknown;
+ id: string,
+ type: string,
+}

MODIFY
src/server/rest/v1/validator/SchemaValidator.ts
src/server/rest/v1/validator/SchemaValidator.ts
@@ -13,8 +13,8 @@ const extraSanitizers = {
export default class SchemaValidator {
private readonly ajv: Ajv.Ajv;
- private _commonSchema: any = JSON.parse(fs.readFileSync(`${global.appRoot}/assets/server/rest/v1/schemas/common/common.json`, 'utf8'));
- private _tenantComponentSchema: any = JSON.parse(fs.readFileSync(`${global.appRoot}/assets/server/rest/v1/schemas/tenant/tenant-components.json`, 'utf8'));
+ private commonSchema: any = JSON.parse(fs.readFileSync(`${global.appRoot}/assets/server/rest/v1/schemas/common/common.json`, 'utf8'));
+ private tenantComponentSchema: any = JSON.parse(fs.readFileSync(`${global.appRoot}/assets/server/rest/v1/schemas/tenant/tenant-components.json`, 'utf8'));
constructor(readonly moduleName: string,
config: {allErrors: boolean; removeAdditional: boolean|'all'|'failing'|undefined;
@@ -25,8 +25,8 @@ export default class SchemaValidator {
coerceTypes: true
}) {
this.ajv = ajvSanitizer(new Ajv(config), extraSanitizers);
- this.ajv.addSchema(this._commonSchema);
- this.ajv.addSchema(this._tenantComponentSchema);
+ this.ajv.addSchema(this.commonSchema);
+ this.ajv.addSchema(this.tenantComponentSchema);
}
public validate(schema: boolean|Record<string, unknown>, content: any): void {

MODIFY
src/server/rest/v1/validator/TenantValidation.ts
src/server/rest/v1/validator/TenantValidation.ts
@@ -3,29 +3,29 @@ import { HttpTenantLogoRequest, HttpTenantsRequest } from '../../../../types/req
import AppError from '../../../../exception/AppError';
import Constants from '../../../../utils/Constants';
import { HTTPError } from '../../../../types/HTTPError';
+import Schema from './Schema';
import SchemaValidator from './SchemaValidator';
import Tenant from '../../../../types/Tenant';
-import UtilsService from '../service/UtilsService';
import fs from 'fs';
import global from '../../../../types/GlobalType';
export default class TenantValidator extends SchemaValidator {
private static _instance: TenantValidator | undefined;
- private _tenantCreateReqSuperAdmin: any;
- private _tenantUpdateReqSuperAdmin: any;
- private _tenantDeleteReqSuperAdmin: any;
- private _tenantGetLogoReqSuperAdmin: any;
- private _tenantGetReqSuperAdmin: any;
- private _tenantsGetReqSuperAdmin: any;
+ private tenantCreateReqSuperAdmin: Schema;
+ private tenantUpdateReqSuperAdmin: Schema;
+ private tenantDeleteReqSuperAdmin: Schema;
+ private tenantGetLogoReqSuperAdmin: Schema;
+ private tenantGetReqSuperAdmin: Schema;
+ private tenantsGetReqSuperAdmin: Schema;
private constructor() {
super('TenantValidator');
- this._tenantCreateReqSuperAdmin = JSON.parse(fs.readFileSync(`${global.appRoot}/assets/server/rest/v1/schemas/tenant/tenant-create-req-super-admin.json`, 'utf8'));
- this._tenantUpdateReqSuperAdmin = JSON.parse(fs.readFileSync(`${global.appRoot}/assets/server/rest/v1/schemas/tenant/tenant-update-req-super-admin.json`, 'utf8'));
- this._tenantDeleteReqSuperAdmin = JSON.parse(fs.readFileSync(`${global.appRoot}/assets/server/rest/v1/schemas/tenant/tenant-delete-req-super-admin.json`, 'utf8'));
- this._tenantGetReqSuperAdmin = JSON.parse(fs.readFileSync(`${global.appRoot}/assets/server/rest/v1/schemas/tenant/tenant-get-req-super-admin.json`, 'utf8'));
- this._tenantsGetReqSuperAdmin = JSON.parse(fs.readFileSync(`${global.appRoot}/assets/server/rest/v1/schemas/tenant/tenants-get-req-super-admin.json`, 'utf8'));
- this._tenantGetLogoReqSuperAdmin = JSON.parse(fs.readFileSync(`${global.appRoot}/assets/server/rest/v1/schemas/tenant/tenant-get-logo-req-super-admin.json`, 'utf8'));
+ this.tenantCreateReqSuperAdmin = JSON.parse(fs.readFileSync(`${global.appRoot}/assets/server/rest/v1/schemas/tenant/tenant-create-req-super-admin.json`, 'utf8'));
+ this.tenantUpdateReqSuperAdmin = JSON.parse(fs.readFileSync(`${global.appRoot}/assets/server/rest/v1/schemas/tenant/tenant-update-req-super-admin.json`, 'utf8'));
+ this.tenantDeleteReqSuperAdmin = JSON.parse(fs.readFileSync(`${global.appRoot}/assets/server/rest/v1/schemas/tenant/tenant-delete-req-super-admin.json`, 'utf8'));
+ this.tenantGetReqSuperAdmin = JSON.parse(fs.readFileSync(`${global.appRoot}/assets/server/rest/v1/schemas/tenant/tenant-get-req-super-admin.json`, 'utf8'));
+ this.tenantsGetReqSuperAdmin = JSON.parse(fs.readFileSync(`${global.appRoot}/assets/server/rest/v1/schemas/tenant/tenants-get-req-super-admin.json`, 'utf8'));
+ this.tenantGetLogoReqSuperAdmin = JSON.parse(fs.readFileSync(`${global.appRoot}/assets/server/rest/v1/schemas/tenant/tenant-get-logo-req-super-admin.json`, 'utf8'));
}
public static getInstance(): TenantValidator {
@@ -37,7 +37,7 @@ export default class TenantValidator extends SchemaValidator {
public validateTenantCreateRequestSuperAdmin(tenant: Tenant): Tenant {
// Validate schema
- this.validate(this._tenantCreateReqSuperAdmin, tenant);
+ this.validate(this.tenantCreateReqSuperAdmin, tenant);
// Validate deps between components
this.validateComponentDependencies(tenant);
return tenant;
@@ -45,7 +45,7 @@ export default class TenantValidator extends SchemaValidator {
public validateTenantUpdateRequestSuperAdmin(tenant: Tenant): Tenant {
// Validate schema
- this.validate(this._tenantUpdateReqSuperAdmin, tenant);
+ this.validate(this.tenantUpdateReqSuperAdmin, tenant);
// Validate deps between components
this.validateComponentDependencies(tenant);
return tenant;
@@ -53,25 +53,25 @@ export default class TenantValidator extends SchemaValidator {
public validateTenantDeleteRequestSuperAdmin(data: any): string {
// Validate schema
- this.validate(this._tenantDeleteReqSuperAdmin, data);
+ this.validate(this.tenantDeleteReqSuperAdmin, data);
return data.ID;
}
public validateGetLogoReqSuperAdmin(data: any): HttpTenantLogoRequest {
// Validate schema
- this.validate(this._tenantGetLogoReqSuperAdmin, data);
+ this.validate(this.tenantGetLogoReqSuperAdmin, data);
return data;
}
public validateTenantGetReqSuperAdmin(data: any): string {
// Validate schema
- this.validate(this._tenantGetReqSuperAdmin, data);
+ this.validate(this.tenantGetReqSuperAdmin, data);
return data.ID;
}
public validateTenantsGetReqSuperAdmin(data: any): HttpTenantsRequest {
// Validate schema
- this.validate(this._tenantsGetReqSuperAdmin, data);
+ this.validate(this.tenantsGetReqSuperAdmin, data);
return data;
}

Typed Schemas