Skip to content

Commit 8d62e03

Browse files
committed
feat: Implement @IsBase58 validator
1 parent ebdca0b commit 8d62e03

File tree

4 files changed

+67
-0
lines changed

4 files changed

+67
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ export * from './string/IsRFC3339';
112112
export * from './string/IsRgbColor';
113113
export * from './string/IsSemVer';
114114
export * from './string/IsTimeZone';
115+
export * from './string/IsBase58';
115116

116117
// -------------------------------------------------------------------------
117118
// Type checkers

src/decorator/string/IsBase58.ts

Lines changed: 30 additions & 0 deletions
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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,8 @@ import {
185185
IsSemVer,
186186
isSemVer,
187187
IsTimeZone,
188+
IsBase58,
189+
isBase58,
188190
} from '../../src/decorator/decorators';
189191
import { Validator } from '../../src/validation/Validator';
190192
import { ValidatorOptions } from '../../src/validation/ValidatorOptions';
@@ -4577,3 +4579,36 @@ describe('isInstance', () => {
45774579
return checkReturnedError(new MyClass(), invalidValues, validationType, message);
45784580
});
45794581
});
4582+
4583+
describe('IsBase58', () => {
4584+
const constraint = '';
4585+
const validValues = ['4Fcj4ooZqEQiyH68xykKJFnwZbePBCxgTjwQVtce1VyS'];
4586+
const invalidValues = [null, undefined, 'my*name-isKinggodHoon'];
4587+
4588+
class MyClass {
4589+
@IsBase58()
4590+
someProperty: string;
4591+
}
4592+
4593+
it('should not fail if validator.validate said that its valid', () => {
4594+
return checkValidValues(new MyClass(), validValues);
4595+
});
4596+
4597+
it('should fail if validator.validate said that its invalid', () => {
4598+
return checkInvalidValues(new MyClass(), invalidValues);
4599+
});
4600+
4601+
it('should not fail if method in validator said that its valid', () => {
4602+
validValues.forEach(value => expect(isBase58(value)).toBeTruthy());
4603+
});
4604+
4605+
it('should fail if method in validator said that its invalid', () => {
4606+
invalidValues.forEach(value => expect(isBase58(value)).toBeFalsy());
4607+
});
4608+
4609+
it('should return error object with proper data', () => {
4610+
const validationType = 'isBase58';
4611+
const message = 'someProperty must be base58 encoded';
4612+
return checkReturnedError(new MyClass(), invalidValues, validationType, message);
4613+
});
4614+
});

0 commit comments

Comments
 (0)