Created
April 30, 2021 18:01
-
-
Save whoisryosuke/32ff8ff12e04ab8443c8250c5df18a3b to your computer and use it in GitHub Desktop.
JS / Yup - Example of Yup form validation - @see: https://github.com/jquense/yup
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
| import * as yup from 'yup'; | |
| // In your React component... | |
| const checkFormData = async () => { | |
| let schema = yup.object().shape({ | |
| testName: yup.string().required(), | |
| name: yup.string().required(), | |
| email: yup.string().email().required(), | |
| }); | |
| // Put your React state in here | |
| // Returns true or false if form is valid or not | |
| const testPassed = await schema.isValid(this.state); | |
| return testPassed | |
| } | |
| const isValid = checkFormData(); | |
| if(isValid) { | |
| // Test passed! | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment