1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | var tslib_1 = require("tslib");
|
4 | var common_1 = require("../common");
|
5 | var SchemaDefinition_1 = require("./SchemaDefinition");
|
6 | var defaultValue = common_1.value.defaultValue;
|
7 | var Schema = (function () {
|
8 | function Schema(args) {
|
9 | var _this = this;
|
10 | var required = defaultValue(args.required, true);
|
11 | var strictNullChecks = defaultValue(args.strictNullChecks, true);
|
12 | var ignoreErrors = defaultValue(args.ignoreErrors, true);
|
13 | args = tslib_1.__assign({}, args, { required: required, strictNullChecks: strictNullChecks, ignoreErrors: ignoreErrors });
|
14 | this.basePath = args.basePath ? common_1.fsPath.resolve(args.basePath) : undefined;
|
15 | var toPath = function (path) {
|
16 | path = (path || '').trim();
|
17 | if (path) {
|
18 | path = _this.basePath ? common_1.fsPath.join(_this.basePath, path) : path;
|
19 | path = common_1.fsPath.resolve(path);
|
20 | }
|
21 | return path;
|
22 | };
|
23 | this.files = Array.isArray(args.files) ? args.files : [args.files];
|
24 | this.files = this.files.map(function (path) { return toPath(path); });
|
25 | this.files = this.files.filter(function (path) { return Boolean(path.trim()); });
|
26 | if (this.files.length === 0) {
|
27 | throw new Error("No files were given to the schema.");
|
28 | }
|
29 | this.files.forEach(function (path) {
|
30 | if (!common_1.fs.pathExistsSync(path)) {
|
31 | throw new Error("Failed to load schema. The file '" + path + "' does not exist.");
|
32 | }
|
33 | });
|
34 | this._program = common_1.TJS.getProgramFromFiles(this.files, args, this.basePath);
|
35 | var generator = common_1.TJS.buildGenerator(this._program, args);
|
36 | if (!generator) {
|
37 | throw new Error("Failed to create a build-generator for the schema.");
|
38 | }
|
39 | this._generator = generator;
|
40 | }
|
41 | Schema.compile = function (args) {
|
42 | return new Schema(args);
|
43 | };
|
44 | Schema.prototype.forType = function (type, options) {
|
45 | if (options === void 0) { options = {}; }
|
46 | try {
|
47 | var g = this._generator;
|
48 | var def = Array.isArray(type)
|
49 | ? g.getSchemaForSymbols(type, options.includeReffedDefinitions)
|
50 | : g.getSchemaForSymbol(type, options.includeReffedDefinitions);
|
51 | return new SchemaDefinition_1.SchemaDefinition({ type: type, def: def });
|
52 | }
|
53 | catch (error) {
|
54 | throw new Error("Failed to generate schema for symbol '" + type + "'. " + error.message);
|
55 | }
|
56 | };
|
57 | Schema.validator = function (schema) {
|
58 | return tslib_1.__awaiter(this, void 0, void 0, function () {
|
59 | var ajv, def, _a, func;
|
60 | return tslib_1.__generator(this, function (_b) {
|
61 | switch (_b.label) {
|
62 | case 0:
|
63 | ajv = common_1.AJV();
|
64 | if (!(typeof schema === 'string')) return [3, 2];
|
65 | return [4, common_1.fs.readJson(common_1.fsPath.resolve(schema))];
|
66 | case 1:
|
67 | _a = _b.sent();
|
68 | return [3, 3];
|
69 | case 2:
|
70 | _a = schema;
|
71 | _b.label = 3;
|
72 | case 3:
|
73 | def = _a;
|
74 | func = ajv.compile(def);
|
75 | return [2, function (data) { return func(data); }];
|
76 | }
|
77 | });
|
78 | });
|
79 | };
|
80 | Schema.validate = function (schema, data) {
|
81 | return tslib_1.__awaiter(this, void 0, void 0, function () {
|
82 | return tslib_1.__generator(this, function (_a) {
|
83 | switch (_a.label) {
|
84 | case 0: return [4, Schema.validator(schema)];
|
85 | case 1: return [2, (_a.sent())(data)];
|
86 | }
|
87 | });
|
88 | });
|
89 | };
|
90 | return Schema;
|
91 | }());
|
92 | exports.Schema = Schema;
|
93 |
|
\ | No newline at end of file |