Skip to content

Commit 9b2146d

Browse files
committed
feat: Implement @IsBase58 validator
1 parent 2ef8ff0 commit 9b2146d

File tree

4 files changed

+67
-0
lines changed

4 files changed

+67
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -829,6 +829,7 @@ isBoolean(value);
829829
| `@IsDecimal(options?: IsDecimalOptions)` | Checks if the string is a valid decimal value. Default IsDecimalOptions are `force_decimal=False`, `decimal_digits: '1,'`, `locale: 'en-US'` |
830830
| `@IsAscii()` | Checks if the string contains ASCII chars only. |
831831
| `@IsBase32()` | Checks if a string is base32 encoded. |
832+
| `@IsBase58()` | Checks if a string is base58 encoded. |
832833
| `@IsBase64()` | Checks if a string is base64 encoded. |
833834
| `@IsIBAN()` | Checks if a string is a IBAN (International Bank Account Number). |
834835
| `@IsBIC()` | Checks if a string is a BIC (Bank Identification Code) or SWIFT code. |

src/decorator/decorators.ts

+1
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ export * from './string/IsPostalCode';
111111
export * from './string/IsRFC3339';
112112
export * from './string/IsRgbColor';
113113
export * from './string/IsSemVer';
114+
export * from './string/IsBase58';
114115

115116
// -------------------------------------------------------------------------
116117
// Type checkers

src/decorator/string/IsBase58.ts

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { ValidationOptions } from '../ValidationOptions';
2+
import { buildMessage, ValidateBy } from '../common/ValidateBy';
3+
import isBase58Validator from 'validator/lib/isBase58';
4+
5+
export const IS_BASE58 = 'isBase58';
6+
7+
/**
8+
* Checks if a string is base58 encoded.
9+
* If given value is not a string, then it returns false.
10+
*/
11+
export function isBase58(value: unknown): boolean {
12+
return typeof value === 'string' && isBase58Validator(value);
13+
}
14+
15+
/**
16+
* Checks if a string is base58 encoded.
17+
* If given value is not a string, then it returns false.
18+
*/
19+
export function IsBase58(validationOptions?: ValidationOptions): PropertyDecorator {
20+
return ValidateBy(
21+
{
22+
name: IS_BASE58,
23+
validator: {
24+
validate: (value, args): boolean => isBase58(value),
25+
defaultMessage: buildMessage(eachPrefix => eachPrefix + '$property must be base58 encoded', validationOptions),
26+
},
27+
},
28+
validationOptions
29+
);
30+
}

test/functional/validation-functions-and-decorators.spec.ts

+35
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,8 @@ import {
184184
isPostalCode,
185185
IsSemVer,
186186
isSemVer,
187+
IsBase58,
188+
isBase58,
187189
} from '../../src/decorator/decorators';
188190
import { Validator } from '../../src/validation/Validator';
189191
import { ValidatorOptions } from '../../src/validation/ValidatorOptions';
@@ -4486,3 +4488,36 @@ describe('isInstance', () => {
44864488
return checkReturnedError(new MyClass(), invalidValues, validationType, message);
44874489
});
44884490
});
4491+
4492+
describe('IsBase58', () => {
4493+
const constraint = '';
4494+
const validValues = ['4Fcj4ooZqEQiyH68xykKJFnwZbePBCxgTjwQVtce1VyS'];
4495+
const invalidValues = [null, undefined, 'my*name-isKinggodHoon'];
4496+
4497+
class MyClass {
4498+
@IsBase58()
4499+
someProperty: string;
4500+
}
4501+
4502+
it('should not fail if validator.validate said that its valid', () => {
4503+
return checkValidValues(new MyClass(), validValues);
4504+
});
4505+
4506+
it('should fail if validator.validate said that its invalid', () => {
4507+
return checkInvalidValues(new MyClass(), invalidValues);
4508+
});
4509+
4510+
it('should not fail if method in validator said that its valid', () => {
4511+
validValues.forEach(value => expect(isBase58(value)).toBeTruthy());
4512+
});
4513+
4514+
it('should fail if method in validator said that its invalid', () => {
4515+
invalidValues.forEach(value => expect(isBase58(value)).toBeFalsy());
4516+
});
4517+
4518+
it('should return error object with proper data', () => {
4519+
const validationType = 'isBase58';
4520+
const message = 'someProperty must be base58 encoded';
4521+
return checkReturnedError(new MyClass(), invalidValues, validationType, message);
4522+
});
4523+
});

0 commit comments

Comments
 (0)