-
Notifications
You must be signed in to change notification settings - Fork 127
/
Copy pathvalidate.js
83 lines (70 loc) · 2.67 KB
/
validate.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
'use strict';
const BbPromise = require('bluebird');
const _ = require('lodash');
module.exports = {
validate() {
return BbPromise.bind(this)
.then(this.validateServicePath)
.then(this.validateServiceName)
.then(this.validateHandlers);
},
validateServicePath() {
if (!this.serverless.config.servicePath) {
throw new Error('This command can only be run inside a service directory');
}
return BbPromise.resolve();
},
validateServiceName() {
const name = this.serverless.service.service;
// should not contain 'goog'
if (name.match(/goog/)) {
throw new Error('Your service should not contain the string "goog"');
}
if (name.match(/_+/)) {
throw new Error('Your service name should not include underscores');
}
return BbPromise.resolve();
},
validateHandlers() {
const functions = this.serverless.service.functions;
_.forEach(functions, (funcVal, funcKey) => {
if (_.includes(funcVal.handler, '/') || _.includes(funcVal.handler, '.')) {
const errorMessage = [
`The "handler" property for the function "${funcKey}" is invalid.`,
' Handlers should be plain strings referencing only the exported function name',
' without characters such as "." or "/" (so e.g. "http" instead of "index.http").',
' Do you want to nest your functions code in a subdirectory?',
' Google solves this by utilizing the "main" config in the projects package.json file.',
' Please check the docs for more info.',
].join('');
throw new Error(errorMessage);
}
});
},
validateEventsProperty(funcObject, functionName, supportedEvents = ['http', 'event']) {
if (!funcObject.events || funcObject.events.length === 0) {
const errorMessage = [
`Missing "events" property for function "${functionName}".`,
' Your function needs at least one "event".',
' Please check the docs for more info.',
].join('');
throw new Error(errorMessage);
}
if (funcObject.events.length > 1) {
const errorMessage = [
`The function "${functionName}" has more than one event.`,
' Only one event per function is supported.',
' Please check the docs for more info.',
].join('');
throw new Error(errorMessage);
}
const eventType = Object.keys(funcObject.events[0])[0];
if (supportedEvents.indexOf(eventType) === -1) {
const errorMessage = [
`Event type "${eventType}" of function "${functionName}" not supported.`,
` supported event types are: ${supportedEvents.join(', ')}`,
].join('');
throw new Error(errorMessage);
}
},
};