Skip to content

Commit 16b26d9

Browse files
committed
update did-decorator
1 parent ced6d94 commit 16b26d9

File tree

3 files changed

+94
-47
lines changed

3 files changed

+94
-47
lines changed

Diff for: src/did/dto/create-did.dto.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,7 @@ export class CreateDidDto {
104104
message: "namespace must be one of the following values: 'testnet', '' ",
105105
})
106106
namespace: string;
107-
@IsOptional()
108-
@IsString()
109-
@MinLength(32)
110-
@MaxLength(48)
111-
@IsMethodSpecificId()
107+
112108
@ApiProperty({
113109
name: 'methodSpecificId',
114110
description: 'MethodSpecificId to be added in did',
@@ -117,6 +113,11 @@ export class CreateDidDto {
117113
minLength: 32,
118114
maxLength: 48,
119115
})
116+
@IsOptional()
117+
@IsString()
118+
@MinLength(32)
119+
@MaxLength(48)
120+
@IsMethodSpecificId()
120121
methodSpecificId?: string;
121122

122123
@ApiProperty({

Diff for: src/utils/customDecorator/did.decorator.ts

+88-41
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ import {
33
SetMetadata,
44
BadRequestException,
55
} from '@nestjs/common';
6+
import {
7+
registerDecorator,
8+
ValidationOptions,
9+
ValidatorConstraint,
10+
ValidatorConstraintInterface,
11+
} from 'class-validator';
612

713
export const IsDid = (): PropertyDecorator => {
814
return applyDecorators(
@@ -37,45 +43,86 @@ export const IsDid = (): PropertyDecorator => {
3743
);
3844
};
3945

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+
// }
5359

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+
}

Diff for: src/utils/utils.ts

-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import {
1515
ArgumentsHost,
1616
HttpStatus,
1717
} from '@nestjs/common';
18-
import { Did } from 'hs-ssi-sdk';
1918

2019
export const existDir = (dirPath) => {
2120
if (!dirPath) throw new Error('Directory path undefined');

0 commit comments

Comments
 (0)