-
Notifications
You must be signed in to change notification settings - Fork 2
/
replay-validate.js
46 lines (39 loc) · 1.15 KB
/
replay-validate.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
const apiSchemaBuilder = require("api-schema-builder");
const schema = apiSchemaBuilder.buildSchemaSync("./swagger/hotplay.json");
module.exports = function(json, callback) {
let schemaEndpoint = schema["/replay"]["post"];
schemaEndpoint.body.validate(json);
let errResult = beautifyAjvError(schemaEndpoint.body.errors);
if (callback) {
if (errResult) {
callback(errResult);
}
else {
callback(null);
}
}
}
function beautifyAjvError(errorArr) {
let errorObj = {};
let result = "";
if (errorArr) {
for (let i = 0; i < errorArr.length; i++) {
try {
let err = errorArr[i];
let errString = err.dataPath + " " + err.message;
if (errString.charAt(0) == ".") {
errString = errString.substring(1);
}
errorObj[err.dataPath] = errString.trim();
result += errorObj[err.dataPath] + "\n";
}
catch (e) {
console.log(e);
}
}
return result.trim();
}
else {
return null;
}
}