1
1
'use strict' ;
2
2
3
3
const fs = require ( 'fs' ) ;
4
- const is = require ( 'is_js' ) ;
5
4
const path = require ( 'path' ) ;
6
5
7
6
/**
@@ -12,7 +11,7 @@ const path = require('path');
12
11
function validateClient ( serverless , options ) {
13
12
const validationErrors = [ ] ;
14
13
15
- if ( ! is . object ( options ) ) {
14
+ if ( ! isObject ( options ) ) {
16
15
validationErrors . push ( 'Options must be an object defined under `custom.client`' ) ;
17
16
throw validationErrors ;
18
17
}
@@ -25,27 +24,27 @@ function validateClient(serverless, options) {
25
24
}
26
25
27
26
// bucketName must be a string
28
- if ( ! is . string ( options . bucketName ) ) {
27
+ if ( typeof options . bucketName !== 'string' ) {
29
28
validationErrors . push ( 'Please specify a bucket name for the client in serverless.yml' ) ;
30
29
}
31
30
32
31
// check header options
33
32
if ( options . objectHeaders ) {
34
- if ( ! is . object ( options . objectHeaders ) ) {
33
+ if ( ! isObject ( options . objectHeaders ) ) {
35
34
validationErrors . push ( 'objectHeaders must be an object' ) ;
36
35
}
37
36
38
37
Object . keys ( options . objectHeaders ) . forEach ( p => {
39
- if ( ! is . array ( options . objectHeaders [ p ] ) ) {
38
+ if ( ! Array . isArray ( options . objectHeaders [ p ] ) ) {
40
39
validationErrors . push ( 'Each member of objectHeaders must be an array' ) ;
41
40
}
42
41
43
42
options . objectHeaders [ p ] . forEach ( h => {
44
- if ( ! ( is . existy ( h . name ) && is . string ( h . name ) ) ) {
43
+ if ( typeof h . name !== ' string' ) {
45
44
validationErrors . push ( `Each object header must have a (string) 'name' attribute` ) ;
46
45
}
47
46
48
- if ( ! ( is . existy ( h . value ) && is . string ( h . value ) ) ) {
47
+ if ( typeof h . value !== ' string' ) {
49
48
validationErrors . push ( `Each object header must have a (string) 'value' attribute` ) ;
50
49
}
51
50
} ) ;
@@ -80,48 +79,48 @@ function validateClient(serverless, options) {
80
79
// https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-s3-websiteconfiguration.html
81
80
if ( options . redirectAllRequestsTo ) {
82
81
const clientConfigOptions = Object . keys ( options ) ;
83
- if ( is . inArray ( 'indexDocument' , clientConfigOptions ) ) {
82
+ if ( clientConfigOptions . includes ( 'indexDocument' ) ) {
84
83
validationErrors . push ( 'indexDocument cannot be specified with redirectAllRequestsTo' ) ;
85
84
}
86
85
87
- if ( is . inArray ( 'errorDocument' , clientConfigOptions ) ) {
86
+ if ( clientConfigOptions . includes ( 'errorDocument' ) ) {
88
87
validationErrors . push ( 'errorDocument cannot be specified with redirectAllRequestsTo' ) ;
89
88
}
90
89
91
- if ( is . inArray ( 'routingRules' , clientConfigOptions ) ) {
90
+ if ( clientConfigOptions . includes ( 'routingRules' ) ) {
92
91
validationErrors . push ( 'routingRules cannot be specified with redirectAllRequestsTo' ) ;
93
92
}
94
93
95
- if ( ! is . existy ( options . redirectAllRequestsTo . hostName ) ) {
94
+ if ( ! options . redirectAllRequestsTo . hostName ) {
96
95
validationErrors . push (
97
96
'redirectAllRequestsTo.hostName is required if redirectAllRequestsTo is specified'
98
97
) ;
99
98
}
100
- if ( ! is . string ( options . redirectAllRequestsTo . hostName ) ) {
99
+ if ( typeof options . redirectAllRequestsTo . hostName !== 'string' ) {
101
100
validationErrors . push ( 'redirectAllRequestsTo.hostName must be a string' ) ;
102
101
}
103
102
104
103
if ( options . redirectAllRequestsTo . protocol ) {
105
- if ( ! is . string ( options . redirectAllRequestsTo . protocol ) ) {
104
+ if ( typeof options . redirectAllRequestsTo . protocol !== 'string' ) {
106
105
validationErrors . push ( 'redirectAllRequestsTo.protocol must be a string' ) ;
107
106
}
108
- if ( ! is . inArray ( options . redirectAllRequestsTo . protocol . toLowerCase ( ) , [ 'http' , 'https' ] ) ) {
107
+ if ( ! [ 'http' , 'https' ] . includes ( options . redirectAllRequestsTo . protocol . toLowerCase ( ) ) ) {
109
108
validationErrors . push ( 'redirectAllRequestsTo.protocol must be either http or https' ) ;
110
109
}
111
110
}
112
111
}
113
112
114
113
if ( options . routingRules ) {
115
- if ( ! is . array ( options . routingRules ) ) {
114
+ if ( ! Array . isArray ( options . routingRules ) ) {
116
115
validationErrors . push ( 'routingRules must be a list' ) ;
117
116
}
118
117
119
118
options . routingRules . forEach ( r => {
120
- if ( ! is . existy ( r . redirect ) ) {
119
+ if ( ! r . redirect ) {
121
120
validationErrors . push ( 'redirect must be specified for each member of routingRules' ) ;
122
121
}
123
122
124
- if ( is . existy ( r . redirect . replaceKeyPrefixWith ) && is . existy ( r . redirect . replaceKeyWith ) ) {
123
+ if ( r . redirect . replaceKeyPrefixWith && r . redirect . replaceKeyWith ) {
125
124
validationErrors . push (
126
125
'replaceKeyPrefixWith and replaceKeyWith cannot both be specified for a member of member of routingRules'
127
126
) ;
@@ -137,13 +136,13 @@ function validateClient(serverless, options) {
137
136
redirectProps . forEach ( p => {
138
137
if ( r . redirect [ p ] ) {
139
138
if ( p === 'httpRedirectCode' ) {
140
- if ( ! is . integer ( r . redirect [ p ] ) ) {
139
+ if ( ! Number . isInteger ( r . redirect [ p ] ) ) {
141
140
validationErrors . push (
142
141
'redirect.httpRedirectCode must be an integer for each member of routingRules'
143
142
) ;
144
143
}
145
144
} else {
146
- if ( ! is . string ( r . redirect [ p ] ) ) {
145
+ if ( typeof r . redirect [ p ] !== 'string' ) {
147
146
validationErrors . push (
148
147
`redirect.${ p } must be a string for each member of routingRules`
149
148
) ;
@@ -153,12 +152,7 @@ function validateClient(serverless, options) {
153
152
} ) ;
154
153
155
154
if ( r . condition ) {
156
- if (
157
- ! (
158
- is . existy ( r . condition . httpErrorCodeReturnedEquals ) ||
159
- is . existy ( r . condition . keyPrefixEquals )
160
- )
161
- ) {
155
+ if ( ! r . condition . httpErrorCodeReturnedEquals && ! r . condition . keyPrefixEquals ) {
162
156
validationErrors . push (
163
157
'condition.httpErrorCodeReturnedEquals or condition.keyPrefixEquals must be defined for each member of routingRules'
164
158
) ;
@@ -168,11 +162,11 @@ function validateClient(serverless, options) {
168
162
conditionProps . forEach ( p => {
169
163
if ( r . condition [ p ] ) {
170
164
if ( p === 'httpErrorCodeReturnedEquals' ) {
171
- if ( ! is . integer ( r . condition [ p ] ) ) {
165
+ if ( ! Number . isInteger ( r . condition [ p ] ) ) {
172
166
validationErrors . push ( 'httpErrorCodeReturnedEquals must be an integer' ) ;
173
167
}
174
168
} else {
175
- if ( ! is . string ( r . condition [ p ] ) ) {
169
+ if ( typeof r . condition [ p ] !== 'string' ) {
176
170
validationErrors . push ( `${ p } must be a string` ) ;
177
171
}
178
172
}
@@ -187,4 +181,8 @@ function validateClient(serverless, options) {
187
181
}
188
182
}
189
183
184
+ function isObject ( value ) {
185
+ return typeof value === 'object' && value !== null && ! Array . isArray ( value ) ;
186
+ }
187
+
190
188
module . exports = validateClient ;
0 commit comments