UNPKG

3.06 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.JobValidator = void 0;
4const utils_1 = require("@terascope/utils");
5const config_validators_1 = require("./config-validators");
6const job_schemas_1 = require("./job-schemas");
7const operation_loader_1 = require("./operation-loader");
8const register_apis_1 = require("./register-apis");
9class JobValidator {
10 constructor(context, options = {}) {
11 this.context = context;
12 this.opLoader = new operation_loader_1.OperationLoader({
13 terasliceOpPath: options.terasliceOpPath,
14 assetPath: context.sysconfig.teraslice.assets_directory,
15 });
16 this.schema = job_schemas_1.jobSchema(context);
17 }
18 /** Validate the job configuration, including the Operations and APIs configuration */
19 validateConfig(jobSpec) {
20 // top level job validation occurs, but not operations
21 const jobConfig = config_validators_1.validateJobConfig(this.schema, utils_1.cloneDeep(jobSpec));
22 const assetIds = jobConfig.assets || [];
23 const apis = {};
24 const validateJobFns = [];
25 const handleModule = (opConfig, op) => {
26 const { Schema, API } = op;
27 if (API != null) {
28 apis[opConfig._op] = API;
29 }
30 const schema = new Schema(this.context);
31 validateJobFns.push((job) => {
32 if (!schema.validateJob)
33 return;
34 schema.validateJob(job);
35 });
36 return schema.validate(opConfig);
37 };
38 jobConfig.operations = jobConfig.operations.map((opConfig, index) => {
39 if (index === 0) {
40 return handleModule(opConfig, this.opLoader.loadReader(opConfig._op, assetIds));
41 }
42 return handleModule(opConfig, this.opLoader.loadProcessor(opConfig._op, assetIds));
43 });
44 jobConfig.apis = jobConfig.apis.map((apiConfig) => {
45 const { Schema } = this.opLoader.loadAPI(apiConfig._name, assetIds);
46 const schema = new Schema(this.context, 'api');
47 validateJobFns.push((job) => {
48 if (!schema.validateJob)
49 return;
50 schema.validateJob(job);
51 });
52 return schema.validate(apiConfig);
53 });
54 register_apis_1.registerApis(this.context, jobConfig);
55 validateJobFns.forEach((fn) => { fn(jobConfig); });
56 Object.keys(apis).forEach((name) => {
57 const api = apis[name];
58 this.context.apis.executionContext.addToRegistry(name, api);
59 });
60 return jobConfig;
61 }
62 hasSchema(obj, name) {
63 if (!obj.schema || typeof obj.schema !== 'function') {
64 throw new Error(`${name} needs to have a method named "schema"`);
65 }
66 else if (typeof obj.schema() !== 'object') {
67 throw new Error(`${name} schema needs to return an object`);
68 }
69 }
70}
71exports.JobValidator = JobValidator;
72//# sourceMappingURL=job-validator.js.map
\No newline at end of file