Skip to content

Commit 386d5d7

Browse files
cjihrigtargos
authored andcommitted
lib: support min/max values in validateInteger()
This commit updates validateInteger() in two ways: - Number.isInteger() is used instead of Number.isSafeInteger(). This ensures that all integer values are supported. - Minimum and maximum values are supported. They default to the min and max safe integer values, but can be customized. PR-URL: #28810 Reviewed-By: Tobias Nießen <tniessen@tnie.de> Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Richard Lau <riclau@uk.ibm.com> Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
1 parent e334c1f commit 386d5d7

1 file changed

Lines changed: 11 additions & 6 deletions

File tree

‎lib/internal/validators.js‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const {
1313
isArrayBufferView
1414
}=require('internal/util/types');
1515
const{ signals }=internalBinding('constants').os;
16+
const{MAX_SAFE_INTEGER,MIN_SAFE_INTEGER}=Number;
1617

1718
functionisInt32(value){
1819
returnvalue===(value|0);
@@ -60,12 +61,16 @@ function parseMode(value, name, def) {
6061
thrownewERR_INVALID_ARG_VALUE(name,value,modeDesc);
6162
}
6263

63-
constvalidateInteger=hideStackFrames((value,name)=>{
64-
if(typeofvalue!=='number')
65-
thrownewERR_INVALID_ARG_TYPE(name,'number',value);
66-
if(!Number.isSafeInteger(value))
67-
thrownewERR_OUT_OF_RANGE(name,'an integer',value);
68-
});
64+
constvalidateInteger=hideStackFrames(
65+
(value,name,min=MIN_SAFE_INTEGER,max=MAX_SAFE_INTEGER)=>{
66+
if(typeofvalue!=='number')
67+
thrownewERR_INVALID_ARG_TYPE(name,'number',value);
68+
if(!Number.isInteger(value))
69+
thrownewERR_OUT_OF_RANGE(name,'an integer',value);
70+
if(value<min||value>max)
71+
thrownewERR_OUT_OF_RANGE(name,`>= ${min} && <= ${max}`,value);
72+
}
73+
);
6974

7075
constvalidateInt32=hideStackFrames(
7176
(value,name,min=-2147483648,max=2147483647)=>{

0 commit comments

Comments
 (0)