@cyclonedx/cyclonedx-library
Version:
Core functionality of CycloneDX for JavaScript (Node.js or WebBrowser).
50 lines (46 loc) • 2.07 kB
JavaScript
/*!
This file is part of CycloneDX JavaScript Library.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
SPDX-License-Identifier: Apache-2.0
Copyright (c) OWASP Foundation. All Rights Reserved.
*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const promises_1 = require("node:fs/promises");
const ajv_1 = __importDefault(require("ajv"));
const ajv_formats_1 = __importDefault(require("ajv-formats"));
const ajv_formats_draft2019_1 = __importDefault(require("ajv-formats-draft2019"));
const ajvOptions = Object.freeze({
useDefaults: false,
strict: false,
strictSchema: false,
addUsedSchema: false
});
exports.default = (async function (schemaPath, schemaMap = {}) {
const [schema, schemas] = await Promise.all([
(0, promises_1.readFile)(schemaPath, 'utf-8').then(c => JSON.parse(c)),
Promise.all(Object.entries(schemaMap).map(async ([k, v]) => await (0, promises_1.readFile)(v, 'utf-8').then(c => [k, JSON.parse(c)]))).then(es => Object.fromEntries(es))
]);
const ajv = new ajv_1.default({ ...ajvOptions, schemas });
(0, ajv_formats_1.default)(ajv);
(0, ajv_formats_draft2019_1.default)(ajv, { formats: ['idn-email'] });
ajv.addFormat('iri-reference', true);
const validator = ajv.compile(schema);
return function (data) {
return validator(JSON.parse(data))
? null
: validator.errors;
};
});
//# sourceMappingURL=ajv.js.map
;