Skip to content

Commit e04819b

Browse files
feat: add @IsBase58 validator
1 parent d6b664a commit e04819b

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
@@ -836,6 +836,7 @@ isBoolean(value);
836836
| `@IsDecimal(options?: IsDecimalOptions)` | Checks if the string is a valid decimal value. Default IsDecimalOptions are `force_decimal=False`, `decimal_digits: '1,'`, `locale: 'en-US'` |
837837
| `@IsAscii()` | Checks if the string contains ASCII chars only. |
838838
| `@IsBase32()` | Checks if a string is base32 encoded. |
839+
| `@IsBase58()` | Checks if a string is base58 encoded. |
839840
| `@IsBase64()` | Checks if a string is base64 encoded. |
840841
| `@IsIBAN()` | Checks if a string is a IBAN (International Bank Account Number). |
841842
| `@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
@@ -113,6 +113,7 @@ export * from './string/IsRgbColor';
113113
export * from './string/IsSemVer';
114114
export * from './string/IsStrongPassword';
115115
export * from './string/IsTimeZone';
116+
export * from './string/IsBase58';
116117

117118
// -------------------------------------------------------------------------
118119
// 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
@@ -188,6 +188,8 @@ import {
188188
isStrongPassword,
189189
IsStrongPasswordOptions,
190190
IsTimeZone,
191+
IsBase58,
192+
isBase58,
191193
} from '../../src/decorator/decorators';
192194
import { Validator } from '../../src/validation/Validator';
193195
import { ValidatorOptions } from '../../src/validation/ValidatorOptions';
@@ -4660,3 +4662,36 @@ describe('IsStrongPassword with options', () => {
46604662
return checkReturnedError(new MyClass(), invalidValues, validationType, message);
46614663
});
46624664
});
4665+
4666+
describe('IsBase58', () => {
4667+
const constraint = '';
4668+
const validValues = ['4Fcj4ooZqEQiyH68xykKJFnwZbePBCxgTjwQVtce1VyS'];
4669+
const invalidValues = [null, undefined, 'my*name-isKinggodHoon'];
4670+
4671+
class MyClass {
4672+
@IsBase58()
4673+
someProperty: string;
4674+
}
4675+
4676+
it('should not fail if validator.validate said that its valid', () => {
4677+
return checkValidValues(new MyClass(), validValues);
4678+
});
4679+
4680+
it('should fail if validator.validate said that its invalid', () => {
4681+
return checkInvalidValues(new MyClass(), invalidValues);
4682+
});
4683+
4684+
it('should not fail if method in validator said that its valid', () => {
4685+
validValues.forEach(value => expect(isBase58(value)).toBeTruthy());
4686+
});
4687+
4688+
it('should fail if method in validator said that its invalid', () => {
4689+
invalidValues.forEach(value => expect(isBase58(value)).toBeFalsy());
4690+
});
4691+
4692+
it('should return error object with proper data', () => {
4693+
const validationType = 'isBase58';
4694+
const message = 'someProperty must be base58 encoded';
4695+
return checkReturnedError(new MyClass(), invalidValues, validationType, message);
4696+
});
4697+
});

0 commit comments

Comments
 (0)