/
config-validators.ts
76 lines (64 loc) · 2.57 KB
/
config-validators.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import convict from 'convict';
import { ValidatedJobConfig, OpConfig, APIConfig } from './interfaces';
import { opSchema, apiSchema } from './job-schemas';
const validateOptions: convict.ValidateOptions = {
allowed: 'warn',
};
/**
* Merges the provided inputSchema with commonSchema and then validates the
* provided opConfig against the resulting schema.
*/
export function validateOpConfig<T>(
inputSchema: convict.Schema<any>, inputConfig: Record<string, any>
): OpConfig & T {
const schema = Object.assign({}, opSchema, inputSchema) as convict.Schema<OpConfig & T>;
const config = convict(schema);
try {
config.load(inputConfig);
config.validate(validateOptions);
} catch (err) {
throw new Error(`Validation failed for operation config: ${inputConfig._op} - ${err.message}`);
}
return config.getProperties();
}
/**
* Merges the provided inputSchema with commonSchema and then validates the
* provided apiConfig against the resulting schema.
*/
export function validateAPIConfig<T>(
inputSchema: convict.Schema<any>, inputConfig: Record<string, any>
): APIConfig & T {
const schema = Object.assign({}, apiSchema, inputSchema) as convict.Schema<APIConfig & T>;
const config = convict(schema);
try {
config.load(inputConfig);
config.validate(validateOptions);
} catch (err) {
throw new Error(`Validation failed for api config: ${inputConfig._name} - ${err.message}`);
}
return config.getProperties();
}
/**
* Merges the provided inputSchema with commonSchema and then validates the
* provided jobConfig against the resulting schema.
*/
export function validateJobConfig<T>(
inputSchema: convict.Schema<any>, inputConfig: Record<string, any>
): ValidatedJobConfig & T {
const config = convict(inputSchema as convict.Schema<ValidatedJobConfig & T>);
try {
config.load(inputConfig);
config.validate(validateOptions);
} catch (err) {
throw new Error(`Validation failed for job config: ${inputConfig.name} - ${err.message}`);
}
const jobProperties = config.getProperties();
if ((jobProperties.cpu && jobProperties.resources_limits_cpu)
|| (jobProperties.cpu && jobProperties.resources_requests_cpu)
|| (jobProperties.memory && jobProperties.resources_limits_memory)
|| (jobProperties.memory && jobProperties.resources_requests_memory)
) {
throw new Error(`Validation failed for job config: ${inputConfig.name} - cpu/memory can't be mixed with resource settings of the same type.`);
}
return jobProperties;
}