Skip to content

Instantly share code, notes, and snippets.

@jmaicaaan
Last active June 29, 2020 14:47
Show Gist options
  • Save jmaicaaan/3db8a1983f3d770ca7ba6daaac04bedd to your computer and use it in GitHub Desktop.
Save jmaicaaan/3db8a1983f3d770ca7ba6daaac04bedd to your computer and use it in GitHub Desktop.
// Updating the first approach (non schema-based)
const handleSubmit = async ({
fname: firstName,
lname: lastName,
image: imageURL
}) => {
const response = await fetch('https://myserver.com/api/user', {
method: 'POST',
body: JSON.stringify({
firstName,
lastName,
imageURL,
}),
});
// ...
};
// Schema-based using Yup
const schema = Yup.object().shape({
firstName: Yup.string(),
lastName: Yup.string(),
avatarURL: Yup.string(),
});
const handleSubmit = async (formValues) => {
const data = schema
.clone()
.from('fname', 'firstName')
.from('lname', 'lastName')
.from('image', 'avatarURL')
.cast(formValues);
const response = await fetch('https://myserver.com/api/user', {
method: 'POST',
body: JSON.stringify(data),
});
// ...
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment