๐ก๏ธInput Validator LabLAB
Input Validator Lab
In this lab you will build an input validation system that uses a custom error type to carry structured field-level error information.
Your Task
1. Define ValidationError
Implement Error() string so it returns:
For example: field email: must contain @
2. Implement validateEmail(s string) error
- The email must contain
@ - There must be at least one
.after the@ - Return a
ValidationErrorwithField: "email"if either check fails - Return
nilif the email is valid
3. Implement validatePassword(s string) error
- The password must be at least 8 characters long
- The password must contain at least one digit (0โ9)
- Return a
ValidationErrorwithField: "password"if either check fails - Return
nilif the password is valid
4. Implement validateAge(n int) error
- The age must be greater than 0 and less than 150
- Return a
ValidationErrorwithField: "age"if the check fails - Return
nilif the age is valid
5. Implement ValidateAll(email, password string, age int) []error
- Run all three validators
- Collect every non-nil error into a
[]errorslice - Return the slice (it may be empty if all inputs are valid)
Expected Behavior
Hint: use
strings.Containsto check for@and., and a loop over the password rune-by-rune (or withstrings.ContainsAny) to detect digits.
โ Enter (Mac) ยท Ctrl+Enter (Win/Linux)
โ Enter (Mac) ยท Ctrl+Enter (Win/Linux)