Files
nest/packages/common/utils/is-uuid.ts
Micael Levi (lab) 82adb3c101 refactor(common,core): use shared utils from common whenever possible
Use every util defined at `shared.utils.ts` from `@nestjs/common`, when
possible. Only spec files aren't touched.
2021-12-16 02:54:06 -04:00

18 lines
654 B
TypeScript

import { BadRequestException } from '../exceptions';
import { isString } from './shared.utils';
const uuid = {
3: /^[0-9A-F]{8}-[0-9A-F]{4}-3[0-9A-F]{3}-[0-9A-F]{4}-[0-9A-F]{12}$/i,
4: /^[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i,
5: /^[0-9A-F]{8}-[0-9A-F]{4}-5[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i,
all: /^[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}$/i,
};
export function isUUID(str: any, version = 'all') {
if (!isString(str)) {
throw new BadRequestException('The value passed as UUID is not a string');
}
const pattern = uuid[version];
return pattern && pattern.test(str);
}