Learn about our RFC process, Open RFC meetings & more.Join in the discussion! »

elixir-jsonschema-validator

1.6.1 • Public • Published

JSON Schema Validator

Build Status tested with jest

This repository contains a JSON Schema that includes custom extensions of life science data. This package can be used directly or set to run as a node server that receives validation requests and gives back results. The validation is done using the AJV library version ^6.0.0 that fully supports the JSON Schema draft-07.

Contents

Installation

npm install elixir-jsonschema-validator

Usage

let { ElixirValidator} = require('elixir-jsonschema-validator');
let jsonSchema = {
    $schema: "http://json-schema.org/draft-07/schema#",
    type:"object",
    properties: {
        alias: {
         description: "A sample unique identifier in a submission.",
            type: "string"
        }
    },
    required: ["alias"]
};
 
let jsonObj = { alias : "MTB1"};
let validator = new ElixirValidator();
validator.validate(jsonSchema, jsonObj).then((validationResult) => {
    console.log(validationResult.validationState)
    for ( let errors of validationResult.validationErrors) {
        console.log(errors.userFriendlyMessage)
    }
});

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 three custom keywords implemented, graph_restriction, isChildTermOf and isValidTerm.

Pick the custom keywords you want to support and add them to the Elixir validator:

// get all the custom extensions
let { ElixirValidator, GraphRestriction, IsChildTermOf, IsValidTerm]} = require('elixir-jsonschema-validator');
// only use the graph_extension keyword
let validator = new ElixirValidator([GraphRestriction])

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.

This keyword works by doing an asynchronous call to the 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:

{
    "$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:

{
    "ontology": "UBERON:0000955"
}

isChildTermOf

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.

This keyword works by doing an asynchronous call to the 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:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$async": true,
  "properties": {
    "term": {
      "type": "string",
      "format": "uri",
      "isChildTermOf": {
        "parentTerm": "http://purl.obolibrary.org/obo/PATO_0000047",
        "ontologyId": "pato"
      }
    }
  }
}

JSON object:

{
  "term": "http://purl.obolibrary.org/obo/PATO_0000383"
}

isValidTerm

This custom keyword evaluates if a given ontology term url exists in OLS (Ontology Lookup Service). It is applied to a string (url) and passes validation if the term exists in OLS. It can be aplied to any string defined in the schema.

This keyword works by doing an asynchronous call to the OLS API that will respond with the required information to determine if the term exists in OLS or not. 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:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$async": true,
 
  "properties": {
    "url": {
      "type": "string",
      "format": "uri",
      "isValidTerm": true
    }
  }
}

JSON object:

{
  "url": "http://purl.obolibrary.org/obo/PATO_0000383"
}

isvalidTaxonomy

This custom keyword evaluates if a given taxonomy exists in ENA's Taxonomy Browser. It is applied to a string (url) and passes validation if the taxonomy exists in ENA. It can be aplied to any string defined in the schema.

This keyword works by doing an asynchronous call to the ENA API that will respond with the required information to determine if the term exists or not. Being an async validation step, whenever used, the schema must have the flag: "$async": true in it's object root.

Usage

Schema:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "title": "Is valid taxonomy expression.",
  "$async": true,
  
  "properties": {
    "value": { 
      "type": "string", 
      "minLength": 1, 
      "isValidTaxonomy": true
    }
  }
}

JSON object:

{
  "metagenomic source" : [ {
    "value" : "wastewater metagenome"
  } ]
}

Validation API

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:

{
  "schema": {},
  "object": {}
}

Where the schema should be a valid json schema to validate the object against.

Example:

{
  "schema": {
    "$schema": "http://json-schema.org/draft-07/schema#",
    
    "type": "object",
    "properties": {
      "alias": {
        "description": "A sample unique identifier in a submission.",
        "type": "string"
      },
      "taxonId": {
        "description": "The taxonomy id for the sample species.",
        "type": "integer"
      },
      "taxon": {
        "description": "The taxonomy name for the sample species.",
        "type": "string"
      },
      "releaseDate": {
        "description": "Date from which this sample is released publicly.",
        "type": "string",
        "format": "date"
      }
    },  
    "required": ["alias", "taxonId" ]
  },
  "object": {
    "alias": "MA456",
    "taxonId": 9606
  }
}

Output

Response with no validation errors:

HTTP status code 200

[]

An example of a validation response with errors:

HTTP status code 200

[
  {
    "errors": [
        "should have required property 'value'"
    ],
    "dataPath": ".attributes['age'][0].value"
  },
  {
    "errors": [
        "should NOT be shorter than 1 characters",
        "should match format \"uri\""
    ],
    "dataPath": ".attributes['breed'][0].terms[0].url"
  }
]

Where errors is an array of error messages for a given input identified by its path on dataPath. There may be one or more error objects within the response array. An empty array represents a valid validation result.

API Errors

Sending malformed JSON or a body with either the schema or the submittable missing will result in an API error (the request will not reach the validation).

  • When sending malformed JSON:

    HTTP status code 400 - Bad Request

    {
      "errors": "Malformed JSON please check your request body."
    }
  • When any of the required body values is missing:

    HTTP status code 422 - Unprocessable Entity

    {
      "errors": {
        "schema": {
          "location": "body",
          "param": "schema",
          "msg": "Required."
        },
        "object": {
          "location": "body",
          "param": "object",
          "msg": "Required."
        }
      }
    }

Running locally

These instructions will get you a copy of the project up and running on your local machine for development and testing purposes.

Prerequisites

To be able to run this project you'll need to have Node.js and npm installed in your machine. npm is distributed with Node.js which means that when you download Node.js, you automatically get npm installed on your computer.

Installing

Node.js / npm

brew install node

After installation check that everything is correctly installed and which versions you are running:

node -v
npm -v

Project

Clone project and install dependencies:

git clone https://github.com/elixir-europe/json-schema-validator.git
cd json-schema-validator
npm install

Running the Tests

npm test

Executing

node src/server

The node server will run on port 3020 and will expose one endpoint: /validate.

Startup arguments

  • logPath

If provided with a log path argument, the application will write the logs to a file on the specified directory with a 24h rotation. To provide the log path add a logPath property after the startup statement:

node src/server --logPath=/log/directory/path
  • pidPath

If provided with a pid file path argument, the application will write the pid into the specified file. If no pid file argument is provided, the application will still create a pid file on the default path: ./server.pid. To provide the pid file path add a pidPath property after the startup statement:

node src/server --pidPath=/pid/file/path/server.pid

Note: This is the file path and not just the directory it will be written to.

Executing with Docker

  1. Build docker image:
docker build -t elixir-jsonschema-validator .
  1. Run docker image:
docker run -p 3020:3020 -d elixir-jsonschema-validator

Development

For development purposes using nodemon is useful. It reloads the application everytime something has changed on save time.

nodemon src/server

License

For more details about licensing see the LICENSE.

Install

npm i elixir-jsonschema-validator

DownloadsWeekly Downloads

2

Version

1.6.1

License

Apache-2.0

Unpacked Size

196 kB

Total Files

74

Last publish

Collaborators

  • avatar