Commit aafb2f90 authored by Dan Jones's avatar Dan Jones
Browse files

Merge branch...

Merge branch '14-create-genericsoarprotocol-class-to-handle-loading-the-real-schema-and-validation' into 'dev'

Resolve "Create GenericSoarProtocol class to handle loading the real schema and validation"

Closes #14

See merge request !9
parents 1d246e62 cce75dee
import axios from 'axios';
import Validator from 'swagger-model-validator';
/**
......@@ -25,8 +26,17 @@ import Validator from 'swagger-model-validator';
class GenericProtocol {
constructor(schema, services) {
this.schema = schema;
this.validator = new Validator(schema);
this.services = services;
this.createValidator(schema);
}
/**
* Create a Validator from the provided JSONSchema
* @param {object} schema
* @returns {Validator}
*/
createValidator(schema) {
this.validator = new Validator(schema);
}
/**
......@@ -84,4 +94,67 @@ class GenericProtocol {
}
}
export { GenericProtocol };
/**
* GenericSoarProtocol defines a simple passthru handler for messages
*
* This provides the same as above but loads a version of the
* SoAR message protocol
*/
class GenericSoarProtocol extends GenericProtocol {
constructor(schema, services) {
super(schema, services);
}
/**
* Create a Validator from the provided JSONSchema
*
* If schema is a string build a URL and retrieve the
* schema from gitlab
* @param {string|object} schema
* @returns {object}
*/
createValidator(schema) {
if (typeof schema === 'string' && schema.match(/^[\w\.]+$/)) {
this.loadSchema(schema)
.then((schema) => {
this.validator = new Validator(schema);
});
} else {
this.validator = new Validator(schema);
}
}
/**
* Load schema from gitlab by tag/branch/commitref
* @param {string} version
* @returns {object}
*/
loadSchema(version) {
this.axios = axios;
let repository = "https://git.noc.ac.uk/communications-backbone-system/backbone-message-format";
let url = `${repository}/-/raw/${version}/project/soar/swagger.json`;
return this.axios.get(url)
.then((response) => {
this.schema = response.data;
return response.data;
});
}
/**
* Validate that a message meets the reqiured schema
* @param {object} message
* @returns {object}
*/
validate(message) {
return this.validator.validate(
message,
this.schema.components.schemas.MESSAGE,
this.schema.components.schemas,
false,
true
);
}
}
export { GenericProtocol, GenericSoarProtocol };
'use strict';
var axios = require('axios');
var Validator = require('swagger-model-validator');
/**
......@@ -27,8 +28,17 @@ var Validator = require('swagger-model-validator');
class GenericProtocol {
constructor(schema, services) {
this.schema = schema;
this.validator = new Validator(schema);
this.services = services;
this.createValidator(schema);
}
/**
* Create a Validator from the provided JSONSchema
* @param {object} schema
* @returns {Validator}
*/
createValidator(schema) {
this.validator = new Validator(schema);
}
/**
......@@ -86,4 +96,68 @@ class GenericProtocol {
}
}
/**
* GenericSoarProtocol defines a simple passthru handler for messages
*
* This provides the same as above but loads a version of the
* SoAR message protocol
*/
class GenericSoarProtocol extends GenericProtocol {
constructor(schema, services) {
super(schema, services);
}
/**
* Create a Validator from the provided JSONSchema
*
* If schema is a string build a URL and retrieve the
* schema from gitlab
* @param {string|object} schema
* @returns {object}
*/
createValidator(schema) {
if (typeof schema === 'string' && schema.match(/^[\w\.]+$/)) {
this.loadSchema(schema)
.then((schema) => {
this.validator = new Validator(schema);
});
} else {
this.validator = new Validator(schema);
}
}
/**
* Load schema from gitlab by tag/branch/commitref
* @param {string} version
* @returns {object}
*/
loadSchema(version) {
this.axios = axios;
let repository = "https://git.noc.ac.uk/communications-backbone-system/backbone-message-format";
let url = `${repository}/-/raw/${version}/project/soar/swagger.json`;
return this.axios.get(url)
.then((response) => {
this.schema = response.data;
return response.data;
});
}
/**
* Validate that a message meets the reqiured schema
* @param {object} message
* @returns {object}
*/
validate(message) {
return this.validator.validate(
message,
this.schema.components.schemas.MESSAGE,
this.schema.components.schemas,
false,
true
);
}
}
exports.GenericProtocol = GenericProtocol;
exports.GenericSoarProtocol = GenericSoarProtocol;
import axios from 'axios';
import Validator from 'swagger-model-validator';
/**
......@@ -25,8 +26,17 @@ import Validator from 'swagger-model-validator';
export class GenericProtocol {
constructor(schema, services) {
this.schema = schema;
this.validator = new Validator(schema);
this.services = services;
this.createValidator(schema);
}
/**
* Create a Validator from the provided JSONSchema
* @param {object} schema
* @returns {Validator}
*/
createValidator(schema) {
this.validator = new Validator(schema);
}
/**
......@@ -83,3 +93,66 @@ export class GenericProtocol {
return message;
}
}
/**
* GenericSoarProtocol defines a simple passthru handler for messages
*
* This provides the same as above but loads a version of the
* SoAR message protocol
*/
export class GenericSoarProtocol extends GenericProtocol {
constructor(schema, services) {
super(schema, services);
}
/**
* Create a Validator from the provided JSONSchema
*
* If schema is a string build a URL and retrieve the
* schema from gitlab
* @param {string|object} schema
* @returns {object}
*/
createValidator(schema) {
if (typeof schema === 'string' && schema.match(/^[\w\.]+$/)) {
this.loadSchema(schema)
.then((schema) => {
this.validator = new Validator(schema);
});
} else {
this.validator = new Validator(schema);
}
}
/**
* Load schema from gitlab by tag/branch/commitref
* @param {string} version
* @returns {object}
*/
loadSchema(version) {
this.axios = axios;
let repository = "https://git.noc.ac.uk/communications-backbone-system/backbone-message-format";
let url = `${repository}/-/raw/${version}/project/soar/swagger.json`;
return this.axios.get(url)
.then((response) => {
this.schema = response.data
return response.data;
});
}
/**
* Validate that a message meets the reqiured schema
* @param {object} message
* @returns {object}
*/
validate(message) {
return this.validator.validate(
message,
this.schema.components.schemas.MESSAGE,
this.schema.components.schemas,
false,
true
);
}
}
\ 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