Skip to content

Commit

Permalink
Cleaned up and streamlined the app and updated the tests. All tests p…
Browse files Browse the repository at this point in the history
…assing
  • Loading branch information
daniwelter committed Jul 26, 2018
1 parent 95ff423 commit e44ef0e
Show file tree
Hide file tree
Showing 10 changed files with 92 additions and 72 deletions.
16 changes: 8 additions & 8 deletions src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ const express = require("express");
const bodyParser = require("body-parser");
const logger = require("./winston");
const runValidation = require("./validation/validator");
const runValidationWithRefs = require("./validation/validator-prototype");
// const runValidationWithRefs = require("./validation/validator-prototype");
const AppError = require("./model/application-error");
const { check, validationResult } = require("express-validator/check");
const { handleValidation } = require("./validation/validation-handler");
// const { handleValidation } = require("./validation/validation-handler");

const argv = require("yargs").argv;
const npid = require("npid");
Expand Down Expand Up @@ -45,7 +45,7 @@ app.post("/validate", [
return res.status(422).json({ errors: errors.mapped() });
} else {
logger.log("debug", "Received POST request.");
runValidation(req.body.schema, req.body.object).then((output) => {
runValidation.validateSingleSchema(req.body.schema, req.body.object).then((output) => {
logger.log("silly", "Sent validation results.");
res.status(200).send(output);
}).catch((err) => {
Expand All @@ -63,11 +63,11 @@ app.get("/validate", (req, res) => {
schema: {},
object: {}
},
repository: "https://github.com/EMBL-EBI-SUBS/json-schema-validator"
repository: "https://github.com/HumanCellAtlas/ingest-validator-js"
});
});

app.post("/prototype", [
app.post("/validateRefs", [
check("schemas", "Required and must be a non empty array.").isArray().not().isEmpty(),
check("rootSchemaId", "Required.").optional(),
check("entity", "Required.").exists()
Expand All @@ -77,7 +77,7 @@ app.post("/prototype", [
return res.status(422).json({ errors: errors.mapped() });
} else {
logger.log("debug", "Received POST request.");
runValidationWithRefs(req.body.schemas, req.body.entity, req.body.rootSchemaId).then((output) => {
runValidation.validateMultiSchema(req.body.schemas, req.body.entity, req.body.rootSchemaId).then((output) => {
logger.log("silly", "Sent validation results.");
res.status(200).send(output);
}).catch((err) => {
Expand All @@ -96,7 +96,7 @@ app.post("/prototype", [
}
);

app.get("/prototype", (req, res) => {
app.get("/validateRefs", (req, res) => {
logger.log("silly", "Received GET request.");
res.send({
message: "This is the Submissions JSON Schema Validator. Please POST to this endpoint the schema and object to validate structured as showed in bodyStructure.",
Expand All @@ -105,7 +105,7 @@ app.get("/prototype", (req, res) => {
rootSchemaId: "",
entity: {}
},
repository: "https://github.com/EMBL-EBI-SUBS/json-schema-validator"
repository: "https://github.com/HumanCellAtlas/ingest-validator-js"
});
});

Expand Down
15 changes: 0 additions & 15 deletions src/validation/validator-prototype.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,6 @@ function convertToValidationErrors(ajvErrorObjects) {
return localErrors;
}

// function validate(schemas, entity, rootSchemaId) {
// let ajv = new Ajv({schemas: schemas, allErrors: true});
// let validate;
// try {
// validate = ajv.getSchema(rootSchemaId);
// validate(entity);
// } catch(err) {
// logger.log("error", err);
// throw new Error("Could not find schema for id: " + rootSchemaId);
// }
//
// logger.log("debug", ajv.errorsText(validate.errors, {dataVar: entity.alias}));
// return validate.errors;
// }

function validate(schemas, entity, rootSchemaId) {
logger.log("silly", "Running validation...");

Expand Down
81 changes: 58 additions & 23 deletions src/validation/validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,29 +27,64 @@ function convertToValidationErrors(ajvErrorObjects) {
return localErrors;
}

function runValidation(inputSchema, inputObject) {
logger.log("silly", "Running validation...");
return new Promise((resolve, reject) => {
var validate = ajv.compile(inputSchema);
Promise.resolve(validate(inputObject))
.then((data) => {
if (validate.errors) {
logger.log("debug", ajv.errorsText(validate.errors, {dataVar: inputObject.alias}));
resolve(convertToValidationErrors(validate.errors));
} else {
resolve([]);
}
}
).catch((err, errors) => {
if (!(err instanceof Ajv.ValidationError)) {
logger.log("error", "An error ocurred while running the validation.");
reject(new AppError("An error ocurred while running the validation."));
} else {
logger.log("debug", ajv.errorsText(err.errors, {dataVar: inputObject.alias}));
resolve(convertToValidationErrors(err.errors));
}
module.exports = {
validateSingleSchema: function(inputSchema, inputObject)
{
logger.log("silly", "Running validation...");
return new Promise((resolve, reject) => {
var validate = ajv.compile(inputSchema);
Promise.resolve(validate(inputObject))
.then((data) => {
if (validate.errors) {
logger.log("debug", ajv.errorsText(validate.errors, {dataVar: inputObject.alias}));
resolve(convertToValidationErrors(validate.errors));
} else {
resolve([]);
}
}
).catch((err, errors) => {
if (!(err instanceof Ajv.ValidationError)) {
logger.log("error", "An error ocurred while running the validation.");
reject(new AppError("An error ocurred while running the validation."));
} else {
logger.log("debug", ajv.errorsText(err.errors, {dataVar: inputObject.alias}));
resolve(convertToValidationErrors(err.errors));
}
});
});
});
},

validateMultiSchema: function(schemas, entity, rootSchemaId) {
logger.log("silly", "Running validation...");


return new Promise((resolve, reject) => {
for (var s of schemas){
if (!ajv.getSchema(s.$id)){
// if (!ajv.getSchema(s.id)){
ajv.addSchema(s);
}
}
var validate = ajv.getSchema(rootSchemaId);
Promise.resolve(validate(entity))
.then((data) => {
if (validate.errors) {
logger.log("debug", ajv.errorsText(validate.errors, {dataVar: entity.alias}));
resolve(convertToValidationErrors(validate.errors));
} else {
resolve([]);
}
}
).catch((err, errors) => {
if (!(err instanceof Ajv.ValidationError)) {
logger.log("error", "An error ocurred while running the validation.");
reject(new AppError("An error ocurred while running the validation."));
} else {
logger.log("debug", ajv.errorsText(err.errors, {dataVar: entity.alias}));
resolve(convertToValidationErrors(err.errors));
}
});
});
}
}

module.exports = runValidation;
14 changes: 7 additions & 7 deletions test/async-validation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ test(" -> isChildTermOf Schema", () => {
let inputObj = fs.readFileSync("examples/objects/isChildTerm.json");
let jsonObj = JSON.parse(inputObj);

return runValidation(jsonSchema, jsonObj).then( (data) => {
return runValidation.validateSingleSchema(jsonSchema, jsonObj).then( (data) => {
expect(data).toBeDefined();
expect(data[0]).toBeDefined();
expect(data[0].dataPath).toBe(".attributes['age'][0].terms[0].url");
Expand All @@ -22,7 +22,7 @@ test("FAANG Schema - FAANG \'organism\' sample", () => {
let inputObj = fs.readFileSync("examples/objects/faang-organism-sample.json");
let jsonObj = JSON.parse(inputObj);

return runValidation(jsonSchema, jsonObj).then( (data) => {
return runValidation.validateSingleSchema(jsonSchema, jsonObj).then( (data) => {
expect(data).toBeDefined();
expect(data.length).toBe(0);
});
Expand All @@ -35,7 +35,7 @@ test("FAANG Schema - \'specimen\' sample", () => {
let inputObj = fs.readFileSync("examples/objects/faang-specimen-sample.json");
let jsonObj = JSON.parse(inputObj);

return runValidation(jsonSchema, jsonObj).then( (data) => {
return runValidation.validateSingleSchema(jsonSchema, jsonObj).then( (data) => {
expect(data).toBeDefined();
expect(data.length).toBe(0);
});
Expand All @@ -48,7 +48,7 @@ test("FAANG Schema - \'pool of specimens\' sample", () => {
let inputObj = fs.readFileSync("examples/objects/faang-poolOfSpecimens-sample.json");
let jsonObj = JSON.parse(inputObj);

return runValidation(jsonSchema, jsonObj).then( (data) => {
return runValidation.validateSingleSchema(jsonSchema, jsonObj).then( (data) => {
expect(data).toBeDefined();
expect(data.length).toBe(0);
});
Expand All @@ -61,7 +61,7 @@ test("FAANG Schema - \'cell specimen\' sample", () => {
let inputObj = fs.readFileSync("examples/objects/faang-cellSpecimen-sample.json");
let jsonObj = JSON.parse(inputObj);

return runValidation(jsonSchema, jsonObj).then( (data) => {
return runValidation.validateSingleSchema(jsonSchema, jsonObj).then( (data) => {
expect(data).toBeDefined();
expect(data.length).toBe(0);
});
Expand All @@ -74,7 +74,7 @@ test("FAANG Schema - \'cell culture\' sample", () => {
let inputObj = fs.readFileSync("examples/objects/faang-cellCulture-sample.json");
let jsonObj = JSON.parse(inputObj);

return runValidation(jsonSchema, jsonObj).then( (data) => {
return runValidation.validateSingleSchema(jsonSchema, jsonObj).then( (data) => {
expect(data).toBeDefined();
expect(data.length).toBe(0);
});
Expand All @@ -87,7 +87,7 @@ test("FAANG Schema - \'cell line\' sample", () => {
let inputObj = fs.readFileSync("examples/objects/faang-cellLine-sample.json");
let jsonObj = JSON.parse(inputObj);

return runValidation(jsonSchema, jsonObj).then( (data) => {
return runValidation.validateSingleSchema(jsonSchema, jsonObj).then( (data) => {
expect(data).toBeDefined();
expect(data.length).toBe(0);
});
Expand Down
4 changes: 2 additions & 2 deletions test/draft-4.test_ignore.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const fs = require("fs");
const protoValidate = require("../src/validation/validator-prototype");
const validator = require("../src/validation/validator");

test("HCA ref schema, species and restriction schema test WITHOUT errors", () => {
let hcaSchema = fs.readFileSync("examples/schemas/draft-04/donor_organism.json");
Expand All @@ -17,7 +17,7 @@ test("HCA ref schema, species and restriction schema test WITHOUT errors", () =>
let ethnicitySchema = fs.readFileSync("examples/schemas/draft-04/ethnicity_ontology.json")
let jsonEthnicitySchema = JSON.parse(ethnicitySchema)

return protoValidate(
return validator.validateMultiSchema(
[jsonHcaSchema, jsonSpeciesSchema, jsonCoreSchema, jsonHumanSchema, jsonEthnicitySchema],
{

Expand Down
4 changes: 2 additions & 2 deletions test/graphRestriction.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ test(" -> graphRestriction Schema", () => {
let inputObj = fs.readFileSync("examples/objects/graphRestriction_pass.json");
let jsonObj = JSON.parse(inputObj);

return runValidation(jsonSchema, jsonObj).then( (data) => {
return runValidation.validateSingleSchema(jsonSchema, jsonObj).then( (data) => {
expect(data).toBeDefined();
});
});
Expand All @@ -20,7 +20,7 @@ test(" -> graphRestriction Schema", () => {
let inputObj = fs.readFileSync("examples/objects/graphRestriction_fail.json");
let jsonObj = JSON.parse(inputObj);

return runValidation(jsonSchema, jsonObj).then( (data) => {
return runValidation.validateSingleSchema(jsonSchema, jsonObj).then( (data) => {
expect(data).toBeDefined();
expect(data[0]).toBeDefined();
});
Expand Down
2 changes: 1 addition & 1 deletion test/isValidTerm.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ test("isValidTerm", () => {
let inputObj = fs.readFileSync("examples/objects/isValidTerm.json");
let jsonObj = JSON.parse(inputObj);

return runValidation(jsonSchema, jsonObj).then( (data) => {
return runValidation.validateSingleSchema(jsonSchema, jsonObj).then( (data) => {
expect(data).toBeDefined();
expect(data[0]).toBeDefined();
});
Expand Down
14 changes: 7 additions & 7 deletions test/schema-references.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const fs = require("fs");
const protoValidate = require("../src/validation/validator-prototype");
const validation = require("../src/validation/validator");

// test("Base schema and definitions schema test WITH erros", () => {
// let baseSchema = fs.readFileSync("examples/schemas/references/base-sample-schema.json");
Expand Down Expand Up @@ -33,7 +33,7 @@ test("Base schema and definitions schema test WITH erros", () => {
let definitionsSchema = fs.readFileSync("examples/schemas/references/definitions-schema.json");
let jsonDefinitionsSchema = JSON.parse(definitionsSchema);

return protoValidate(
return validation.validateMultiSchema(
[jsonBaseSchema, jsonDefinitionsSchema],
{
alias: "abc",
Expand All @@ -60,7 +60,7 @@ test("Base schema and definitions schema test WITHOUT errors", () => {
let definitionsSchema = fs.readFileSync("examples/schemas/references/definitions-schema.json");
let jsonDefinitionsSchema = JSON.parse(definitionsSchema);

return protoValidate(
return validation.validateMultiSchema(
[jsonBaseSchema, jsonDefinitionsSchema],
{
alias: "abc",
Expand Down Expand Up @@ -88,7 +88,7 @@ test("ML schema -> base schema -> definitions schema test", () => {
let definitionsSchema = fs.readFileSync("examples/schemas/references/definitions-schema.json");
let jsonDefinitionsSchema = JSON.parse(definitionsSchema);

return protoValidate(
return validation.validateMultiSchema(
[jsonMLSchema, jsonBaseSchema, jsonDefinitionsSchema],
{
alias: "abc",
Expand All @@ -112,7 +112,7 @@ test("HCA ref schema and species schema test WITHOUT errors", () => {
let speciesSchema = fs.readFileSync("examples/schemas/references/hca-species-schema.json");
let jsonSpeciesSchema = JSON.parse(speciesSchema);

return protoValidate(
return validation.validateMultiSchema(
[jsonHcaSchema, jsonSpeciesSchema],
{
biomaterial_id: "abc",
Expand Down Expand Up @@ -142,7 +142,7 @@ test("HCA ref schema, species and restriction schema test WITHOUT errors", () =>
let inputSchema = fs.readFileSync("examples/schemas/references/graphRestriction-ref-schema.json");
let jsonRestrictionSchema = JSON.parse(inputSchema);

return protoValidate(
return validation.validateMultiSchema(
[jsonHcaSchema, jsonSpeciesSchema, jsonRestrictionSchema],
{
biomaterial_id: "abc",
Expand Down Expand Up @@ -177,7 +177,7 @@ test("HCA ref schema, species and restriction schema test WITH errors", () => {
let inputSchema = fs.readFileSync("examples/schemas/references/graphRestriction-ref-schema.json");
let jsonRestrictionSchema = JSON.parse(inputSchema);

return protoValidate(
return validation.validateMultiSchema(
[jsonHcaSchema, jsonSpeciesSchema, jsonRestrictionSchema],
{
biomaterial_id: "abc",
Expand Down
8 changes: 4 additions & 4 deletions test/validator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const fs = require("fs");
const runValidation = require("../src/validation/validator");

test("Empty Schema (empty object)", () => {
return runValidation({}, {}).then( (data) => {
return runValidation.validateSingleSchema({}, {}).then( (data) => {
expect(data).toBeDefined();
expect(data.length).toBe(0);
});
Expand All @@ -15,7 +15,7 @@ test("Attributes Schema", () => {
let inputObj = fs.readFileSync("examples/objects/attributes.json");
let jsonObj = JSON.parse(inputObj);

return runValidation(jsonSchema, jsonObj).then( (data) => {
return runValidation.validateSingleSchema(jsonSchema, jsonObj).then( (data) => {
expect(data).toBeDefined();
expect(data.length).toBe(1);
expect(data[0].errors.length).toBe(1);
Expand All @@ -30,7 +30,7 @@ test("BioSamples Schema - FAANG \'organism\' sample", () => {
let inputObj = fs.readFileSync("examples/objects/faang-organism-sample.json");
let jsonObj = JSON.parse(inputObj);

return runValidation(jsonSchema, jsonObj).then( (data) => {
return runValidation.validateSingleSchema(jsonSchema, jsonObj).then( (data) => {
expect(data).toBeDefined();
expect(data.length).toBe(0);
});
Expand All @@ -43,7 +43,7 @@ test("Study Schema", () => {
let inputObj = fs.readFileSync("examples/objects/study.json");
let jsonObj = JSON.parse(inputObj);

return runValidation(jsonSchema, jsonObj).then( (data) => {
return runValidation.validateSingleSchema(jsonSchema, jsonObj).then( (data) => {
expect(data).toBeDefined();
expect(data.length).toBe(2);
});
Expand Down
Loading

0 comments on commit e44ef0e

Please sign in to comment.