Skip to content

Commit

Permalink
feat: use mongoose validation when validator disable
Browse files Browse the repository at this point in the history
  • Loading branch information
Chinlinlee committed Oct 7, 2023
1 parent 3158637 commit 4e12601
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion api/FHIRApiService/services/base.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ class BaseFhirApiService {
}

static async validateRequestResource(resource) {
let resourceType = _.get(resource, "resourceType");
// Validate user request body
if (process.env.ENABLE_VALIDATOR === "true") {
let { validateResource } = require("@root/utils/validator/processor");
Expand All @@ -112,13 +113,35 @@ class BaseFhirApiService {
let containedValidation = await validateContainedList(resource);
if (!containedValidation.status) {
let operationOutcomeError = handleError.processing(`The resource in contained error. ${containedValidation.message}`);
logger.error(`[Error: ${JSON.stringify(operationOutcomeError)}] [Resource Type: ${this.resourceType}]`);
logger.error(`[Error: ${JSON.stringify(operationOutcomeError)}] [Resource Type: ${resourceType}]`);
return {
status: false,
code: 422,
result: operationOutcomeError
};
}

try {
let mongooseDoc = new mongoose.model(resourceType)(resource);
let mongooseValidation = await mongooseDoc.validate(resource);
} catch (e) {
let name = _.get(e, "name");
if (name === "ValidationError") {
let operationOutcomeError = handleError.processing(e.message);
return {
status: false,
code: 422,
result: operationOutcomeError
};
}

let operationOutcomeError = handleError.exception(e.message);
return {
status: false,
code: 500,
result: operationOutcomeError
};
}
}

return {
Expand Down

0 comments on commit 4e12601

Please sign in to comment.