@naturalcycles/nodejs-lib
Version:
Standard library for Node.js
34 lines (33 loc) • 1.12 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.readAjvSchemas = exports.readJsonSchemas = void 0;
const fs = require("fs");
const __1 = require("../..");
const ajvSchema_1 = require("./ajvSchema");
/**
* Does fs.readFileSync + JSON.parse for ALL files matching the passed `glob` pattern.
* E.g `someDir/**\/*.schema.json`
*
* Returns them as an array of JsonSchema.
*
* @experimental
*/
function readJsonSchemas(patterns, opt) {
return __1.fastGlob.sync(patterns, opt).map(fileName => JSON.parse(fs.readFileSync(fileName, 'utf8')));
}
exports.readJsonSchemas = readJsonSchemas;
/**
* Reads json schemas from given dir (glob pattern).
* Creates new AjvSchema for each of them (ajv validates them upon creation).
* Passes `schemas` option to ajv, so, schemas may $ref each other and it'll be fine.
*
* @experimental
*/
function readAjvSchemas(patterns, cfg) {
const schemas = readJsonSchemas(patterns);
return schemas.map(schema => ajvSchema_1.AjvSchema.create(schema, {
schemas,
...cfg,
}));
}
exports.readAjvSchemas = readAjvSchemas;
;