Skip to content

Commit

Permalink
feat: allow specifying options for @IsBase64 decorator (#1845)
Browse files Browse the repository at this point in the history
  • Loading branch information
NoNameProvided authored Dec 9, 2022
1 parent 2b60e0e commit b0d4a23
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
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

1 comment on commit b0d4a23

@uamanager
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like options are not passed to validator.

Please sign in to comment.