Repository: typestack/class-validator
Pull request #1765 (2022-10-19 08:29:19)

feat: add `@IsBase58` validator
## Description
@IsBase58 validator implemented that checks whether the given string is a valid Base58 encoded string.

## Checklist
- [x] the pull request title describes what this PR does (not a vague title like `Update index.md`)
- [x] the pull request targets the *default* branch of the repository (`develop`)
- [x] the code follows the established code style of the repository
  - `npm run prettier:check` passes
  - `npm run lint:check` passes
- [x] tests are added for the changes I made (if any source code was modified)
- [x] documentation added or updated
- [x] I have run the project locally and verified that there are no errors


braaar: Nice PR! I've added this to my epic #1747 
NoNameProvided: Please rebase for review. 
Kinggodhoon: @NoNameProvided 

I did rebase.

I'm not sure if this is right because I'm a beginner in development.

It would be very helpful if you could let me know if there is a better way.

thank you
Kinggodhoon: @NoNameProvided 
I did rebase.

I'm not sure if this is right because I'm a beginner in development.

It would be very helpful if you could let me know if there is a better way.

thank you.

Patch:
diff --git a/README.md b/README.md
index 24cd128..85ef7b2 100644
--- a/README.md
+++ b/README.md
@@ -836,6 +836,7 @@ isBoolean(value);
 | `@IsDecimal(options?: IsDecimalOptions)`        | Checks if the string is a valid decimal value. Default IsDecimalOptions are `force_decimal=False`, `decimal_digits: '1,'`, `locale: 'en-US'` |
 | `@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. |
 | `@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. |
diff --git a/src/decorator/decorators.ts b/src/decorator/decorators.ts
index 0798325..542c9cb 100644
--- a/src/decorator/decorators.ts
+++ b/src/decorator/decorators.ts
@@ -113,6 +113,7 @@ export * from './string/IsRgbColor';
 export * from './string/IsSemVer';
 export * from './string/IsStrongPassword';
 export * from './string/IsTimeZone';
+export * from './string/IsBase58';
 
 // -------------------------------------------------------------------------
 // Type checkers
diff --git a/src/decorator/string/IsBase58.ts b/src/decorator/string/IsBase58.ts
new file mode 100644
index 0000000..cd474a0
--- /dev/null
+++ b/src/decorator/string/IsBase58.ts
@@ -0,0 +1,30 @@
+import { ValidationOptions } from '../ValidationOptions';
+import { buildMessage, ValidateBy } from '../common/ValidateBy';
+import isBase58Validator from 'validator/lib/isBase58';
+
+export const IS_BASE58 = 'isBase58';
+
+/**
+ * Checks if a string is base58 encoded.
+ * If given value is not a string, then it returns false.
+ */
+export function isBase58(value: unknown): boolean {
+  return typeof value === 'string' && isBase58Validator(value);
+}
+
+/**
+ * Checks if a string is base58 encoded.
+ * If given value is not a string, then it returns false.
+ */
+export function IsBase58(validationOptions?: ValidationOptions): PropertyDecorator {
+  return ValidateBy(
+    {
+      name: IS_BASE58,
+      validator: {
+        validate: (value, args): boolean => isBase58(value),
+        defaultMessage: buildMessage(eachPrefix => eachPrefix + '$property must be base58 encoded', validationOptions),
+      },
+    },
+    validationOptions
+  );
+}


Test patch:
diff --git a/test/functional/validation-functions-and-decorators.spec.ts b/test/functional/validation-functions-and-decorators.spec.ts
index 14b92b3..c2826dd 100644
--- a/test/functional/validation-functions-and-decorators.spec.ts
+++ b/test/functional/validation-functions-and-decorators.spec.ts
@@ -188,6 +188,8 @@ import {
   isStrongPassword,
   IsStrongPasswordOptions,
   IsTimeZone,
+  IsBase58,
+  isBase58,
 } from '../../src/decorator/decorators';
 import { Validator } from '../../src/validation/Validator';
 import { ValidatorOptions } from '../../src/validation/ValidatorOptions';
@@ -4660,3 +4662,36 @@ describe('IsStrongPassword with options', () => {
     return checkReturnedError(new MyClass(), invalidValues, validationType, message);
   });
 });
+
+describe('IsBase58', () => {
+  const constraint = '';
+  const validValues = ['4Fcj4ooZqEQiyH68xykKJFnwZbePBCxgTjwQVtce1VyS'];
+  const invalidValues = [null, undefined, 'my*name-isKinggodHoon'];
+
+  class MyClass {
+    @IsBase58()
+    someProperty: string;
+  }
+
+  it('should not fail if validator.validate said that its valid', () => {
+    return checkValidValues(new MyClass(), validValues);
+  });
+
+  it('should fail if validator.validate said that its invalid', () => {
+    return checkInvalidValues(new MyClass(), invalidValues);
+  });
+
+  it('should not fail if method in validator said that its valid', () => {
+    validValues.forEach(value => expect(isBase58(value)).toBeTruthy());
+  });
+
+  it('should fail if method in validator said that its invalid', () => {
+    invalidValues.forEach(value => expect(isBase58(value)).toBeFalsy());
+  });
+
+  it('should return error object with proper data', () => {
+    const validationType = 'isBase58';
+    const message = 'someProperty must be base58 encoded';
+    return checkReturnedError(new MyClass(), invalidValues, validationType, message);
+  });
+});