/
index.js
257 lines (224 loc) · 8.43 KB
/
index.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
'use strict';
const Ajv = require('ajv');
const _ = require('lodash');
const schema = require('../../configSchema');
const normalizeAjvErrors = require('./normalizeAjvErrors');
const FUNCTION_NAME_PATTERN = '^[a-zA-Z0-9-_]+$';
const ERROR_PREFIX = 'Configuration error';
const WARNING_PREFIX = 'Configuration warning';
const normalizeSchemaObject = (object, instanceSchema) => {
for (const [key, value] of Object.entries(object)) {
if (!_.isObject(value)) continue;
if (!value.$ref) {
normalizeSchemaObject(value, instanceSchema);
continue;
}
if (!value.$ref.startsWith('#/definitions/')) {
throw new Error(`Unsupported reference ${value.$ref}`);
}
object[key] = _.get(instanceSchema, value.$ref.slice(2).split('/'));
}
};
// Normalizer is introduced to workaround https://github.com/ajv-validator/ajv/issues/1287
// normalizedObjectsMap allows to handle circular structures without issues
const normalizedObjectsSet = new WeakSet();
const normalizeUserConfig = object => {
if (normalizedObjectsSet.has(object)) return object;
normalizedObjectsSet.add(object);
if (Array.isArray(object)) {
for (const value of object) {
if (_.isObject(value)) normalizeUserConfig(value);
}
} else {
for (const [key, value] of Object.entries(object)) {
if (value == null) delete object[key];
else if (_.isObject(value)) normalizeUserConfig(value);
}
}
return object;
};
class ConfigSchemaHandler {
constructor(serverless) {
this.serverless = serverless;
this.schema = _.cloneDeep(schema);
// TODO: Switch back to deepFreeze(this.schema.properties.service) once awsKmsKeyArn property is removed, see https://github.com/serverless/serverless/issues/8261
Object.freeze(this.schema.properties.service.name);
deepFreeze(this.schema.properties.plugins);
deepFreeze(this.schema.properties.package);
Object.freeze(this.schema.properties.layers);
}
validateConfig(userConfig) {
if (this.serverless.service.provider.name === 'aws') {
// TODO: Remove after provider is fully covered, see https://github.com/serverless/serverless/issues/8022
this.serverless.configSchemaHandler.relaxProviderSchema();
}
if (!this.schema.properties.provider.properties.name) {
if (this.serverless.service.configValidationMode !== 'off') {
this.serverless.cli.log(
`${WARNING_PREFIX}: Unrecognized provider '${this.serverless.service.provider.name}'`,
'Serverless',
{ color: 'orange' }
);
this.serverless.cli.log(' ');
this.serverless.cli.log(
"You're relying on provider plugin which doesn't " +
'provide a validation schema for its config.',
'Serverless',
{ color: 'orange' }
);
this.serverless.cli.log(
'Please report the issue at its bug tracker linking: ' +
'https://www.serverless.com/framework/docs/providers/aws/guide/plugins#extending-validation-schema',
'Serverless',
{ color: 'orange' }
);
this.serverless.cli.log(
'You may turn off this message with "configValidationMode: off" setting',
'Serverless',
{ color: 'orange' }
);
this.serverless.cli.log(' ');
}
this.relaxProviderSchema();
}
const ajv = new Ajv({ allErrors: true, coerceTypes: 'array', verbose: true });
require('ajv-keywords')(ajv, 'regexp');
// Workaround https://github.com/ajv-validator/ajv/issues/1255
normalizeSchemaObject(this.schema, this.schema);
const validate = ajv.compile(this.schema);
validate(normalizeUserConfig(userConfig));
if (validate.errors) {
const messages = normalizeAjvErrors(validate.errors).map(err => err.message);
this.handleErrorMessages(messages);
}
}
handleErrorMessages(messages) {
if (messages.length) {
if (this.serverless.service.configValidationMode === 'error') {
const errorMessage =
messages.length > 1
? `${ERROR_PREFIX}: \n ${messages.join('\n ')}`
: `${ERROR_PREFIX} ${messages[0]}`;
throw new this.serverless.classes.Error(errorMessage);
} else {
if (messages.length === 1) {
this.serverless.cli.log(`${WARNING_PREFIX} ${messages[0]}`, 'Serverless', {
color: 'orange',
});
} else {
this.serverless.cli.log(`${WARNING_PREFIX}:`, 'Serverless', {
color: 'orange',
});
for (const message of messages) {
this.serverless.cli.log(` ${message}`, 'Serverless', { color: 'orange' });
}
}
this.serverless.cli.log(' ');
this.serverless.cli.log(
'Learn more about configuration validation here: http://slss.io/configuration-validation',
'Serverless',
{ color: 'orange' }
);
this.serverless.cli.log(' ');
}
}
}
defineTopLevelProperty(name, subSchema) {
this.schema.properties[name] = subSchema;
}
defineProvider(name, options = {}) {
const currentProvider = this.serverless.service.provider.name;
if (currentProvider !== name) {
return;
}
if (options.definitions) {
Object.assign(this.schema.definitions, options.definitions);
}
this.schema.properties.provider.properties.name = { const: name };
if (options.provider) {
addPropertiesToSchema(this.schema.properties.provider, options.provider);
}
if (options.function) {
addPropertiesToSchema(
this.schema.properties.functions.patternProperties[FUNCTION_NAME_PATTERN],
options.function
);
}
if (options.functionEvents) {
for (const functionName of Object.keys(options.functionEvents)) {
this.defineFunctionEvent(name, functionName, options.functionEvents[functionName]);
}
}
if (options.resources) this.schema.properties.resources = options.resources;
// In case provider implementers do not set stage or variableSyntax options,
// then they are set here. The framework internally sets these options in
// Service class.
if (!this.schema.properties.provider.properties.stage) {
addPropertiesToSchema(this.schema.properties.provider, {
properties: { stage: { type: 'string' } },
});
}
if (!this.schema.properties.provider.properties.variableSyntax) {
addPropertiesToSchema(this.schema.properties.provider, {
properties: { variableSyntax: { type: 'string' } },
});
}
}
defineCustomProperties(configSchemaParts) {
addPropertiesToSchema(this.schema.properties.custom, configSchemaParts);
}
defineFunctionEvent(providerName, name, configSchema) {
if (this.serverless.service.provider.name !== providerName) {
return;
}
this.schema.properties.functions.patternProperties[
FUNCTION_NAME_PATTERN
].properties.events.items.anyOf.push({
type: 'object',
properties: { [name]: configSchema },
required: [name],
additionalProperties: false,
});
}
relaxProviderSchema() {
// provider
this.schema.properties.provider.additionalProperties = true;
// functions[]
this.schema.properties.functions.patternProperties[
FUNCTION_NAME_PATTERN
].additionalProperties = true;
// functions[].events[]
if (
Array.isArray(
this.schema.properties.functions.patternProperties[FUNCTION_NAME_PATTERN].properties.events
.items.anyOf
) &&
this.schema.properties.functions.patternProperties[FUNCTION_NAME_PATTERN].properties.events
.items.anyOf.length === 1
) {
this.schema.properties.functions.patternProperties[
FUNCTION_NAME_PATTERN
].properties.events.items = {};
}
}
}
function addPropertiesToSchema(subSchema, extension = { properties: {}, required: [] }) {
subSchema.properties = Object.assign(subSchema.properties, extension.properties);
if (!subSchema.required) subSchema.required = [];
if (Array.isArray(extension.required)) subSchema.required.push(...extension.required);
}
/*
* Deep freezes an object. Stolen from
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze
*/
function deepFreeze(object) {
const propNames = Object.getOwnPropertyNames(object);
for (const name of propNames) {
const value = object[name];
if (value && typeof value === 'object') {
deepFreeze(value);
}
}
return Object.freeze(object);
}
module.exports = ConfigSchemaHandler;