I have a profile creation form in my project for which i am using react-hooks-form and yup library for validation.
In the form there is one field named Github-Username which is optional. But i want to validate it if users enters the username and it should be more than 2 characters, something like that.
const schema = yup.object().shape({
company: yup.string().min(3).required(),
website: yup.string(),
location: yup.string().min(2).required(),
skills: yup.string().min(3).required(),
githubUsername: yup.string().min(3).nullable().notRequired(),
bio: yup.string(),
});
const { register, handleSubmit, errors, touched } = useForm({
resolver: yupResolver(schema),
});
// Form Field
<Form.Group controlId="formBasicGusername">
<Form.Label>Github Username</Form.Label>
<Form.Control
type="text"
name="githubUsername"
ref={register}
/>
<span className="text-danger text-capitalize">
{errors.githubUsername?.message}
</span>
</Form.Group>
This is the schema i have written so far, which is not working for githubUsername. It showing the error if it's empty. I want to validate only if it's not empty. Any leads on this?