Skip to content

Instantly share code, notes, and snippets.

@jmaicaaan
Last active June 29, 2020 15:11
Show Gist options
  • Save jmaicaaan/c5a47a19ba2d20d2cc42494569a463db to your computer and use it in GitHub Desktop.
Save jmaicaaan/c5a47a19ba2d20d2cc42494569a463db to your computer and use it in GitHub Desktop.
// non schema-based
const handleResponse = async () => {
const response = await fetch('https://myserver.com/api/user/1');
const {
first_name: firstName,
last_name: lastName,
avatar_url: avatarURL,
} = await response.json();
return {
firstName,
lastName,
avatarURL,
};
};
// Schema-based using Yup
const schema = Yup.object().shape({
firstName: Yup.string(),
lastName: Yup.string(),
avatarURL: Yup.string(),
});
const handleResponse = async () => {
const schema = schema
.clone()
.from('first_name', 'firstName')
.from('last_name', 'lastName')
.from('avatar_url', 'avatarURL');
const response = await fetch('https://myserver.com/api/user/1');
const data = await response.json();
return schema.cast(data);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment