mirror of
https://github.com/HamzaSha1/zod-backend.git
synced 2026-03-10 20:41:46 +00:00
66 lines
1.7 KiB
TypeScript
66 lines
1.7 KiB
TypeScript
import {
|
|
registerDecorator,
|
|
ValidationOptions,
|
|
ValidatorConstraint,
|
|
ValidatorConstraintInterface,
|
|
ValidationArguments,
|
|
} from 'class-validator';
|
|
import { PoiType } from '../enums';
|
|
|
|
@ValidatorConstraint({ name: 'IsValidPoiNumber', async: false })
|
|
export class IsValidPoiNumberConstraint implements ValidatorConstraintInterface {
|
|
validate(poiNumber: string, args: ValidationArguments) {
|
|
const object = args.object as any;
|
|
const poiType = object.poiType;
|
|
|
|
if (!poiNumber || !poiType) {
|
|
return false;
|
|
}
|
|
|
|
// Saudi National ID: 10 digits, typically starts with 1 or 2
|
|
const nationalIdPattern = /^[12]\d{9}$/;
|
|
|
|
// Iqama (Resident ID): 10 digits, typically starts with other numbers (not 1 or 2)
|
|
const iqamaPattern = /^[3-9]\d{9}$/;
|
|
|
|
if (poiType === PoiType.NAT) {
|
|
return nationalIdPattern.test(poiNumber);
|
|
}
|
|
|
|
if (poiType === PoiType.IQA) {
|
|
return iqamaPattern.test(poiNumber);
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
defaultMessage(args: ValidationArguments) {
|
|
const object = args.object as any;
|
|
const poiType = object.poiType;
|
|
|
|
if (poiType === PoiType.NAT) {
|
|
return 'National ID must be 10 digits and start with 1 or 2';
|
|
}
|
|
|
|
if (poiType === PoiType.IQA) {
|
|
return 'Iqama number must be 10 digits and start with 3-9';
|
|
}
|
|
|
|
return 'Invalid POI number format';
|
|
}
|
|
}
|
|
|
|
export function IsValidPoiNumber(validationOptions?: ValidationOptions) {
|
|
return function (object: Object, propertyName: string) {
|
|
registerDecorator({
|
|
target: object.constructor,
|
|
propertyName: propertyName,
|
|
options: validationOptions,
|
|
constraints: [],
|
|
validator: IsValidPoiNumberConstraint,
|
|
});
|
|
};
|
|
}
|
|
|
|
|