Skip to content

Commit

Permalink
Merge pull request #378 from athombv/fix/image-validation-error
Browse files Browse the repository at this point in the history
fix(validation): improve image validation error message
  • Loading branch information
RobinBol authored Nov 10, 2023
2 parents 78ec9ac + d10133a commit c259415
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 9 deletions.
10 changes: 5 additions & 5 deletions lib/App/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ class App {
if (!driver.images) {
throw new Error(`drivers.${driver.id}: property \`images\` is required in order to publish an app.`);
}
await this._validateImages(driver.images, 'driver');
await this._validateImages(driver.images, 'driver', `drivers.${driver.id}`);
}

if (levelVerified) {
Expand Down Expand Up @@ -560,15 +560,15 @@ class App {
}
}

async _validateImages(imagesObj, type) {
async _validateImages(imagesObj, type, errorPath) {
const sizes = ['small', 'large'];
for (let i = 0; i < sizes.length; i++) {
const size = sizes[i];
const imagePath = imagesObj[size];
const extension = extname(imagePath);

if (typeof IMAGE_MARKERS[extension] === 'undefined') {
throw new Error(`Invalid image extention: ${extension}`);
throw new Error(`Invalid image extension (${extension})${ errorPath ? ` ${errorPath}.${size}` : ''}: ${join(this._path, imagePath)}`);
}

await this._ensureFileExistsCaseSensitive(imagePath);
Expand All @@ -577,14 +577,14 @@ class App {
const imageBytes = await this._readBytes(imagePath, compareBuffer.length);

if (!imageBytes.equals(compareBuffer)) {
throw new Error(`Invalid image: ${imagePath}`);
throw new Error(`Invalid image${ errorPath ? ` ${errorPath}.${size}` : ''}: ${join(this._path, imagePath)}`);
}

const requiredSize = IMAGE_SIZES[type][size];
const imageSize = await imageSizeAsync(join(this._path, imagePath));
if (imageSize.width !== requiredSize.width
|| imageSize.height !== requiredSize.height) {
throw new Error(`Invalid image size (${imageSize.width}x${imageSize.height}): ${imagePath}\nRequired: ${requiredSize.width}x${requiredSize.height}`);
throw new Error(`Invalid image size (${imageSize.width}x${imageSize.height})${ errorPath ? ` ${errorPath}.${size}` : ''}: ${join(this._path, imagePath)}\nRequired: ${requiredSize.width}x${requiredSize.height}`);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions test/validate-base-manifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -329,8 +329,8 @@ describe('HomeyLib.App#validate() base manifest', function() {

await assertValidates(app, {
debug: true, // debug does not validate images
publish: /invalid image extention/i,
verified: /invalid image extention/i,
publish: /invalid image extension/i,
verified: /invalid image extension/i,
});
});

Expand Down
4 changes: 2 additions & 2 deletions test/validate-driver-manifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,8 @@ describe('HomeyLib.App#validate() driver manifest', function() {

await assertValidates(app, {
debug: true, // debug does not validate images
publish: /invalid image extention/i,
verified: /invalid image extention/i,
publish: /invalid image extension/i,
verified: /invalid image extension/i,
});
});

Expand Down

0 comments on commit c259415

Please sign in to comment.