Skip to content

Commit

Permalink
Merge pull request #118 from DigiChanges/feat/GC/NRS-43/validate-birt…
Browse files Browse the repository at this point in the history
…hday-format

feat: add custom validate to birthday user format
  • Loading branch information
Murzbul authored Oct 28, 2022
2 parents 48d14b7 + 42acc54 commit adbc2b3
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 7 deletions.
2 changes: 1 addition & 1 deletion dev.init.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env bash

yarn sync:db
yarn command addUser --email [email protected] --firstName super --lastName admin --documentType DNI --documentNumber 12345679 --gender male --phone 541112345678 --country AR --address av.1234 --password 12345678 --birthday 05/07/1990 --isSuperAdmin true
yarn command addUser --email [email protected] --firstName super --lastName admin --documentType DNI --documentNumber 12345679 --gender male --phone 541112345678 --country AR --address av.1234 --password 12345678 --birthday 1990-07-05 --isSuperAdmin true
yarn command addUserRole --role Admin --email [email protected] --firstName node --lastName node --password 12345678 --documentType DNI --documentNumber 12345678 --gender male --phone 541112345678 --country AR --address av.1234 --isSuperAdmin false --birthday 04/07/1990
yarn command activeUser --email [email protected]
yarn command activeUser --email [email protected]
Expand Down
44 changes: 44 additions & 0 deletions src/Auth/Presentation/Helpers/CustomUserValidation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { ValidationOptions, ValidatorConstraint, ValidatorConstraintInterface, registerDecorator } from 'class-validator';

export function IsValidBirthday(validationOptions?: ValidationOptions)
{
return (object: any, propertyName: string) =>
{
registerDecorator({
target: object.constructor,
propertyName,
options: validationOptions,
validator: IsValidBirthdayConstraint
});
};
}

@ValidatorConstraint({ name: 'IsValidBirthday' })
class IsValidBirthdayConstraint implements ValidatorConstraintInterface
{
public validate(date: string)
{
return date ? this.dateIsValid(date) : false;
}

private dateIsValid(dateStr: string)
{
const regex = /^\d{4}-\d{2}-\d{2}$/;

if (dateStr.match(regex) === null)
{
return false;
}

const date = new Date(dateStr);

const timestamp = date.getTime();

if (typeof timestamp !== 'number' || Number.isNaN(timestamp))
{
return false;
}

return date.toISOString().startsWith(dateStr);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { decorate } from 'ts-mixer';
import { IsBoolean, IsEmail, IsString, Length } from 'class-validator';
import UserRepPayload from '../../../Domain/Payloads/User/UserRepPayload';
import IRoleDomain from '../../../Domain/Entities/IRoleDomain';
import { IsValidBirthday } from '../../Helpers/CustomUserValidation';

class UserWithoutPermissionsRequest implements UserRepPayload
{
Expand Down Expand Up @@ -50,8 +51,9 @@ class UserWithoutPermissionsRequest implements UserRepPayload
return this._email;
}

@decorate(Length(3, 10))
@decorate(IsString())
@decorate(IsValidBirthday({ message: 'Invalid format' }))

get birthday(): string
{
return this._birthday;
Expand Down
10 changes: 5 additions & 5 deletions src/Auth/Tests/user.handler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ describe('Start User Test', () =>
firstName: 'Jhon',
lastName: 'Doe',
email: '[email protected]',
birthday: '04/08/1991',
birthday: '1991-08-04',
documentType: 'dni',
documentNumber: '35319112',
gender: 'male',
Expand Down Expand Up @@ -83,7 +83,7 @@ describe('Start User Test', () =>
firstName: 'Jhon',
lastName: 'Doe',
email: '[email protected]',
birthday: '04/08/1992',
birthday: '1992-08-04',
documentType: 'dni',
documentNumber: '35319321',
gender: 'male',
Expand Down Expand Up @@ -142,7 +142,7 @@ describe('Start User Test', () =>
firstName: 'Jhon Update',
lastName: 'Doe Update',
email: '[email protected]',
birthday: '04/08/1991',
birthday: '1991-08-04',
documentType: 'dni',
documentNumber: '35319131',
gender: 'female',
Expand Down Expand Up @@ -209,7 +209,7 @@ describe('Start User Test', () =>
email: '[email protected]',
firstName: 'Jhon for delete',
lastName: 'Doe Update',
birthday: '04/08/1992',
birthday: '1992-08-04',
documentType: 'dni',
documentNumber: '25319112',
gender: 'male',
Expand Down Expand Up @@ -385,7 +385,7 @@ describe('Start User Test', () =>
email: '[email protected]',
firstName: 150,
lastName: 'Doe 1',
birthday: '04/07/1990',
birthday: '1990-08-04',
documentType: 'dni',
documentNumber: '35319132',
gender: 'male',
Expand Down

0 comments on commit adbc2b3

Please sign in to comment.