-
Notifications
You must be signed in to change notification settings - Fork 4
/
validator.js
106 lines (90 loc) · 3.81 KB
/
validator.js
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/**
* The Validation entry point.
* Determines which version of the schema is being validated and chooses an implementation accordingly
*/
'use strict';
//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------
const fs = require('fs');
const path = require('path');
const _ = require('lodash');
const ValidatorError = require('./validator-error');
class Validator {
//------------------------------------------------------------------------------
// Public Interface
//------------------------------------------------------------------------------
/**
* Validates a model of the deserialized YAML
*
* @param objectModel Deserialized YAML
* @throws An error containing the details of the validation failure
*/
static validate(objectModel, outputFormat, yaml, opts) {
const version = _.get(objectModel, 'version');
return Validator._getValidator(version).validate(objectModel, outputFormat, yaml, opts);
}
static validateWithContext(objectModel, outputFormat, yaml, context, opts) {
const version = _.get(objectModel, 'version');
return Validator._getValidator(version).validateWithContext(objectModel, outputFormat, yaml, context, opts);
}
static getJsonSchemas(version) {
return Validator._getValidator(version).getJsonSchemas();
}
static getRootJoiSchema(version) {
return Validator._getValidator(version).getRootJoiSchema();
}
static getStepsJoiSchemas(version) {
return Validator._getValidator(version).getStepsJoiSchemas();
}
static generateJSONPaths(version, { fieldType, joiSchema, convertToCamelCase }) {
return Validator._getValidator(version).generateJSONPaths({ fieldType, joiSchema, convertToCamelCase });
}
/**
* Get a regex for Codefresh variable such as: '${{VARIABLE_NAME}}'
*
* @param {string} version validator's version
* @param {object} opts options
* @param {boolean} [opts.isExact] return a regex which matches not only part of the string but mathes the whole string entirely
*/
static getVariableRegex(version, opts) {
return Validator._getValidator(version).getVariableRegex(opts);
}
static _getValidator(version) {
const defaultVersion = '1.0';
let modelVersion = (version === '1' || version === 1) ? '1.0' : version;
if (!modelVersion) {
modelVersion = defaultVersion;
} else {
modelVersion = modelVersion.toString();
}
const validatorPath = path.join(__dirname, 'schema', modelVersion, 'validator');
if (!fs.existsSync(`${validatorPath}.js`)) {
const message = `Current version: ${modelVersion} is invalid. please change version to 1.0`;
const error = new Error(message);
error.name = 'ValidationError';
error.isJoi = true;
error.details = [
{
message,
type: 'Validation',
context: {
key: 'version',
},
level: 'workflow',
docsLink: 'https://codefresh.io/docs/docs/codefresh-yaml/what-is-the-codefresh-yaml/',
actionItems: `Please change the version to valid one`,
lines: 0,
},
];
throw new ValidatorError(error);
}
const VersionedValidator = require(validatorPath); // eslint-disable-line
if (!VersionedValidator) {
throw new Error(`Unable to find a validator for schema version ${modelVersion}`);
}
return VersionedValidator;
}
}
// Exported objects/methods
module.exports = Validator;