Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
feat: allow specifying options for @IsBase64 decorator
  • Loading branch information
NoNameProvided committed Dec 9, 2022
commit cbc2708091dcb77a4597d57883595e0bb2e06c67
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. |
Expand Down
11 changes: 8 additions & 3 deletions src/decorator/string/IsBase64.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,30 @@
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';

/**
* 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),
Expand Down