From 8465b44b917b8216c2565befbbbfe754ea9cc65d Mon Sep 17 00:00:00 2001 From: Dani Welter Date: Thu, 26 Jul 2018 18:50:54 +0100 Subject: [PATCH] Removed duplicate script and updated readme --- README.md | 47 +++++++++++++++++++++--- src/custom/isvalidterm_original.js | 57 ------------------------------ 2 files changed, 43 insertions(+), 61 deletions(-) delete mode 100644 src/custom/isvalidterm_original.js diff --git a/README.md b/README.md index d392dc7..e223569 100644 --- a/README.md +++ b/README.md @@ -92,7 +92,7 @@ nodemon src/server ``` ## Validation API -This validator exposes two endpoints that will accept POST requests: `/validate` and `/prototype`. +This validator exposes two endpoints that will accept POST requests: `/validate` for a single stand-alone schema and data object, and `/validateRefs` for a complex schema referencing other schemas and a related data object. ### /validate The endpoint will expect the body to have the following structure: @@ -139,7 +139,7 @@ Where the schema should be a valid json schema to validate the object against. } ``` -### /prototype +### /validateRefs The endpoint will expect the body to have the following structure: ```js { @@ -247,10 +247,49 @@ Sending malformed JSON or a body with either the schema or the submittable missi ## Custom keywords The AJV library supports the implementation of custom json schema keywords to address validation scenarios that go beyond what json schema can handle. -This validator has two custom keywords implemented, `isChildTermOf` and `isValidTerm`. +This validator has three custom keywords implemented, `graph_restriction`, `isChildTermOf` and `isValidTerm`. + +### graph_restriction + +This custom keyword *evaluates if an ontology term is child of another*. This keyword is applied to a string (CURIE) and **passes validation if the term is a child of the term defined in the schema**. +The keyword requires one or more **parent terms** *(classes)* and **ontology ids** *(ontologies)*, both of which should exist in [OLS - Ontology Lookup Service](https://www.ebi.ac.uk/ols). + +This keyword works by doing an asynchronous call to the [OLS API](https://www.ebi.ac.uk/ols/api/) that will respond with the required information to know if a given term is child of another. +Being an async validation step, whenever used is a schema, the schema must have the flag: `"$async": true` in it's object root. + + +#### Usage +Schema: +```js +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "$id": "http://schema.dev.data.humancellatlas.org/module/ontology/5.3.0/organ_ontology", + "$async": true, + "properties": { + "ontology": { + "description": "A term from the ontology [UBERON](https://www.ebi.ac.uk/ols/ontologies/uberon) for an organ or a cellular bodily fluid such as blood or lymph.", + "type": "string", + "graph_restriction": { + "ontologies" : ["obo:hcao", "obo:uberon"], + "classes": ["UBERON:0000062","UBERON:0000179"], + "relations": ["rdfs:subClassOf"], + "direct": false, + "include_self": false + } + } + } +} +``` +JSON object: +```js +{ + "ontology": "UBERON:0000955" +} +``` + ### isChildTermOf -This custom keyword *evaluates if an ontology term is child of other*. This keyword is applied to a string (url) and **passes validation if the term is a child of the term defined in the schema**. +This custom keyword also *evaluates if an ontology term is child of another* and is a simplified version of the graph_restriction keyword. This keyword is applied to a string (url) and **passes validation if the term is a child of the term defined in the schema**. The keyword requires the **parent term** and the **ontology id**, both of which should exist in [OLS - Ontology Lookup Service](https://www.ebi.ac.uk/ols). This keyword works by doing an asynchronous call to the [OLS API](https://www.ebi.ac.uk/ols/api/) that will respond with the required information to know if a given term is child of another. diff --git a/src/custom/isvalidterm_original.js b/src/custom/isvalidterm_original.js deleted file mode 100644 index 8551509..0000000 --- a/src/custom/isvalidterm_original.js +++ /dev/null @@ -1,57 +0,0 @@ -var Ajv = require("ajv"); -var request = require("request"); -const logger = require("../winston"); -const CustomAjvError = require("../model/custom-ajv-error"); - -module.exports = function isValidTerm(ajv) { - - function findTerm(schema, data) { - return new Promise((resolve, reject) => { - if(schema) { - const olsSearchUrl = "https://www.ebi.ac.uk/ols/api/search?q="; - let errors = []; - - const termUri = data; - const encodedTermUri = encodeURIComponent(termUri); - const url = olsSearchUrl + encodedTermUri + "&exact=true&groupField=true&queryFields=iri"; - - logger.log("debug", `Looking for term [${termUri}] in OLS.`); - request(url, (error, Response, body) => { - let jsonBody = JSON.parse(body); - - if(jsonBody.response.numFound === 1) { - logger.log("debug", "Found 1 match!"); - resolve(true); - } else if (jsonBody.response.numFound === 0) { - logger.log("debug", "Could not find term in OLS."); - errors.push( - new CustomAjvError( - "isValidTerm", `provided term does not exist in OLS: [${termUri}]`, - {keyword: "isValidTerm"}) - ); - reject(new Ajv.ValidationError(errors)); - } else { - errors.push( - new CustomAjvError( - "isValidTerm", "Something went wrong while validating term, try again.", - {keyword: "isValidTerm"}) - ); - reject(new Ajv.ValidationError(errors)); - } - }); - } else { - resolve(true); - } - }); - } - - isValidTerm.definition = { - async: true, - type: "string", - validate: findTerm, - errors: true - }; - - ajv.addKeyword("isValidTerm", isValidTerm.definition); - return ajv; -};