Skip to content

Commit

Permalink
feat: change validator storepath;add empty profile
Browse files Browse the repository at this point in the history
- support validate with empty profile (base validation)
- add new code `invalid` in operation outcome
- change validator storepath to relative path
  • Loading branch information
Chinlinlee committed Mar 11, 2022
1 parent fd5e51e commit 36ea1e1
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 15 deletions.
45 changes: 36 additions & 9 deletions api/FHIRApiService/$validate.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const _ = require('lodash');
const FHIR = require('fhir').Fhir;
const { handleError } = require('../../models/FHIR/httpMessage');
const mongodb = require('../../models/mongodb');
const { handleError, OperationOutcome, issue} = require('../../models/FHIR/httpMessage');
const { validateByProfile, validateByMetaProfile } = require('../../models/FHIR/fhir-validator');
const { logger } = require('../../utils/log');
const path = require('path');
Expand All @@ -23,13 +24,7 @@ module.exports = async function (req, res, resourceType) {
return res.status(code).send(item);
};
try {
let operationOutcomeMessage;
let profileUrl = _.get(req.query, "profile");
if (profileUrl) {
operationOutcomeMessage = await validateByProfile(profileUrl, req.body);
} else {
operationOutcomeMessage = await validateByMetaProfile(req.body);
}
let operationOutcomeMessage = await getValidateResult(req, resourceType);
let haveError = (_.get(operationOutcomeMessage, "issue")) ? operationOutcomeMessage.issue.find(v=> v.severity === "error") : false;
if (haveError) {
return doRes(412, operationOutcomeMessage);
Expand All @@ -41,4 +36,36 @@ module.exports = async function (req, res, resourceType) {
let operationOutcomeError = handleError.exception(errorStr);
return doRes(500, operationOutcomeError);
}
};
};

/**
*
* @param {import('express').Request} req
* @param {string} resourceType
*/
async function getValidateResult(req, resourceType) {
try {
let profileUrl = _.get(req.query, "profile");
let metaProfiles = _.get(req.body, "meta.profile", false);
if (profileUrl) {
return await validateByProfile(profileUrl, req.body);
} else if (metaProfiles) {
return await validateByMetaProfile(req.body);
}
let validation = await mongodb[resourceType].validate(req.body);
} catch(e) {
let name = _.get(e, "name");
if (name === "ValidationError") {
let operationOutcomeError = new OperationOutcome([]);
for (let errorKey in e.errors) {
let error = e.errors[errorKey];
let message = _.get(error, "message", `${error} invalid`);
let errorIssue = new issue("error", "invalid", message);
_.set(errorIssue, "Location", [errorKey]);
operationOutcomeError.issue.push(errorIssue);
}
return operationOutcomeError;
}
throw e;
}
}
7 changes: 4 additions & 3 deletions models/FHIR/fhir-validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,13 @@ async function validateByMetaProfile(resourceContent) {
await fetchCodeSystem(resJson);
}
let contentHash = hash(resJson);
let storePath = path.join(process.env.VALIDATION_FILES_ROOT_PATH, hash({url:url}) + ".json");
let urlHash = hash({url:url});
let storePath = path.join(process.env.VALIDATION_FILES_ROOT_PATH, urlHash + ".json");
fs.writeFile(path.resolve(storePath), JSON.stringify(resJson), ()=> {});
let validationFileObj = {
url: url,
hash: contentHash,
path: storePath,
path: `${urlHash}.json`,
id: resJson.id
};
await mongodb.FHIRValidationFiles.findOneAndUpdate({
Expand Down Expand Up @@ -148,7 +149,7 @@ async function refreshResourceResolver() {
* Call C# API server to validate with profiles.
* @param {Array<string>} profile The string array of profiles URL.
* @param {JSON} resourceContent The FHIR resource JSON object.
* @return {JSON}
* @return {Promise<JSON>}
*/
async function validate(profile, resourceContent) {
try {
Expand Down
3 changes: 2 additions & 1 deletion models/FHIR/httpMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ const handleError = {
"not-supported" : (err) => getOperationOutCome(err,"not-supported"),
"security": (err) => getOperationOutCome(err, "security"),
"expired": (err) => getOperationOutCome(err, "expired"),
"forbidden": (err) => getOperationOutCome(err, "forbidden")
"forbidden": (err) => getOperationOutCome(err, "forbidden"),
"invalid": (err) => getOperationOutCome(err, "invalid")
};


Expand Down
5 changes: 3 additions & 2 deletions models/FHIR/schedule-update-validation-files.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@ const schedule = require('node-schedule');
} else if (resJson.resourceType === "ValueSet") {
await fetchCodeSystem(resJson);
}
let storePath = path.join(process.env.VALIDATION_FILES_ROOT_PATH, hash({url:url}) + ".json");
let urlHash = hash({url:url});
let storePath = path.join(process.env.VALIDATION_FILES_ROOT_PATH, urlHash + ".json");
fs.writeFile(path.resolve(storePath), JSON.stringify(resJson), ()=> {});
let validationFileObj = {
url: url,
hash: contentHash,
path: storePath,
path: `${urlHash}.json`,
id: resJson.id
};
await mongodb.FHIRValidationFiles.findOneAndUpdate({
Expand Down

0 comments on commit 36ea1e1

Please sign in to comment.