From cbc2708091dcb77a4597d57883595e0bb2e06c67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Attila=20Ol=C3=A1h?= Date: Fri, 9 Dec 2022 21:04:21 +0000 Subject: [PATCH] feat: allow specifying options for `@IsBase64` decorator --- README.md | 2 +- src/decorator/string/IsBase64.ts | 11 ++++++++--- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 458a5e2de4..9f39d39a73 100644 --- a/README.md +++ b/README.md @@ -837,7 +837,7 @@ isBoolean(value); | `@IsAscii()` | Checks if the string contains ASCII chars only. | | `@IsBase32()` | Checks if a string is base32 encoded. | | `@IsBase58()` | Checks if a string is base58 encoded. | -| `@IsBase64()` | Checks if a string is base64 encoded. | +| `@IsBase64(options?: IsBase64Options)` | Checks if a string is base64 encoded. | | `@IsIBAN()` | Checks if a string is a IBAN (International Bank Account Number). | | `@IsBIC()` | Checks if a string is a BIC (Bank Identification Code) or SWIFT code. | | `@IsByteLength(min: number, max?: number)` | Checks if the string's length (in bytes) falls in a range. | diff --git a/src/decorator/string/IsBase64.ts b/src/decorator/string/IsBase64.ts index 75600c6982..44692109ed 100644 --- a/src/decorator/string/IsBase64.ts +++ b/src/decorator/string/IsBase64.ts @@ -1,6 +1,7 @@ import { ValidationOptions } from '../ValidationOptions'; import { buildMessage, ValidateBy } from '../common/ValidateBy'; import isBase64Validator from 'validator/lib/isBase64'; +import type ValidatorJS from 'validator'; export const IS_BASE64 = 'isBase64'; @@ -8,18 +9,22 @@ export const IS_BASE64 = 'isBase64'; * Checks if a string is base64 encoded. * If given value is not a string, then it returns false. */ -export function isBase64(value: unknown): boolean { - return typeof value === 'string' && isBase64Validator(value); +export function isBase64(value: unknown, options?: ValidatorJS.IsBase64Options): boolean { + return typeof value === 'string' && isBase64Validator(value, options); } /** * Checks if a string is base64 encoded. * If given value is not a string, then it returns false. */ -export function IsBase64(validationOptions?: ValidationOptions): PropertyDecorator { +export function IsBase64( + options?: ValidatorJS.IsBase64Options, + validationOptions?: ValidationOptions +): PropertyDecorator { return ValidateBy( { name: IS_BASE64, + constraints: [options], validator: { validate: (value, args): boolean => isBase64(value), defaultMessage: buildMessage(eachPrefix => eachPrefix + '$property must be base64 encoded', validationOptions),