Skip to content

Commit e9e86f5

Browse files
authored
Type-check density option before range validation (#4536)
1 parent 2f0bcf0 commit e9e86f5

2 files changed

Lines changed: 15 additions & 1 deletion

File tree

lib/input.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ function _createInputDescriptor (input, inputOptions, containerOptions) {
125125
}
126126
// Density
127127
if (is.defined(inputOptions.density)) {
128-
if (is.inRange(inputOptions.density, 1, 100000)) {
128+
if (is.number(inputOptions.density) && is.inRange(inputOptions.density, 1, 100000)) {
129129
inputDescriptor.density = inputOptions.density;
130130
} else {
131131
throw is.invalidParameterError('density', 'number between 1 and 100000', inputOptions.density);

test/unit/io.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -901,6 +901,20 @@ suite('Input/output', () => {
901901
/Expected number between 1 and 100000 for density but received zoinks of type string/
902902
);
903903
});
904+
test('Invalid density: numeric string', (t) => {
905+
t.plan(1);
906+
t.assert.throws(
907+
() => sharp({ density: '50' }),
908+
/Expected number between 1 and 100000 for density but received 50 of type string/
909+
);
910+
});
911+
test('Invalid density: array', (t) => {
912+
t.plan(1);
913+
t.assert.throws(
914+
() => sharp({ density: [50] }),
915+
/Expected number between 1 and 100000 for density but received 50 of type object/
916+
);
917+
});
904918
test('Invalid ignoreIcc: string', (t) => {
905919
t.plan(1);
906920
t.assert.throws(

0 commit comments

Comments
 (0)