-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b9b87d9
commit 2d92009
Showing
6 changed files
with
60 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { ZodError } from 'zod'; | ||
|
||
export const formatZodError = (zodError: ZodError) => { | ||
const formattedError = zodError.errors.map((error) => { | ||
return { | ||
field: error.path, | ||
error: error.message, | ||
}; | ||
}); | ||
return formattedError; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { ZodError, z } from 'zod'; | ||
|
||
import { CreateUserParams, LoginParams } from 'types'; | ||
import { ApiError } from 'utils/apiError'; | ||
import { errors } from 'config/errors'; | ||
import { formatZodError } from 'utils/validator'; | ||
|
||
const userSchema = z.object({ | ||
email: z.string().email({ message: 'Invalid email' }), | ||
name: z.string(), | ||
password: z.string().min(1, { message: "Can't be an empty password" }), | ||
}); | ||
|
||
const loginSchema = z.object({ | ||
email: z.string().email({ message: 'Invalid email' }), | ||
password: z.string().min(1, { message: "Can't be an empty password" }), | ||
}); | ||
|
||
export const validateUserSchema = (user: CreateUserParams) => { | ||
try { | ||
userSchema.parse(user); | ||
} catch (err) { | ||
const formattedZodError = formatZodError(err as ZodError); | ||
throw new ApiError(errors.VALIDATION_ERROR, true, formattedZodError); | ||
} | ||
}; | ||
|
||
export const validateLoginSchema = (loginParams: LoginParams) => { | ||
try { | ||
loginSchema.parse(loginParams); | ||
} catch (err) { | ||
const formattedZodError = formatZodError(err as ZodError); | ||
throw new ApiError(errors.VALIDATION_ERROR, true, formattedZodError); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters