Skip to content

Commit

Permalink
Additional tests, refactoring and bug fixes (e.g. #2)
Browse files Browse the repository at this point in the history
  • Loading branch information
m-mohr committed Aug 14, 2020
1 parent f8a6202 commit d7db480
Show file tree
Hide file tree
Showing 20 changed files with 2,068 additions and 841 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"assets/subtype-schemas.json"
],
"dependencies": {
"@openeo/js-commons": "^1.1.0",
"@openeo/js-commons": "^1.1.1",
"ajv": "^6.12.0"
},
"devDependencies": {
Expand All @@ -44,7 +44,7 @@
"docs": "jsdoc src -r -d docs/ -P package.json -R README.md",
"build": "npx webpack",
"compat": "jshint src",
"test": "jest --testPathIgnorePatterns assets/ --env=jsdom",
"test_node": "jest --testPathIgnorePatterns assets/ --env=node"
"test": "jest --env=jsdom",
"test_node": "jest --env=node"
}
}
13 changes: 10 additions & 3 deletions src/error.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const Utils = require('@openeo/js-commons/src/utils.js');
const Utils = require('./utils');

const MESSAGES = {
"MultipleResultNodes": "Multiple result nodes specified for process graph.",
Expand All @@ -20,7 +20,12 @@ const MESSAGES = {
"ProcessMissing": "No process specified"
};

module.exports = class ProcessGraphError extends Error {
/**
* An error class for this library.
*
* @class
*/
class ProcessGraphError extends Error {

constructor(codeOrMsg, variables = {}) {
super();
Expand All @@ -42,4 +47,6 @@ module.exports = class ProcessGraphError extends Error {
};
}

};
}

module.exports = ProcessGraphError;
11 changes: 9 additions & 2 deletions src/errorlist.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
module.exports = class ErrorList {
/**
* A list of errors.
*
* @class
*/
class ErrorList {

constructor() {
this.errors = [];
Expand Down Expand Up @@ -50,4 +55,6 @@ module.exports = class ErrorList {
return this.errors;
}

};
}

module.exports = ErrorList;
67 changes: 58 additions & 9 deletions src/jsonschema.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
const ajv = require('ajv');
const Utils = require('@openeo/js-commons/src/utils.js');
const Utils = require('./utils');
const ProcessUtils = require('@openeo/js-commons/src/processUtils.js');
const keywords = require('./keywords');

var geoJsonSchema = require("../assets/GeoJSON.json");
var subtypeSchemas = require("../assets/subtype-schemas.json");
const ProcessGraph = require('./processgraph');

module.exports = class JsonSchemaValidator {
/**
* JSON Schema Validator.
*
* @class
*/
class JsonSchemaValidator {

constructor() {
this.ajv = new ajv({
Expand All @@ -32,6 +38,7 @@ module.exports = class JsonSchemaValidator {
output: null
};
this.epsgCodes = null;
this.processRegistry = null;
}

getFunctionName(subtype) {
Expand Down Expand Up @@ -90,6 +97,11 @@ module.exports = class JsonSchemaValidator {
Object.assign(schema, subtypeSchemas.definitions[subtype]);
// Remove subtype to avoid recursion
delete schema.subtype;
if (subtype === 'process-graph') {
// Special case: all validation will be done in validateProcessGraph()
delete schema.required;
delete schema.properties;
}
}
else {
schema = this.makeSchema(schema, true);
Expand Down Expand Up @@ -173,8 +185,24 @@ module.exports = class JsonSchemaValidator {

async validateWkt2Definition(data) {
// To be overridden by end-user application, just doing a very basic check here based on code ported over from proj4js
var codeWords = ['PROJECTEDCRS', 'PROJCRS', 'GEOGCS','GEOCCS','PROJCS','LOCAL_CS', 'GEODCRS', 'GEODETICCRS', 'GEODETICDATUM', 'ENGCRS', 'ENGINEERINGCRS'];
return codeWords.some(word => data.indexOf(word) > -1);
var codeWords = [
'BOUNDCRS',
'COMPOUNDCRS',
'ENGCRS', 'ENGINEERINGCRS',
'GEODCRS', 'GEODETICCRS',
'GEOGCRS', 'GEOGRAPHICCRS',
'PARAMETRICCRS',
'PROJCRS', 'PROJECTEDCRS',
'TIMECRS',
'VERTCRS', 'VERTICALCRS'
];
data = data.toUpperCase();
if (!codeWords.some(word => data.indexOf(word) !== -1)) {
throw new ajv.ValidationError([{
message: "Invalid WKT2 string specified."
}]);
}
return true;
}

async validateTemporalInterval(data) {
Expand Down Expand Up @@ -203,11 +231,30 @@ module.exports = class JsonSchemaValidator {
return true;
}

setProcessGraphParser(processGraph) {
this.processGraph = processGraph;
}

async validateProcessGraph(data) {
const ProcessGraph = require('./processgraph'); // Avoid circular reference
var parser = new ProcessGraph(data);
parser.parse();
return true;
try {
const ProcessGraph = require('./processgraph');
var parser;
if (data instanceof ProcessGraph) {
parser = data;
}
else if (this.processGraph) {
parser = this.processGraph.createProcessGraphInstance(data);
}
else {
parser = new ProcessGraph(data, null, this);
}
await parser.validate();
return true;
} catch (error) {
throw new ajv.ValidationError([{
message: error.message
}]);
}
}

// Checks whether the valueSchema is compatible to the paramSchema.
Expand Down Expand Up @@ -255,4 +302,6 @@ module.exports = class JsonSchemaValidator {
return compatible !== -1;
}

};
}

module.exports = JsonSchemaValidator;
4 changes: 3 additions & 1 deletion src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const ProcessGraph = require('./processgraph');
const ProcessGraphError = require('./error');
const ProcessGraphNode = require('./node');
const ProcessRegistry = require('./registry');
const Utils = require('./utils');

module.exports = {
BaseProcess,
Expand All @@ -13,5 +14,6 @@ module.exports = {
ProcessGraph,
ProcessGraphError,
ProcessGraphNode,
ProcessRegistry
ProcessRegistry,
Utils
};
Loading

0 comments on commit d7db480

Please sign in to comment.