@@ -3,6 +3,12 @@ import {
3
3
SetMetadata ,
4
4
BadRequestException ,
5
5
} from '@nestjs/common' ;
6
+ import {
7
+ registerDecorator ,
8
+ ValidationOptions ,
9
+ ValidatorConstraint ,
10
+ ValidatorConstraintInterface ,
11
+ } from 'class-validator' ;
6
12
7
13
export const IsDid = ( ) : PropertyDecorator => {
8
14
return applyDecorators (
@@ -37,45 +43,86 @@ export const IsDid = (): PropertyDecorator => {
37
43
) ;
38
44
} ;
39
45
40
- export const IsMethodSpecificId = ( ) : PropertyDecorator => {
41
- return applyDecorators (
42
- SetMetadata ( 'isMethodSpecificId' , true ) ,
43
- ( target : object , propertyKey : string | symbol ) => {
44
- let original = target [ propertyKey ] ;
45
- const descriptor : PropertyDescriptor = {
46
- get : ( ) => original ,
47
- set : ( val : any ) => {
48
- if ( val . trim ( ) === '' ) {
49
- throw new BadRequestException ( [
50
- `${ propertyKey . toString ( ) } cannot be empty` ,
51
- ] ) ;
52
- }
46
+ // export const IsMethodSpecificId = (): PropertyDecorator => {
47
+ // return applyDecorators(
48
+ // SetMetadata('isMethodSpecificId', true),
49
+ // (target: object, propertyKey: string | symbol) => {
50
+ // let original = target[propertyKey];
51
+ // const descriptor: PropertyDescriptor = {
52
+ // get: () => original,
53
+ // set: (val: any) => {
54
+ // if (val.trim() === '') {
55
+ // throw new BadRequestException([
56
+ // `${propertyKey.toString()} cannot be empty`,
57
+ // ]);
58
+ // }
53
59
54
- const did = val ;
55
- if ( did . includes ( 'did:hid:' ) ) {
56
- throw new BadRequestException ( [
57
- `Invalid ${ propertyKey . toString ( ) } ` ,
58
- ] ) ;
59
- }
60
- if ( did . includes ( 'hid' ) ) {
61
- throw new BadRequestException ( [
62
- `Invalid ${ propertyKey . toString ( ) } ` ,
63
- ] ) ;
64
- }
65
- if ( did . includes ( ':' ) ) {
66
- throw new BadRequestException ( [
67
- `Invalid ${ propertyKey . toString ( ) } ` ,
68
- ] ) ;
69
- }
70
- if ( did . includes ( '.' ) ) {
71
- throw new BadRequestException ( [
72
- `Invalid ${ propertyKey . toString ( ) } ` ,
73
- ] ) ;
74
- }
75
- original = val ;
76
- } ,
77
- } ;
78
- Object . defineProperty ( target , propertyKey , descriptor ) ;
79
- } ,
80
- ) ;
81
- } ;
60
+ // const did = val;
61
+ // if (did.includes('did:hid:')) {
62
+ // throw new BadRequestException([
63
+ // `Invalid ${propertyKey.toString()}`,
64
+ // ]);
65
+ // }
66
+ // if (did.includes('hid')) {
67
+ // throw new BadRequestException([
68
+ // `Invalid ${propertyKey.toString()}`,
69
+ // ]);
70
+ // }
71
+ // if (did.includes(':')) {
72
+ // throw new BadRequestException([
73
+ // `Invalid ${propertyKey.toString()}`,
74
+ // ]);
75
+ // }
76
+ // if (did.includes('.')) {
77
+ // throw new BadRequestException([
78
+ // `Invalid ${propertyKey.toString()}`,
79
+ // ]);
80
+ // }
81
+ // original = val;
82
+ // },
83
+ // };
84
+ // Object.defineProperty(target, propertyKey, descriptor);
85
+ // },
86
+ // );
87
+ // };
88
+
89
+ @ValidatorConstraint ( { async : false } )
90
+ export class IsMethodSpecificIdConstraint
91
+ implements ValidatorConstraintInterface
92
+ {
93
+ validate ( value : any ) : boolean {
94
+ if ( typeof value !== 'string' || value . trim ( ) === '' ) {
95
+ throw new BadRequestException ( 'Value cannot be empty' ) ;
96
+ }
97
+
98
+ const did = value . trim ( ) ;
99
+ if (
100
+ did . includes ( 'did:hid:' ) ||
101
+ did . includes ( 'hid' ) ||
102
+ did . includes ( ':' ) ||
103
+ did . includes ( '.' )
104
+ ) {
105
+ throw new BadRequestException ( 'Invalid method-specific ID' ) ;
106
+ }
107
+
108
+ return true ;
109
+ }
110
+
111
+ defaultMessage ( ) : string {
112
+ return 'Invalid method-specific ID format' ;
113
+ }
114
+ }
115
+
116
+ export function IsMethodSpecificId (
117
+ validationOptions ?: ValidationOptions ,
118
+ ) : PropertyDecorator {
119
+ return function ( object : object , propertyName : string ) {
120
+ registerDecorator ( {
121
+ target : object . constructor ,
122
+ propertyName : propertyName ,
123
+ options : validationOptions ,
124
+ constraints : [ ] ,
125
+ validator : IsMethodSpecificIdConstraint ,
126
+ } ) ;
127
+ } ;
128
+ }
0 commit comments