UNPKG

4.31 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.jsonSchemaValidate = void 0;
4const ajv_1 = require("ajv");
5const ajv_formats_1 = require("ajv-formats");
6const debug_ = require("debug");
7const fs = require("fs");
8const path = require("path");
9const debug = debug_("r2:streamer#utils/json-schema-validate");
10const _cachedJsonSchemas = {};
11function jsonSchemaValidate(jsonSchemasRootpath, jsonSchemasNames, jsonToValidate) {
12 try {
13 for (const jsonSchemaName of jsonSchemasNames) {
14 const jsonSchemaName_ = jsonSchemaName.replace(/\//g, path.sep);
15 const jsonSchemaPath = path.join(jsonSchemasRootpath, jsonSchemaName_ + ".schema.json");
16 if (_cachedJsonSchemas[jsonSchemaPath]) {
17 continue;
18 }
19 if (!fs.existsSync(jsonSchemaPath)) {
20 debug("Skipping JSON SCHEMAS (not found): " + jsonSchemaPath);
21 return undefined;
22 }
23 let jsonSchemaStr = fs.readFileSync(jsonSchemaPath, { encoding: "utf8" });
24 if (!jsonSchemaStr) {
25 debug("File load fail: " + jsonSchemaPath);
26 return undefined;
27 }
28 jsonSchemaStr = jsonSchemaStr.replace(/\?<grandfathered>/g, "");
29 jsonSchemaStr = jsonSchemaStr.replace(/\?<privateUse>/g, "");
30 jsonSchemaStr = jsonSchemaStr.replace(/\?<privateUse2>/g, "");
31 jsonSchemaStr = jsonSchemaStr.replace(/\?<extension>/g, "");
32 jsonSchemaStr = jsonSchemaStr.replace(/\?<variant>/g, "");
33 jsonSchemaStr = jsonSchemaStr.replace(/\?<script>/g, "");
34 jsonSchemaStr = jsonSchemaStr.replace(/\?<extlang>/g, "");
35 jsonSchemaStr = jsonSchemaStr.replace(/\?<language>/g, "");
36 jsonSchemaStr = jsonSchemaStr.replace(/\?<region>/g, "");
37 if (jsonSchemaStr.indexOf("?<") >= 0) {
38 debug("REGEX WARNING!!");
39 return undefined;
40 }
41 const jsonSchema = global.JSON.parse(jsonSchemaStr);
42 debug(`JSON SCHEMA is now cached: ${jsonSchema["$id"]} (${jsonSchemaPath})`);
43 _cachedJsonSchemas[jsonSchemaPath] = jsonSchema;
44 }
45 const ajv = new ajv_1.default({
46 allErrors: true,
47 allowUnionTypes: true,
48 coerceTypes: false,
49 strict: true,
50 validateFormats: true,
51 verbose: true,
52 });
53 ajv_formats_1.default(ajv);
54 let idRoot;
55 for (const jsonSchemaName of jsonSchemasNames) {
56 const jsonSchemaPath = path.join(jsonSchemasRootpath, jsonSchemaName + ".schema.json");
57 const jsonSchema = _cachedJsonSchemas[jsonSchemaPath];
58 if (!jsonSchema) {
59 debug("!jsonSchema?? " + jsonSchemaPath);
60 return undefined;
61 }
62 if (!idRoot) {
63 idRoot = jsonSchema["$id"];
64 }
65 ajv.addSchema(jsonSchema, jsonSchema["$id"]);
66 }
67 if (!idRoot) {
68 debug("!idRoot?? ");
69 return undefined;
70 }
71 const ajvValid = ajv.validate(idRoot, jsonToValidate);
72 if (!ajvValid) {
73 const errors = ajv.errors;
74 if (errors) {
75 const errs = [];
76 for (const err of errors) {
77 const jsonPath = err.dataPath.replace(/^\./, "").replace(/\[([0-9]+)\]/g, ".$1");
78 errs.push({
79 ajvDataPath: err.dataPath,
80 ajvMessage: err.message ? err.message : "",
81 ajvSchemaPath: err.schemaPath,
82 jsonPath,
83 });
84 }
85 return errs;
86 }
87 }
88 }
89 catch (err) {
90 debug("JSON Schema VALIDATION PROBLEM.");
91 debug(err);
92 const errs = [];
93 errs.push({
94 ajvDataPath: err && toString ? err.toString() : "ajvDataPath",
95 ajvMessage: err.message ? err.message : "ajvMessage",
96 ajvSchemaPath: "ajvSchemaPath",
97 jsonPath: "jsonPath",
98 });
99 return errs;
100 }
101 return undefined;
102}
103exports.jsonSchemaValidate = jsonSchemaValidate;
104//# sourceMappingURL=json-schema-validate.js.map
\No newline at end of file