Skip to content

Commit 07d48ad

Browse files
authored
🤖 Merge PR DefinitelyTyped#74023 feat(validator): add overload for isByteLength() by @hkleungai
1 parent 7469026 commit 07d48ad

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

‎types/validator/index.d.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,9 +289,11 @@ declare namespace validator {
289289
/**
290290
* Check if the string's length (in UTF-8 bytes) falls in a range.
291291
*
292-
* @param [options] - Options
292+
* @param [optionsOrMin] - Options, or the minimum byte length allowed.
293+
* @param [max] - The maximum byte length allowed.
293294
*/
294-
export function isByteLength(str: string, options?: IsByteLengthOptions): boolean;
295+
export function isByteLength(str: string, optionsOrMin?: number | IsByteLengthOptions): boolean;
296+
export function isByteLength(str: string, min: number, max: number): boolean;
295297

296298
export interface IsCreditCardOptions {
297299
/**

‎types/validator/validator-tests.ts‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import isBeforeFunc, { IsBeforeOptions } from "validator/lib/isBefore";
2121
import isBICFunc from "validator/lib/isBIC";
2222
import isBooleanFunc from "validator/lib/isBoolean";
2323
import isBtcAddressFunc from "validator/lib/isBtcAddress";
24-
import isByteLengthFunc from "validator/lib/isByteLength";
24+
import isByteLengthFunc, { IsByteLengthOptions } from "validator/lib/isByteLength";
2525
import isCreditCardFunc from "validator/lib/isCreditCard";
2626
import isCurrencyFunc from "validator/lib/isCurrency";
2727
import isDataURIFunc from "validator/lib/isDataURI";
@@ -428,7 +428,7 @@ import isBeforeFuncEs, { IsBeforeOptions as IsBeforeOptionsEs } from "validator/
428428
import isBICFuncEs from "validator/es/lib/isBIC";
429429
import isBooleanFuncEs from "validator/es/lib/isBoolean";
430430
import isBtcAddressFuncEs from "validator/es/lib/isBtcAddress";
431-
import isByteLengthFuncEs from "validator/es/lib/isByteLength";
431+
import isByteLengthFuncEs, { IsByteLengthOptions as IsByteLengthOptionsEs } from "validator/es/lib/isByteLength";
432432
import isCreditCardFuncEs from "validator/es/lib/isCreditCard";
433433
import isCurrencyFuncEs from "validator/es/lib/isCurrency";
434434
import isDataURIFuncEs from "validator/es/lib/isDataURI";
@@ -762,8 +762,17 @@ const any: any = null;
762762

763763
result = validator.isBoolean("sample");
764764

765-
const isByteLengthOptions: validator.IsByteLengthOptions = {};
766-
result = validator.isByteLength("sample", isByteLengthOptions);
765+
result = validator.isByteLength("sample");
766+
result = validator.isByteLength("sample", {});
767+
result = validator.isByteLength("sample", { min: 16, max: 64 } satisfies IsByteLengthOptions);
768+
result = validator.isByteLength("sample", 16);
769+
result = validator.isByteLength("sample", 16, 64);
770+
// Both overloads happen to allow using exactly two argument, [str, number] & [str, object]
771+
// Hence if 2nd arg is of union type `number | object`, typechecking can pass.
772+
result = validator.isByteLength("sample", result ? 16 : { min: 16 });
773+
// @ts-expect-error
774+
// Using 3rd arg here is problematic, without first ensuring that the 2nd argument is a number.
775+
result = validator.isByteLength("sample", result ? 16 : { min: 16 }, 64);
767776

768777
const isCreditCardOptions: validator.IsCreditCardOptions = {};
769778
result = validator.isCreditCard("sample"); // $ExpectType boolean

0 commit comments

Comments
 (0)