Skip to content

Commit

Permalink
Better error handling on isChildTermOf
Browse files Browse the repository at this point in the history
  • Loading branch information
Flávia Penim committed Jan 12, 2018
1 parent 8159843 commit a9636c1
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 33 deletions.
75 changes: 43 additions & 32 deletions src/ischildtermof.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,41 +18,52 @@ module.exports = function defFunc(ajv) {
const olsSearchUrl = "https://www.ebi.ac.uk/ols/api/search?q=";

let errors = [];
let errorCount = 0;
for (var i = 0; i < data.length; i++) {
const termUri = encodeURIComponent(data[i]);
const url = olsSearchUrl + termUri
+ "&exact=true&groupField=true&allChildrenOf=" + encodeURIComponent(parentTerm)
+ "&ontology=" + ontologyId + "&queryFields=iri";

console.log('Evaluating isChildTermOf', url);
request(url, function(error, response, body) {
let jsonBody = JSON.parse(body);
if(jsonBody.response.numFound === 1) {
resolve(true);
} else if(jsonBody.response.numFound === 0) {
errors.push({
keyword: 'isChildTermOf',
message: 'is not child term of ' + parentTerm,
params: {keyword: 'isChildTermOf'}
});
errorCount++;
if (errorCount === data.length) {
reject(new Ajv.ValidationError(errors));
}
} else {
errors.push({
keyword: 'isChildTermOf',
message: 'Something went wrong while validating term, try again.',
params: {keyword: 'isChildTermOf'}
});
errorCount++;
if (errorCount === data.length) {
reject(new Ajv.ValidationError(errors));

if(parentTerm && ontologyId) {
let errorCount = 0;
for (var i = 0; i < data.length; i++) {
const termUri = encodeURIComponent(data[i]);
const url = olsSearchUrl + termUri
+ "&exact=true&groupField=true&allChildrenOf=" + encodeURIComponent(parentTerm)
+ "&ontology=" + ontologyId + "&queryFields=iri";

console.log('Evaluating isChildTermOf', url);
request(url, function(error, response, body) {
let jsonBody = JSON.parse(body);
if(jsonBody.response.numFound === 1) {
resolve(true);
} else if(jsonBody.response.numFound === 0) {
errors.push({
keyword: 'isChildTermOf',
message: 'is not child term of ' + parentTerm,
params: {keyword: 'isChildTermOf'}
});
errorCount++;
if (errorCount === data.length) {
reject(new Ajv.ValidationError(errors));
}
} else {
errors.push({
keyword: 'isChildTermOf',
message: 'Something went wrong while validating term, try again.',
params: {keyword: 'isChildTermOf'}
});
errorCount++;
if (errorCount === data.length) {
reject(new Ajv.ValidationError(errors));
}
}
}
});
}
} else {
errors.push({
keyword: 'isChildTermOf',
message: 'Missing required variable in schema isChildTermOf, required properties are: parentTerm and ontologyId.',
params: {keyword: 'isChildTermOf'}
});
reject(new Ajv.ValidationError(errors));
}

});
}

Expand Down
2 changes: 1 addition & 1 deletion src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ app.post('/validate', (req, res) => {

app.get('/validate', (req, res) => {
res.send({
message: "This is the USI JSON Schema Validator. Please POST to this endpoint the schema and object to validate structured as in bodyStructure. For more information and examples on how to use the validator see https://github.com/EMBL-EBI-SUBS/json-schema-validator.",
message: "This is the USI JSON Schema Validator. Please POST to this endpoint the schema and object to validate structured as in bodyStructure. For more information and examples on how to use the validator see https://github.com/EMBL-EBI-SUBS/json-schema-validator",
bodyStructure: {
schema: {},
object: {}
Expand Down

0 comments on commit a9636c1

Please sign in to comment.