-
Notifications
You must be signed in to change notification settings - Fork 1
/
rule.js
172 lines (159 loc) · 4.59 KB
/
rule.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
const PropertyHelper = require('../helpers/property');
const OptionsHelper = require('../helpers/options');
const ValidationError = require('../errors/validation-error');
class Rule {
constructor(options) {
this.options = options || new OptionsHelper();
/** @type {string[] | '*'} */
this.targetModels = [];
/** @type {'*' | {[model: string]: '*' | string[]}} */
this.targetFields = {};
/** @type {string[] | '*'} */
this.targetValidationModes = '*';
/**
* @type {{
* name: string;
* description: string;
* tests: {[key: string]: {
* description?: string;
* message: string;
* sampleValues?: { [messageTemplateArg: string]: string };
* category: string;
* severity: string;
* type: string;
* }}
* }}
*/
this.meta = {
name: 'Rule',
description: 'This is a base rule description that should be overridden.',
tests: {},
};
}
async validate(nodeToTest) {
let errors = [];
if (!this.isValidationModeTargeted(nodeToTest.options.validationMode)) {
return errors;
}
if (this.isModelTargeted(nodeToTest.model)) {
const modelErrors = this.validateModel(nodeToTest);
errors = errors.concat(modelErrors);
}
for (const field in nodeToTest.value) {
if (
Object.prototype.hasOwnProperty.call(nodeToTest.value, field)
&& this.isFieldTargeted(nodeToTest.model, field)
) {
const fieldErrors = await this.validateField(nodeToTest, field);
errors = errors.concat(fieldErrors);
}
}
return errors;
}
/**
* @param {import('../classes/model-node').ModelNodeType} node
* @returns {Promise<import('../errors/validation-error')[]>}
*/
// eslint-disable-next-line no-unused-vars
validateModel(node) {
throw Error('Model validation rule not implemented');
}
/**
* @param {import('../classes/model-node').ModelNodeType} node
* @param {string} field
* @returns {Promise<import('../errors/validation-error')[]>}
*/
// eslint-disable-next-line no-unused-vars
async validateField(node, field) {
throw Error('Field validation rule not implemented');
}
createError(testKey, extra = {}, messageValues = undefined) {
const rule = this.meta.tests[testKey];
let { message } = rule;
if (typeof messageValues !== 'undefined') {
for (const key in messageValues) {
if (Object.prototype.hasOwnProperty.call(messageValues, key)) {
message = message.replace(new RegExp(`{{${key}}}`, 'g'), messageValues[key]);
}
}
}
const error = Object.assign(
extra,
{
rule: this.meta.name,
category: rule.category,
type: rule.type,
severity: rule.severity,
message,
},
);
return new ValidationError(error);
}
isModelTargeted(model) {
return (
this.targetModels === '*'
|| PropertyHelper.stringMatchesField(
this.targetModels,
model.type,
model.version,
)
|| (
this.targetModels instanceof Array
&& PropertyHelper.arrayHasField(
this.targetModels,
model.type,
model.version,
)
)
);
}
/**
* @param {import('../classes/model')} model
* @param {string} field
*/
isFieldTargeted(model, field) {
if (this.targetFields === '*') {
return true;
}
if (typeof this.targetFields === 'object') {
for (const modelType in this.targetFields) {
if (Object.prototype.hasOwnProperty.call(this.targetFields, modelType)) {
if (
PropertyHelper.stringMatchesField(
modelType,
model.type,
model.version,
)
&& (
this.targetFields[modelType] === '*'
|| PropertyHelper.stringMatchesField(
this.targetFields[modelType],
field,
model.version,
)
|| (
this.targetFields[modelType] instanceof Array
&& PropertyHelper.arrayHasField(
this.targetFields[modelType],
field,
model.version,
)
)
)
) {
return true;
}
}
}
}
return false;
}
isValidationModeTargeted(validationMode) {
if (this.targetValidationModes === '*') return true;
if (this.targetValidationModes instanceof Array) {
return this.targetValidationModes.includes(validationMode);
}
return false;
}
}
module.exports = Rule;