Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Guardian Register #5

Merged
merged 137 commits into from
Apr 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
137 commits
Select commit Hold shift + click to select a range
3ffafe4
chore: set off @typescript-eslint/no-namespace rule
Mar 26, 2023
ac83ee9
chore: remove ts-jest and setup babel
Mar 26, 2023
643c21b
feat: ensure SignUpController returns 400 if no name is provided
Mar 26, 2023
c32a343
test: ensure SignUpController returns 400 if no name is provided
Mar 26, 2023
e745ea8
feat: ensure SignUpController returns an error if no name is provided
Mar 26, 2023
c433bd6
test: ensure SignUpController returns an error if no name is provided
Mar 26, 2023
f3f3792
test: ensure SignUpController returns an error if no name is provided
Mar 26, 2023
fa7fde1
Merge branch 'feature/user-register' of github.com:PetJournal/petjour…
Mar 26, 2023
eff0071
Merge branch 'feature/user-register' of github.com:PetJournal/petjour…
Mar 26, 2023
25bb142
Merge branch 'feature/user-register' of github.com:PetJournal/petjour…
Mar 26, 2023
01b0176
chore: set off @typescript-eslint/strict-boolean-expressions
Mar 26, 2023
5efe0ea
feat: ensure SignUpController returns 400 if no lastName is provided
Mar 26, 2023
11ede7d
test: ensure SignUpController returns 400 if no lastName is provided
Mar 26, 2023
9f5fe63
feat: ensure SignUpController returns 400 if no email is provided
Mar 26, 2023
df58195
test: ensure SignUpController returns 400 if no email is provided
Mar 26, 2023
1ca1105
refactor: ensure SignUpController.handle implement correct interfaces
Mar 26, 2023
62c72bd
refactor: add customized error for missing param
Mar 26, 2023
aa762c4
refactor: move duplicated code to a helper method
Mar 26, 2023
2c31eba
refactor: simplify code
Mar 26, 2023
ef0b614
feat: ensure SignUpController returns 400 if no password is provided
Mar 26, 2023
a98162d
test: ensure SignUpController returns 400 if no password is provided
Mar 26, 2023
90fd226
feat: ensure SignUpController returns 400 if no password confirmation…
Mar 26, 2023
1ff6afd
test: ensure SignUpController returns 400 if no password confirmation…
Mar 26, 2023
6af8e15
feat: ensure SignUpController returns 400 if isProvicyPolicyAccepted …
Mar 26, 2023
2943747
test: ensure SignUpController returns 400 if isProvicyPolicyAccepted …
Mar 26, 2023
c5a3672
refactor: ensure SignUpController implements correct interface
Mar 26, 2023
96c4098
refactor: move sut creation to a factory helper method
Mar 26, 2023
a70d564
feat: ensure SignUpController returns 400 if an invalid email is prov…
Mar 27, 2023
077f2ad
test: ensure SignUpController returns 400 if an invalid email is prov…
Mar 27, 2023
4376246
test: ensure SignUpController calls EmailValidator with correct email
Mar 29, 2023
688df08
feat: ensure SignUpController returns 500 if EmailValidator throws
Mar 29, 2023
16a4e9f
test: ensure SignUpController returns 500 if EmailValidator throws
Mar 29, 2023
0d66a23
refactor: move server error to a helper method
Mar 29, 2023
7327f65
refactor: move all error imports to a single file
Mar 29, 2023
afaa6fa
refactor: move EmailValidator creation to a factory helper method
Mar 29, 2023
2711e59
refactor: change mocking strategy
Mar 29, 2023
867e607
feat: ensure SignUpController returns 400 if password confirmation fails
Mar 29, 2023
a7bda6d
test: ensure SignUpController returns 400 if password confirmation fails
Mar 29, 2023
754ee36
refactor: remove code duplication
Mar 29, 2023
c5c011e
feat: ensure SignUpController calls AddGuardian with correct values
Mar 29, 2023
4ea26fb
test: ensure SignUpController calls AddGuardian with correct values
Mar 29, 2023
e7956ef
feat: ensure SignUpController returns 500 if EmailValidator throws
Mar 29, 2023
669197e
test: ensure SignUpController return 500 if AddGuardian throws
Mar 29, 2023
8a34f2f
feat: ensure SignUpController returns 200 if valid data is provided
Mar 29, 2023
bc07fa2
test: ensure SignUpController returns 200 if valid data is provided
Mar 29, 2023
6a091f1
refactor: move success http response to a helper method
Mar 29, 2023
01417b8
feat: ensure SignUpController returns 400 if isProvicyPolicyAccepted …
Mar 29, 2023
d4b4ca4
test: ensure SignUpController returns 400 if isProvicyPolicyAccepted …
Mar 29, 2023
e24205c
refactor: transform handle to async
Mar 29, 2023
1622a6e
refactor: change import
Mar 29, 2023
eeb2a65
fix: remove false positive
Mar 29, 2023
4fd085a
chore: update jest scripts
Mar 29, 2023
527c4ae
feat: ensure EmailValidatorAdapter returns false if validator returns…
Mar 29, 2023
4662d78
test: ensure EmailValidatorAdapter returns false if validator returns…
Mar 29, 2023
c2f9994
chore: add tests to rootDirs
Apr 4, 2023
df0d2d4
feat: ensure EmailValidatorAdapter returns true if validator returns …
Apr 4, 2023
0b0a13f
test: ensure EmailValidatorAdapter returns true if validator returns …
Apr 4, 2023
249c9a4
test: ensure EmailValidatorAdapter calls validator with correct email
Apr 4, 2023
46dfc12
refactor: move sut creation to a factory helper method
Apr 4, 2023
ed5d93b
refactor: change file name
Apr 4, 2023
9dd4023
feat: ensure SignUpController returns 400 if an invalid firstName is …
Apr 4, 2023
4d1dda4
test: ensure SignUpController returns 400 if an invalid firstName is …
Apr 4, 2023
a2d74f8
test: ensure SignUpController calls NameValidator with correct firstName
Apr 4, 2023
7d813b7
test: ensure SignUpController returns 500 if NameValidator throws
Apr 4, 2023
1a41783
refactor: ensure SignUpController calls NameValidator with correct va…
Apr 5, 2023
6e96752
refactor: change variable name
Apr 5, 2023
0e60805
feat: ensure SignUpController calls PhoneValidator with correct phone
Apr 5, 2023
6d69fba
test: ensure SignUpController calls PhoneValidator with correct phone
Apr 5, 2023
195ea38
test: ensure SignUpController returns 400 if an invalid phone is prov…
Apr 5, 2023
776aa60
test: ensure SignUpController returns 500 if PhoneValidator throws
Apr 5, 2023
c46a39d
feat: ensure SignUpController calls PasswordValidator with correct pa…
Apr 5, 2023
b6430ac
test: ensure SignUpController calls PasswordValidator with correct pa…
Apr 5, 2023
fd8989a
test: ensure SignUpController returns 500 if PasswordValidator throws
Apr 5, 2023
bbfea6e
feat: ensure NameValidator returns false if validator returns false
Apr 5, 2023
05d3741
test: ensure NameValidator returns false if validator returns false
Apr 5, 2023
25deb10
feat: ensure NameValidator returns true if validator returns true
Apr 5, 2023
4479187
test: ensure NameValidatorAdapter calls validator with correct name
Apr 5, 2023
585b6d5
feat: ensure PasswordValidatorAdapter return false if validator retur…
Apr 5, 2023
4dc1015
test: ensure PasswordValidatorAdapter return false if validator retur…
Apr 5, 2023
9208a25
feat: ensure PasswordValidatorAdapter return true if validator return…
Apr 5, 2023
b2f5b08
test: ensure PasswordValidatorAdapter return true if validator return…
Apr 5, 2023
4edebbd
test: ensure PasswordValidatorAdapter calls validator with correct pa…
Apr 5, 2023
5717dad
refactor: improve semantics
Apr 5, 2023
2c6a404
feat: ensure PhoneValidatorAdapter return false if validator returns …
Apr 5, 2023
1b60583
test: ensure PhoneValidatorAdapter return false if validator returns …
Apr 5, 2023
5b0793a
feat: ensure PhoneValidatorAdapter return true if validator returns true
Apr 5, 2023
3dc1c91
test: ensure PhoneValidatorAdapter return true if validator returns true
Apr 5, 2023
f5ae4ac
test: ensure PhoneValidatorAdapter calls validator with correct phone
Apr 5, 2023
8af3a45
feat: ensure DbAddGuardian calls Encrypter with correct password
Apr 5, 2023
6961761
test: ensure DbAddGuardian calls Encrypter with correct password
Apr 5, 2023
66ac95b
refactor: move sut creation to a factory helper method
Apr 5, 2023
33e4f25
refactor: move encrypter creation to a factory helper method
Apr 5, 2023
ad8cc80
test: ensure DbAddGuardian throws if Encrypter throws
Apr 5, 2023
1932c1c
feat: ensure DbAddGuardian calls AddGuardianRepository with correct v…
Apr 5, 2023
2685e06
test: ensure DbAddGuardian calls AddGuardianRepository with correct v…
Apr 5, 2023
c8e375d
test: ensure DbAddGuardian throws if AddGuardianRepository throws
Apr 5, 2023
ca75622
feat: ensure DbAddGuardian return an guardian on success
Apr 5, 2023
35e95f0
test: ensure DbAddGuardian return an guardian on success
Apr 5, 2023
0771e15
feat: ensure BcryptAdapter calls bcrypt with correct values
Apr 5, 2023
c3bd58b
test: ensure BcryptAdapter calls bcrypt with correct values
Apr 5, 2023
af20412
feat: ensure BcryptAdapter returns a hash on success
Apr 5, 2023
b6ae02c
test: ensure BcryptAdapter returns a hash on success
Apr 5, 2023
ba5a9eb
refactor: move sut creation to a factory helper method
Apr 5, 2023
913b0f6
test: ensure BcryptAdapter throws if bcrypt throws
Apr 5, 2023
93ef474
chore: migrate to yarn pnp
Apr 10, 2023
c3d9411
chore: change yarn nodeLinker to node-modules
Apr 13, 2023
b347b13
refactor: change variable name
Apr 14, 2023
34332eb
feat: ensure GuardianAccountRepository returns an guardian account on…
Apr 15, 2023
55eec6b
test: ensure GuardianAccountRepository returns an guardian account on…
Apr 15, 2023
ebeaa25
test: ensure GuardianAccountRepository returns an error if duplicated…
Apr 15, 2023
2d7709d
chore: add express
Apr 15, 2023
f9c8de4
feat: ensure requests parse body as json
Apr 15, 2023
f9dd11d
test: ensure requests parse body as json
Apr 15, 2023
35a3fb1
feat: ensure cors is enabled
Apr 15, 2023
50b4195
test: ensure cors is enabled
Apr 15, 2023
5f9dce5
feat: ensure requests return default content type as json
Apr 15, 2023
c23f8ed
test: ensure requests return default content type as json
Apr 15, 2023
31fafc4
test: ensure requests return xml content type when forced
Apr 15, 2023
cb9ab92
refactor: group middleware interfaces in a single file
Apr 15, 2023
62ba286
feat: ensure SignUp route returns an guardian account on success
Apr 15, 2023
b19d20b
test: ensure SignUp route returns an guardian account on success
Apr 15, 2023
9530eb1
refactor: turns prisma helper generic
Apr 17, 2023
7cbb3c7
refactor: connect to postgresql using prisma before integration tests
Apr 17, 2023
efbc9b8
refactor: move port to config env file
Apr 17, 2023
1289431
feat: add SignUpController Factory
Apr 17, 2023
6801b05
feat: add ExpressRouteAdapter
Apr 17, 2023
eaee3d6
chore: remove main layer from tests
Apr 17, 2023
880b6ef
fix: fixes AddGuardian interface
Apr 17, 2023
744d92a
docs: add docs schemas
Apr 17, 2023
c701dd2
docs: add docs components
Apr 17, 2023
a657670
docs: add docs paths
Apr 17, 2023
ae72130
docs: add register api docs
Apr 17, 2023
1e3eaa0
feat: add swagger api
Apr 17, 2023
7c33ea7
docs: add signup examples
Apr 17, 2023
bf4f07f
chore: add typescript paths
Apr 19, 2023
7f7d406
refactor: turns tests more understandle
Apr 19, 2023
0bff635
refactor: remove extra whitespace
Apr 19, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
NODE_ENV=development
POSTGRES_USER=admin
POSTGRES_PASSWORD=admin
POSTGRES_DB=database
PG_HOST=localhost
PG_PORT=54320
DATABASE_URL="postgresql://admin:admin@localhost:54320/database?schema=public"
11 changes: 10 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
node_modules
.pnp.*
.yarn/*
!.yarn/patches
!.yarn/plugins
!.yarn/releases
!.yarn/sdks
!.yarn/versions
dist
coverage
node_modules
.env*
!.env.example
5 changes: 5 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,10 @@
"extends": "standard-with-typescript",
"parserOptions": {
"project": "./tsconfig.json"
},
"rules": {
"@typescript-eslint/no-namespace": "off",
"@typescript-eslint/strict-boolean-expressions": "off",
"@typescript-eslint/no-misused-promises": "off"
}
}
11 changes: 10 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
node_modules
.pnp.*
.yarn/*
!.yarn/patches
!.yarn/plugins
!.yarn/releases
!.yarn/sdks
!.yarn/versions
dist
coverage
node_modules
.env*
!.env.example
2 changes: 1 addition & 1 deletion .husky/commit-msg
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

npx --no -- commitlint --edit "$1"
yarn run commitlint --edit "$1"
2 changes: 1 addition & 1 deletion .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

npx lint-staged
yarn run lint-staged
13 changes: 0 additions & 13 deletions .jest.config.js

This file was deleted.

7 changes: 7 additions & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"recommendations": [
"arcanis.vscode-zipfs",
"dbaeumer.vscode-eslint",
"editorconfig.editorconfig"
]
}
9 changes: 9 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"search.exclude": {
"**/.yarn": true,
"**/.pnp.*": true
},
"eslint.nodePath": ".yarn/sdks",
"typescript.tsdk": ".yarn/sdks/typescript/lib",
"typescript.enablePromptUseWorkspaceTsdk": true
}
541 changes: 541 additions & 0 deletions .yarn/plugins/@yarnpkg/plugin-interactive-tools.cjs

Large diffs are not rendered by default.

873 changes: 873 additions & 0 deletions .yarn/releases/yarn-3.5.0.cjs

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions .yarnrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
nodeLinker: node-modules

yarnPath: .yarn/releases/yarn-3.5.0.cjs
16 changes: 16 additions & 0 deletions docker-compose.dev.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
version: '3.8'

services:
db:
image: postgres
container_name: petjournal_db
restart: always
ports:
- 54320:5432
env_file:
- .env.dev
volumes:
- pgdata:/var/lib/postgresql/data

volumes:
pgdata:
22 changes: 22 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
collectCoverageFrom: [
'<rootDir>/src/**/*.ts',
'!<rootDir>/src/main/**',
'!<rootDir>/src/**/index.ts'
],
coverageDirectory: 'coverage',
coverageProvider: 'babel',
moduleNameMapper: {
'@/tests/(.+)': '<rootDir>/tests/$1',
'@/(.+)': '<rootDir>/src/$1'
},
roots: [
'<rootDir>/src',
'<rootDir>/tests'
],
preset: 'ts-jest',
transform: {
'\\.ts$': 'ts-jest'
}
}
40 changes: 32 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
"description": "",
"main": "index.js",
"scripts": {
"start": "node dist/main/server.js",
"dev": "sucrase-node src/main/server.ts",
"build": "rimraf dist && tsc -p tsconfig-build.json",
"lint": "eslint .",
"lint:fix": "yarn run lint -- --fix",
"test": "jest --passWithNoTests --no-cache --runInBand",
"test:watch": "yarn test -- --watch",
"test:staged": "yarn test -- --findRelatedTests",
"test:coverage": "yarn test -- --coverage",
"lint:fix": "yarn run lint --fix",
"test": "jest --passWithNoTests --no-cache --runInBand --silent --noStackTrace",
"test:watch": "yarn test --watch",
"test:verbose": "jest --passWithNoTests --no-cache --runInBand",
"test:staged": "yarn test --findRelatedTests",
"test:coverage": "yarn test:verbose --coverage",
"prepare": "husky install"
},
"keywords": [],
Expand All @@ -19,8 +22,15 @@
"devDependencies": {
"@commitlint/cli": "^17.4.4",
"@commitlint/config-conventional": "^17.4.4",
"@types/bcrypt": "^5.0.0",
"@types/express": "^4.17.17",
"@types/jest": "^29.5.0",
"@types/module-alias": "^2.0.1",
"@types/node": "^18.15.5",
"@types/supertest": "^2.0.12",
"@types/swagger-ui-express": "^4.1.3",
"@types/uuid": "^9.0.1",
"@types/validator": "^13.7.14",
"@typescript-eslint/eslint-plugin": "^5.43.0",
"eslint": "^8.0.1",
"eslint-config-standard-with-typescript": "^34.0.1",
Expand All @@ -30,10 +40,24 @@
"husky": "^8.0.0",
"jest": "^29.5.0",
"lint-staged": "^13.2.0",
"ts-jest": "^29.0.5",
"typescript": "^5.0.2"
"prisma": "^4.12.0",
"sucrase": "^3.32.0",
"supertest": "^6.3.3",
"ts-jest": "^29.1.0",
"typescript": "^5.0.2",
"uuid": "^9.0.0"
},
"dependencies": {
"rimraf": "^4.4.0"
"@prisma/client": "^4.12.0",
"bcrypt": "^5.1.0",
"express": "^4.18.2",
"module-alias": "^2.2.2",
"rimraf": "^4.4.0",
"swagger-ui-express": "^4.6.2",
"validator": "^13.9.0"
},
"packageManager": "[email protected]",
"prisma": {
"schema": "./src/infra/repos/postgresql/prisma/schema.prisma"
}
}
5 changes: 5 additions & 0 deletions src/application/controllers/controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { type HttpRequest, type HttpResponse } from '@/application/helpers/http'

export interface Controller {
handle: (httpRequest: HttpRequest) => Promise<HttpResponse>
}
67 changes: 67 additions & 0 deletions src/application/controllers/signup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { InvalidParamError, MissingParamError } from '@/application/errors'
import { type EmailValidator, type NameValidator, type PasswordValidator, type PhoneValidator } from '@/application/validation/protocols'
import { badRequest, serverError, success, type HttpRequest, type HttpResponse } from '@/application/helpers/http'
import { type Controller } from '@/application/controllers/controller'
import { type AddGuardian } from '@/domain/use-cases/add-guardian'

export class SignUpController implements Controller {
private readonly addGuardian: AddGuardian
private readonly emailValidator: EmailValidator
private readonly nameValidator: NameValidator
private readonly passwordValidator: PasswordValidator
private readonly phoneValidator: PhoneValidator

constructor (addGuardian: AddGuardian, emailValidator: EmailValidator, nameValidator: NameValidator, passwordValidator: PasswordValidator, phoneValidator: PhoneValidator) {
this.emailValidator = emailValidator
this.addGuardian = addGuardian
this.nameValidator = nameValidator
this.passwordValidator = passwordValidator
this.phoneValidator = phoneValidator
}

async handle (httpRequest: HttpRequest): Promise<HttpResponse> {
try {
const requiredFields = ['firstName', 'lastName', 'email', 'password', 'passwordConfirmation', 'isPrivacyPolicyAccepted']
for (const field of requiredFields) {
if (httpRequest.body[field] === undefined) {
return badRequest(new MissingParamError(field))
}
}
const { firstName, lastName, email, phone, password, passwordConfirmation, isPrivacyPolicyAccepted } = httpRequest.body
if (passwordConfirmation !== password) {
return badRequest(new InvalidParamError('passwordConfirmation'))
}
if (!isPrivacyPolicyAccepted) {
return badRequest(new InvalidParamError('isPrivacyPolicyAccepted'))
}
const isValidName = this.nameValidator.isValid(firstName, lastName)
if (!isValidName) {
return badRequest(new InvalidParamError('name'))
}
const isValidEmail = this.emailValidator.isValid(email)
if (!isValidEmail) {
return badRequest(new InvalidParamError('email'))
}
const isValidPassword = this.passwordValidator.isValid(password)
if (!isValidPassword) {
return badRequest(new InvalidParamError('password'))
}
const isValidPhone = this.phoneValidator.isValid(phone)
if (!isValidPhone) {
return badRequest(new InvalidParamError('phone'))
}
const guardian = await this.addGuardian.add({
firstName,
lastName,
email,
phone,
password,
isPrivacyPolicyAccepted
})
return success(guardian)
} catch (error) {
console.error(error)
return serverError()
}
}
}
3 changes: 3 additions & 0 deletions src/application/errors/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './invalid-param-error'
export * from './missing-param-error'
export * from './server-error'
6 changes: 6 additions & 0 deletions src/application/errors/invalid-param-error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export class InvalidParamError extends Error {
constructor (paramName: string) {
super(`Invalid param: ${paramName}`)
this.name = 'InvalidParamError'
}
}
6 changes: 6 additions & 0 deletions src/application/errors/missing-param-error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export class MissingParamError extends Error {
constructor (paramName: string) {
super(`Missing param: ${paramName}`)
this.name = 'MissingParamError'
}
}
6 changes: 6 additions & 0 deletions src/application/errors/server-error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export class ServerError extends Error {
constructor () {
super('Internal server error.\nAn unexpected error happened. Please try again in a moment.')
this.name = 'ServerError'
}
}
25 changes: 25 additions & 0 deletions src/application/helpers/http.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { ServerError } from '@/application/errors'

export interface HttpRequest {
body?: any
}

export interface HttpResponse {
statusCode: number
body: any
}

export const badRequest = (error: Error): HttpResponse => ({
statusCode: 400,
body: error
})

export const serverError = (): HttpResponse => ({
statusCode: 500,
body: new ServerError()
})

export const success = (data: any): HttpResponse => ({
statusCode: 200,
body: data
})
3 changes: 3 additions & 0 deletions src/application/validation/protocols/email-validator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface EmailValidator {
isValid: (email: string) => boolean
}
4 changes: 4 additions & 0 deletions src/application/validation/protocols/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * from './email-validator'
export * from './name-validator'
export * from './password-validator'
export * from './phone-validator'
3 changes: 3 additions & 0 deletions src/application/validation/protocols/name-validator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface NameValidator {
isValid: (firstName: string, lastName: string) => boolean
}
3 changes: 3 additions & 0 deletions src/application/validation/protocols/password-validator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface PasswordValidator {
isValid: (password: string) => boolean
}
3 changes: 3 additions & 0 deletions src/application/validation/protocols/phone-validator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface PhoneValidator {
isValid: (phone: string) => boolean
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { type EmailValidator } from '@/application/validation/protocols/email-validator'
import validator from 'validator'

export class EmailValidatorAdapter implements EmailValidator {
isValid (email: string): boolean {
return validator.isEmail(email)
}
}
4 changes: 4 additions & 0 deletions src/application/validation/validators/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * from './email-validator-adapter'
export * from './name-validator-adapter'
export * from './password-validator-adapter'
export * from './phone-validator-adapter'
14 changes: 14 additions & 0 deletions src/application/validation/validators/name-validator-adapter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { type NameValidator } from '@/application/validation/protocols'
import validator from 'validator'

export class NameValidatorAdapter implements NameValidator {
isValid (firstName: string, lastName: string): boolean {
const isValidName = (field: string): boolean => validator.matches(field, /^[a-zA-Z]{3,}$/)

if (isValidName(firstName) && isValidName(lastName)) {
return true
}

return false
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { type PasswordValidator } from '@/application/validation/protocols'
import validator from 'validator'

export class PasswordValidatorAdapter implements PasswordValidator {
isValid (password: string): boolean {
return validator.isStrongPassword(password)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { type PhoneValidator } from '@/application/validation/protocols'
import validator from 'validator'

export class PhoneValidatorAdapter implements PhoneValidator {
isValid (phone: string): boolean {
return validator.isMobilePhone(phone, 'pt-BR')
}
}
Loading