Skip to content

Commit 3c9c92e

Browse files
authored
Merge pull request #68 from sushobhit-lt/DOT-1974
handle customViewport validations
2 parents c185b3b + 1b3f2d8 commit 3c9c92e

File tree

1 file changed

+48
-3
lines changed

1 file changed

+48
-3
lines changed

commands/utils/validate.js

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ function validateConfig(configFile) {
127127
validateConfigBrowsers(storybookConfig.browsers);
128128
resolutions = storybookConfig.resolutions || storybookConfig.viewports
129129
storybookConfig.resolutions = validateConfigResolutions(resolutions);
130+
storybookConfig.viewports = storybookConfig.resolutions;
131+
validateCustomViewPorts(storybookConfig.customViewports)
130132
} catch (error) {
131133
console.log(`[smartui] Error: Invalid config, ${error.message}`);
132134
process.exit(constants.ERROR_CATCHALL);
@@ -174,10 +176,16 @@ function validateConfigResolutions(resolutions) {
174176
}
175177
let width = element[0];
176178
let height = element[1];
177-
if (typeof width != 'number' || width < MIN_RESOLUTION_WIDTH || width > MAX_RESOLUTION_WIDTH) {
179+
if (typeof width != 'number') {
180+
width = Number(width);
181+
}
182+
if (typeof height != 'number') {
183+
height = Number(height);
184+
}
185+
if (width && width < MIN_RESOLUTION_WIDTH || width > MAX_RESOLUTION_WIDTH) {
178186
throw new ValidationError(`width must be > ${MIN_RESOLUTION_WIDTH}, < ${MAX_RESOLUTION_WIDTH}`);
179187
}
180-
if (height && (typeof height != 'number' || height < MIN_RESOLUTION_WIDTH || height > MAX_RESOLUTION_WIDTH)) {
188+
if (height & ( height < MIN_RESOLUTION_WIDTH || height > MAX_RESOLUTION_WIDTH)) {
181189
throw new ValidationError(`height must be > ${MIN_RESOLUTION_HEIGHT}, < ${MAX_RESOLUTION_HEIGHT}`);
182190
}
183191
res.push([width, height || 0]);
@@ -186,6 +194,42 @@ function validateConfigResolutions(resolutions) {
186194
return res
187195
}
188196

197+
198+
function validateCustomViewPorts(customViewports) {
199+
if (!Array.isArray(customViewports)) {
200+
return
201+
}
202+
if (customViewports && customViewports.length == 0) {
203+
return
204+
}
205+
customViewports.forEach(element => {
206+
if (!Array.isArray(element.stories) || element.stories == 0) {
207+
throw new ValidationError('Missing `stories` in customViewports config. please check the config file');
208+
}
209+
if(!element.styles || !element.styles?.width ){
210+
throw new ValidationError('Missing `styles` in customViewports key. Please check the config file');
211+
}
212+
213+
let width = element.styles.width;
214+
let height = element.styles.height;
215+
if (width && typeof width != 'number') {
216+
width = Number(width);
217+
}
218+
if (height && typeof height != 'number') {
219+
height = Number(height);
220+
}
221+
if (width && width < MIN_RESOLUTION_WIDTH || width > MAX_RESOLUTION_WIDTH) {
222+
throw new ValidationError(`customViewports.styles width must be > ${MIN_RESOLUTION_WIDTH}, < ${MAX_RESOLUTION_WIDTH}`);
223+
}
224+
if (height & ( height < MIN_RESOLUTION_WIDTH || height > MAX_RESOLUTION_WIDTH)) {
225+
throw new ValidationError(`customViewports.styles height must be > ${MIN_RESOLUTION_HEIGHT}, < ${MAX_RESOLUTION_HEIGHT}`);
226+
}
227+
element.styles.width = width;
228+
element.styles.height = height;
229+
});
230+
return
231+
}
232+
189233
module.exports = {
190234
ValidationError,
191235
validateProjectToken,
@@ -194,5 +238,6 @@ module.exports = {
194238
validateLatestBuild,
195239
validateConfig,
196240
validateConfigBrowsers,
197-
validateConfigResolutions
241+
validateConfigResolutions,
242+
validateCustomViewPorts
198243
};

0 commit comments

Comments
 (0)