forked from Open-EO/openeo-processes
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtestHelpers.js
223 lines (202 loc) · 4.66 KB
/
testHelpers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
const glob = require('glob');
const fs = require('fs');
const path = require('path');
const ajv = require('ajv');
const $RefParser = require("@apidevtools/json-schema-ref-parser");
const markdownlint = require('markdownlint');
const spellcheck = require('markdown-spellcheck').default;
const ajvOptions = {
schemaId: 'auto',
format: 'full'
};
const spellcheckOptions = {
ignoreAcronyms: true,
ignoreNumbers: true,
suggestions: false,
relativeSpellingFiles: true,
dictionary: {
language: "en-us"
}
};
// Read custom dictionary for spell check
const words = fs.readFileSync('.words').toString().split(/\r\n|\n|\r/);
for(let i in words) {
spellcheck.spellcheck.addWord(words[i]);
}
// Add the process IDs to the word list
const files = glob.sync("../{*,proposals/*}.json", {realpath: true});
for(let i in files) {
spellcheck.spellcheck.addWord(path.basename(files[i], path.extname(files[i])));
}
async function getAjv() {
let subtypes = await $RefParser.dereference(
require('../meta/subtype-schemas.json'),
{
dereference: { circular: "ignore" }
}
);
let jsv = new ajv(ajvOptions);
jsv.addKeyword("parameters", {
dependencies: [
"type",
"subtype"
],
metaSchema: {
type: "array",
items: {
type: "object",
required: [
"name",
"description",
"schema"
],
properties: {
name: {
type: "string",
pattern: "^[A-Za-z0-9_]+$"
},
description: {
type: "string"
},
optional: {
type: "boolean"
},
deprecated: {
type: "boolean"
},
experimental: {
type: "boolean"
},
default: {
// Any type
},
schema: {
oneOf: [
{
type: "object",
// ToDo: Check Schema
},
{
type: "array",
items: {
type: "object"
// ToDo: Check Schema
}
}
]
}
}
}
},
valid: true
});
jsv.addKeyword("subtype", {
dependencies: [
"type"
],
metaSchema: {
type: "string",
enum: Object.keys(subtypes.definitions)
},
compile: function (subtype, schema) {
if (schema.type != subtypes.definitions[subtype].type) {
throw "Subtype '"+subtype+"' not allowed for type '"+schema.type+"'."
}
return () => true;
},
errors: false
});
return jsv;
}
function isObject(obj) {
return (typeof obj === 'object' && obj === Object(obj) && !Array.isArray(obj));
}
function normalizeString(str) {
return str.replace(/\r\n|\r|\n/g, "\n").trim();
}
function checkDescription(text, p = null, commonmark = true) {
if (!text) {
return;
}
// Check markdown
if (commonmark) {
const options = {
strings: {
description: text
},
config: {
"line-length": false, // Nobody cares in JSON files anyway
"first-line-h1": false, // Usually no headings in descriptions
"fenced-code-language": false, // Usually no languages available anyway
"single-trailing-newline": false, // New lines at end of a JSON string doesn't make sense. We don't have files here.
}
};
const result = markdownlint.sync(options);
expect(result).toEqual({description: []});
}
// Check spelling
checkSpelling(text, p);
}
function checkSpelling(text, p = null) {
if (!text) {
return;
}
const errors = spellcheck.spell(text, spellcheckOptions);
if (errors.length > 0) {
let pre = "Misspelled word";
if (p && p.id) {
pre += " in " + p.id;
}
console.warn(pre + ": " + JSON.stringify(errors));
}
}
function prepareSchema(schema) {
if (Array.isArray(schema)) {
schema = {
anyOf: schema
};
}
if (typeof schema["$schema"] === 'undefined') {
// Set applicable JSON SChema draft version if not already set
schema["$schema"] = "http://json-schema.org/draft-07/schema#";
}
return schema;
}
function checkJsonSchema(jsv, schema, checkFormat = true) {
if (Array.isArray(schema)) {
// lint: For array schemas there should be more than one schema specified, otherwise use directly the schema object
expect(schema.length).toBeGreaterThan(1);
}
let result = jsv.compile(prepareSchema(schema));
expect(result.errors).toBeNull();
checkSchemaRecursive(schema, checkFormat);
}
function checkSchemaRecursive(schema, checkFormat = true) {
for(var i in schema) {
var val = schema[i];
if (typeof val === 'object' && val !== null) {
checkSchemaRecursive(val, checkFormat);
}
switch(i) {
case 'title':
case 'description':
checkSpelling(val);
break;
case 'format':
if (checkFormat && schema.subtype !== val) {
throw "format '"+val+"' has no corresponding subtype.";
}
break;
}
}
}
module.exports = {
getAjv,
normalizeString,
checkDescription,
checkSpelling,
checkJsonSchema,
checkSchemaRecursive,
prepareSchema,
isObject
};