UNPKG

1.11 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.readAjvSchemas = exports.readJsonSchemas = void 0;
4const fs = require("fs");
5const globby = require("globby");
6const ajvSchema_1 = require("./ajvSchema");
7/**
8 * Does fs.readFileSync + JSON.parse for ALL files matching the passed `glob` pattern.
9 * E.g `someDir/**\/*.schema.json`
10 *
11 * Returns them as an array of JsonSchema.
12 *
13 * @experimental
14 */
15function readJsonSchemas(patterns, opt) {
16 return globby.sync(patterns, opt).map(fileName => JSON.parse(fs.readFileSync(fileName, 'utf8')));
17}
18exports.readJsonSchemas = readJsonSchemas;
19/**
20 * Reads json schemas from given dir (glob pattern).
21 * Creates new AjvSchema for each of them (ajv validates them upon creation).
22 * Passes `schemas` option to ajv, so, schemas may $ref each other and it'll be fine.
23 *
24 * @experimental
25 */
26function readAjvSchemas(patterns, cfg) {
27 const schemas = readJsonSchemas(patterns);
28 return schemas.map(schema => ajvSchema_1.AjvSchema.create(schema, {
29 schemas,
30 ...cfg,
31 }));
32}
33exports.readAjvSchemas = readAjvSchemas;