Unverified Commit 9fa51175 authored by Karoly Erdos's avatar Karoly Erdos Committed by GitHub
Browse files

Merge pull request #34 from EMBL-EBI-SUBS/subs-1822_add_custom_keyword_for_taxonomy_validation

[Subs 1822] add custom keyword for taxonomy validation
parents 83aa088f 6cf84833
{
"attributes": {
"metagenomic source" : [ {
"value" : "not valid taxonomy"
} ]
}
}
\ No newline at end of file
{
"attributes": {
"metagenomic source" : [ {
"value" : "wastewater metagenome"
} ]
}
}
\ No newline at end of file
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Is valid taxonomy expression.",
"$async": true,
"type": "object",
"properties": {
"attributes": {
"type": "object",
"properties": {},
"patternProperties": {
"^.*$": {
"type": "array",
"minItems": 1,
"items": {
"properties": {
"value": { "type": "string", "minLength": 1, "isValidTaxonomy": true}
},
"required": ["value"]
}
}
}
}
}
}
\ No newline at end of file
{
"name": "json-schema-validator",
"version": "1.5.6",
"version": "1.6.0",
"description": "A nodejs JSON schema validator.",
"main": "src/server.js",
"repository": "https://github.com/EMBL-EBI-SUBS/json-schema-validator.git",
......
var Ajv = require("ajv");
var request = require("request");
const logger = require("../winston");
const CustomAjvError = require("../model/custom-ajv-error");
const NoResults = "No results.";
module.exports = function isValidTaxonomy(ajv) {
function findTaxonomy(schema, data) {
return new Promise((resolve, reject) => {
if(schema) {
const taxonomySearchUrl = "https://www.ebi.ac.uk/ena/taxonomy/rest/any-name";
let errors = [];
const taxonomyExpression = data;
const encodedTaxonomyUri = encodeURIComponent(taxonomyExpression);
const url = [taxonomySearchUrl, encodedTaxonomyUri].join("/");
logger.log("debug", `Looking for taxonomy [${taxonomyExpression}] with ENA taxonomy validator.`);
request(url, (error, Response, body) => {
logger.log("debug", `Raw response: ${body}`);
if (body === NoResults) {
generateNotExistsErrorMessage();
} else {
let jsonBody = JSON.parse(body);
logger.log("debug", `Response JSON: ${JSON.stringify(jsonBody, null, 2)}`);
if (jsonBody) {
let numFound = jsonBody.length;
if (numFound === 1 && jsonBody[0]["taxId"] && jsonBody[0]["submittable"] == "true") {
logger.log("debug", "Found 1 match!");
resolve(true);
} else if (numFound === 0) {
generateNotExistsErrorMessage()
} else {
errors.push(
new CustomAjvError(
"isValidTaxonomy", `Something went wrong while validating the given taxonomy expression [${taxonomyExpression}], try again.`,
{keyword: "isValidTaxonomy"})
);
reject(new Ajv.ValidationError(errors));
}
} else {
generateNotExistsErrorMessage();
}
}
function generateNotExistsErrorMessage() {
logger.log("debug", `Could not find the given taxonomy [${taxonomyExpression}].`);
errors.push(
new CustomAjvError(
"isValidTaxonomy", `provided taxonomy expression does not exist: [${taxonomyExpression}]`,
{keyword: "isValidTaxonomy"})
);
reject(new Ajv.ValidationError(errors));
}
});
} else {
resolve(true);
}
});
}
isValidTaxonomy.definition = {
async: true,
type: "string",
validate: findTaxonomy,
errors: true
};
ajv.addKeyword("isValidTaxonomy", isValidTaxonomy.definition);
return ajv;
};
......@@ -2,12 +2,14 @@ let Ajv = require("ajv");
const logger = require("./winston");
let IsChildTermOf = require("./custom/ischildtermof");
let IsValidTerm = require("./custom/isvalidterm");
let IsValidTaxonomy = require("./custom/isvalidtaxonomy");
const ValidationError = require("./model/validation-error");
const AppError = require("./model/application-error");
let ajv = new Ajv({allErrors: true});
let isChildTermOf = new IsChildTermOf(ajv);
let isValidTerm = new IsValidTerm(ajv);
new IsValidTaxonomy(ajv);
function convertToValidationErrors(ajvErrorObjects) {
let localErrors = [];
......
const fs = require("fs");
const runValidation = require("../src/validator");
test("valid taxonomy expression should pass the validation", () => {
let inputSchema = fs.readFileSync("examples/schemas/isValidTaxonomy-schema.json");
let jsonSchema = JSON.parse(inputSchema);
let inputObj = fs.readFileSync("examples/objects/isValidTaxonomy.json");
let jsonObj = JSON.parse(inputObj);
return runValidation(jsonSchema, jsonObj).then( (data) => {
console.log(data);
expect(data).toBeDefined();
expect(data.length).toBe(0);
});
});
test("invalid taxonomy expresson should return an error", () => {
let inputSchema = fs.readFileSync("examples/schemas/isValidTaxonomy-schema.json");
let jsonSchema = JSON.parse(inputSchema);
let inputObj = fs.readFileSync("examples/objects/isInvalidTaxonomy.json");
let jsonObj = JSON.parse(inputObj);
return runValidation(jsonSchema, jsonObj).then( (data) => {
console.log(data);
expect(data).toBeDefined();
expect(data.length).toBe(1);
});
});
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment