-
Notifications
You must be signed in to change notification settings - Fork 2
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
Add runtime type check #66
base: main
Are you sure you want to change the base?
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Overall the changes are good, just check the proper error message handling and the optional helper function to keep your code cleaner :D
discord || telegram || github || projectLink || aditionalInformation | ||
? 'Aditional information' | ||
: '' | ||
const discordHandle = discord ? `Discord handle: ${discord}` : '' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To keep it DRY, I recommend creating a helper function that takes the optional data variable and its message and returns an empty string, and replaces all of these ternary operators.
something like that
// Helper function that returns an empty string if the optional data is falsy
const handleOptionalData = (optionalData: string, message: string) => optionalData ? message : ''
// instead of
const discordHandle = discord ? `Discord handle: ${discord}` : ''
// replacing for
const discordHandle = handleOptionalData(discord, `Discord handle: ${discord}`)
} catch (err) { | ||
console.log(err) | ||
res.status(400).json({ | ||
success: false, | ||
}) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In case of an error in the execution of the endpoint is always good to also send the error message. Getting the message with the correct typing from an error response is a bit tricky, but here's a solution:
} catch (err) { | |
console.log(err) | |
res.status(400).json({ | |
success: false, | |
}) | |
} | |
} catch (err) { | |
console.log(err) | |
let message | |
if (err instanceof Error) message = err.message | |
else message = String(err) | |
res.status(400).json({ | |
success: false, | |
message | |
}) | |
} |
|
||
export const SubmitSchema = z.object({ | ||
firstName: nonEmptyStringContraint, | ||
email: nonEmptyStringContraint, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Zod string objects has a built-in method for checking the email syntax:
email: nonEmptyStringContraint, | |
email: nonEmptyStringContraint.email(), |
No description provided.