Skip to content

Commit

Permalink
update did-decorator
Browse files Browse the repository at this point in the history
  • Loading branch information
Pratap2018 committed Jan 3, 2025
1 parent ced6d94 commit 16b26d9
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 47 deletions.
11 changes: 6 additions & 5 deletions src/did/dto/create-did.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,7 @@ export class CreateDidDto {
message: "namespace must be one of the following values: 'testnet', '' ",
})
namespace: string;
@IsOptional()
@IsString()
@MinLength(32)
@MaxLength(48)
@IsMethodSpecificId()

@ApiProperty({
name: 'methodSpecificId',
description: 'MethodSpecificId to be added in did',
Expand All @@ -117,6 +113,11 @@ export class CreateDidDto {
minLength: 32,
maxLength: 48,
})
@IsOptional()
@IsString()
@MinLength(32)
@MaxLength(48)
@IsMethodSpecificId()
methodSpecificId?: string;

@ApiProperty({
Expand Down
129 changes: 88 additions & 41 deletions src/utils/customDecorator/did.decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ import {
SetMetadata,
BadRequestException,
} from '@nestjs/common';
import {
registerDecorator,
ValidationOptions,
ValidatorConstraint,
ValidatorConstraintInterface,
} from 'class-validator';

export const IsDid = (): PropertyDecorator => {
return applyDecorators(
Expand Down Expand Up @@ -37,45 +43,86 @@ export const IsDid = (): PropertyDecorator => {
);
};

export const IsMethodSpecificId = (): PropertyDecorator => {
return applyDecorators(
SetMetadata('isMethodSpecificId', true),
(target: object, propertyKey: string | symbol) => {
let original = target[propertyKey];
const descriptor: PropertyDescriptor = {
get: () => original,
set: (val: any) => {
if (val.trim() === '') {
throw new BadRequestException([
`${propertyKey.toString()} cannot be empty`,
]);
}
// export const IsMethodSpecificId = (): PropertyDecorator => {
// return applyDecorators(
// SetMetadata('isMethodSpecificId', true),
// (target: object, propertyKey: string | symbol) => {
// let original = target[propertyKey];
// const descriptor: PropertyDescriptor = {
// get: () => original,
// set: (val: any) => {
// if (val.trim() === '') {
// throw new BadRequestException([
// `${propertyKey.toString()} cannot be empty`,
// ]);
// }

const did = val;
if (did.includes('did:hid:')) {
throw new BadRequestException([
`Invalid ${propertyKey.toString()}`,
]);
}
if (did.includes('hid')) {
throw new BadRequestException([
`Invalid ${propertyKey.toString()}`,
]);
}
if (did.includes(':')) {
throw new BadRequestException([
`Invalid ${propertyKey.toString()}`,
]);
}
if (did.includes('.')) {
throw new BadRequestException([
`Invalid ${propertyKey.toString()}`,
]);
}
original = val;
},
};
Object.defineProperty(target, propertyKey, descriptor);
},
);
};
// const did = val;
// if (did.includes('did:hid:')) {
// throw new BadRequestException([
// `Invalid ${propertyKey.toString()}`,
// ]);
// }
// if (did.includes('hid')) {
// throw new BadRequestException([
// `Invalid ${propertyKey.toString()}`,
// ]);
// }
// if (did.includes(':')) {
// throw new BadRequestException([
// `Invalid ${propertyKey.toString()}`,
// ]);
// }
// if (did.includes('.')) {
// throw new BadRequestException([
// `Invalid ${propertyKey.toString()}`,
// ]);
// }
// original = val;
// },
// };
// Object.defineProperty(target, propertyKey, descriptor);
// },
// );
// };

@ValidatorConstraint({ async: false })
export class IsMethodSpecificIdConstraint
implements ValidatorConstraintInterface
{
validate(value: any): boolean {
if (typeof value !== 'string' || value.trim() === '') {
throw new BadRequestException('Value cannot be empty');
}

const did = value.trim();
if (
did.includes('did:hid:') ||
did.includes('hid') ||
did.includes(':') ||
did.includes('.')
) {
throw new BadRequestException('Invalid method-specific ID');
}

return true;
}

defaultMessage(): string {
return 'Invalid method-specific ID format';
}
}

export function IsMethodSpecificId(
validationOptions?: ValidationOptions,
): PropertyDecorator {
return function (object: object, propertyName: string) {
registerDecorator({
target: object.constructor,
propertyName: propertyName,
options: validationOptions,
constraints: [],
validator: IsMethodSpecificIdConstraint,
});
};
}
1 change: 0 additions & 1 deletion src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {
ArgumentsHost,
HttpStatus,
} from '@nestjs/common';
import { Did } from 'hs-ssi-sdk';

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

0 comments on commit 16b26d9

Please sign in to comment.