-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathseries.validator.ts
More file actions
43 lines (38 loc) · 1.18 KB
/
series.validator.ts
File metadata and controls
43 lines (38 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import { Address } from '@multiversx/sdk-core';
import {
registerDecorator,
ValidationOptions,
ValidatorConstraint,
ValidatorConstraintInterface,
} from 'class-validator';
@ValidatorConstraint()
export class IsValidSeriesConstraint implements ValidatorConstraintInterface {
validate(series: any): boolean {
try {
return !Address.fromBech32(series).isEmpty();
} catch (error) {
if (this.isValidTokenIdentifier(series)) {
return true;
}
return series === 'factory';
}
}
defaultMessage() {
// here you can provide default error message if validation failed
return 'Invalid series';
}
private isValidTokenIdentifier(tokenID: string): boolean {
return tokenID.split('-').length == 2;
}
}
export function IsValidSeries(validationOptions?: ValidationOptions) {
return (object: object, propertyName: string) => {
registerDecorator({
target: object.constructor,
propertyName: propertyName,
options: validationOptions,
constraints: [],
validator: IsValidSeriesConstraint,
});
};
}