Last active
June 29, 2020 15:11
-
-
Save jmaicaaan/c5a47a19ba2d20d2cc42494569a463db to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// 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