@@ -3,6 +3,19 @@ var { constants } = require('./constants');
33const fs = require ( 'fs' ) ;
44const { getLastCommit } = require ( './git' ) ;
55
6+ const MAX_RESOLUTIONS = 5
7+ const MIN_RESOLUTION_WIDTH = 320
8+ const MIN_RESOLUTION_HEIGHT = 320
9+ const MAX_RESOLUTION_WIDTH = 7680
10+ const MAX_RESOLUTION_HEIGHT = 7680
11+
12+ class ValidationError extends Error {
13+ constructor ( message ) {
14+ super ( message ) ;
15+ this . name = 'ValidationError' ;
16+ }
17+ }
18+
619function validateProjectToken ( options ) {
720 if ( process . env . PROJECT_TOKEN ) {
821 return axios . get ( constants [ options . env ] . AUTH_URL , {
@@ -110,37 +123,13 @@ function validateConfig(configFile) {
110123 process . exit ( 1 ) ;
111124 }
112125
113- // Sanity check browsers
114- if ( storybookConfig . browsers . length == 0 ) {
115- console . log ( '[smartui] Error: Empty browsers list in config.' ) ;
116- process . exit ( 0 ) ;
117- }
118- if ( storybookConfig . browsers . length > constants . VALID_BROWSERS . length ) {
119- console . log ( '[smartui] Error: Invalid or duplicate browsers in config.' ) ;
120- process . exit ( 0 ) ;
121- }
122- storybookConfig . browsers . forEach ( element => {
123- if ( ! ( constants . VALID_BROWSERS . includes ( element . toLowerCase ( ) ) ) ) {
124- console . log ( `[smartui] Error: Invalid value for browser. Accepted browsers are ${ constants . VALID_BROWSERS . join ( ',' ) } ` ) ;
125- process . exit ( 0 ) ;
126- }
127- } ) ;
128-
129- // Sanity check resolutions
130- if ( storybookConfig . resolutions . length == 0 ) {
131- console . log ( '[smartui] Error: Invalid number of resolutions. Min. required - 1' ) ;
132- process . exit ( 0 ) ;
133- }
134- if ( storybookConfig . resolutions . length > 5 ) {
135- console . log ( '[smartui] Error: Invalid number of resolutions. Max allowed - 5' ) ;
126+ try {
127+ validateConfigBrowsers ( storybookConfig . browsers ) ;
128+ storybookConfig . resolutions = validateConfigResolutions ( storybookConfig . resolutions ) ;
129+ } catch ( error ) {
130+ console . log ( `[smartui] Error: Invalid config, ${ error . message } ` ) ;
136131 process . exit ( 0 ) ;
137132 }
138- storybookConfig . resolutions . forEach ( element => {
139- if ( element . length != 2 || element [ 0 ] <= 0 || element [ 1 ] <= 0 ) {
140- console . log ( '[smartui] Error: Invalid resolutions.' )
141- process . exit ( 0 ) ;
142- }
143- } ) ;
144133
145134 // Sanity check waitForTimeout
146135 if ( ! Object . hasOwn ( storybookConfig , 'waitForTimeout' ) ) {
@@ -154,4 +143,55 @@ function validateConfig(configFile) {
154143 return storybookConfig
155144}
156145
157- module . exports = { validateProjectToken, validateStorybookUrl, validateStorybookDir, validateLatestBuild, validateConfig } ;
146+ function validateConfigBrowsers ( browsers ) {
147+ if ( browsers . length == 0 ) {
148+ throw new ValidationError ( 'empty browsers list.' ) ;
149+ }
150+ const set = new Set ( ) ;
151+ for ( let element of browsers ) {
152+ if ( ! constants . VALID_BROWSERS . includes ( element . toLowerCase ( ) ) || set . has ( element ) ) {
153+ throw new ValidationError ( `invalid or duplicate value for browser. Accepted browsers are ${ constants . VALID_BROWSERS . join ( ',' ) } ` ) ;
154+ }
155+ set . add ( element ) ;
156+ } ;
157+ }
158+
159+ function validateConfigResolutions ( resolutions ) {
160+ if ( ! Array . isArray ( resolutions ) ) {
161+ throw new ValidationError ( 'invalid resolutions.' ) ;
162+ }
163+ if ( resolutions . length == 0 ) {
164+ throw new ValidationError ( 'empty resolutions list in config.' ) ;
165+ }
166+ if ( resolutions . length > 5 ) {
167+ throw new ValidationError ( `max resolutions: ${ MAX_RESOLUTIONS } ` ) ;
168+ }
169+ let res = [ ] ;
170+ resolutions . forEach ( element => {
171+ if ( ! Array . isArray ( element ) || element . length == 0 || element . length > 2 ) {
172+ throw new ValidationError ( 'invalid resolutions.' ) ;
173+ }
174+ let width = element [ 0 ] ;
175+ let height = element [ 1 ] ;
176+ if ( typeof width != 'number' || width < MIN_RESOLUTION_WIDTH || width > MAX_RESOLUTION_WIDTH ) {
177+ throw new ValidationError ( `width must be > ${ MIN_RESOLUTION_WIDTH } , < ${ MAX_RESOLUTION_WIDTH } ` ) ;
178+ }
179+ if ( height && ( typeof height != 'number' || height < MIN_RESOLUTION_WIDTH || height > MAX_RESOLUTION_WIDTH ) ) {
180+ throw new ValidationError ( `height must be > ${ MIN_RESOLUTION_HEIGHT } , < ${ MAX_RESOLUTION_HEIGHT } ` ) ;
181+ }
182+ res . push ( [ width , height || 0 ] ) ;
183+ } ) ;
184+
185+ return res
186+ }
187+
188+ module . exports = {
189+ ValidationError,
190+ validateProjectToken,
191+ validateStorybookUrl,
192+ validateStorybookDir,
193+ validateLatestBuild,
194+ validateConfig,
195+ validateConfigBrowsers,
196+ validateConfigResolutions
197+ } ;
0 commit comments