diff --git a/dist/protocol.esm.js b/dist/protocol.esm.js index 3971ac0d7eaefbf8ce14623624c8de48e3b1d9bc..af2c4d3897ce985e5fe135acedf3f608326f59ae 100644 --- a/dist/protocol.esm.js +++ b/dist/protocol.esm.js @@ -1,3 +1,4 @@ +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 }; diff --git a/dist/protocol.js b/dist/protocol.js index 6d8c468511c78e84c6c42b8aa36f762f8f27fbaf..748120d0fb9a8c71cd073cb0cd65671442e3f617 100644 --- a/dist/protocol.js +++ b/dist/protocol.js @@ -1,5 +1,6 @@ '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; diff --git a/src/protocol/index.js b/src/protocol/index.js index 266542c20803fe75780fd22836ff59e2957cfc7d..a0bc8f73aea588256fbc0c0380db64327917728f 100644 --- a/src/protocol/index.js +++ b/src/protocol/index.js @@ -1,3 +1,4 @@ +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