Add BE validator
This commit is contained in:
@ -9,6 +9,7 @@ import (
|
||||
"myapp/internal/cards/encryption"
|
||||
"myapp/internal/models"
|
||||
"myapp/internal/urlsigner"
|
||||
"myapp/internal/validator"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
@ -124,6 +125,14 @@ func (app *application) CreateCustomerAndSubscribeToPlan(w http.ResponseWriter,
|
||||
return
|
||||
}
|
||||
|
||||
// validate data
|
||||
v := validator.New()
|
||||
v.Check(len(data.FirstName) > 1, "first_name", "must be at least 2 characters")
|
||||
if !v.Valid() {
|
||||
app.failedValidation(w, r, v.Errors)
|
||||
return
|
||||
}
|
||||
|
||||
app.infoLog.Println(data.Email, data.LastFour, data.PaymentMethod, data.Plan)
|
||||
|
||||
card := cards.Card{
|
||||
|
@ -95,3 +95,20 @@ func (app *application) passwordMatches(hash, password string) (bool, error) {
|
||||
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func (app *application) failedValidation(
|
||||
w http.ResponseWriter,
|
||||
r *http.Request,
|
||||
errors map[string]string,
|
||||
) {
|
||||
var payload struct {
|
||||
OK bool `json:"ok"`
|
||||
Message string `json:"message"`
|
||||
Errors map[string]string `json:"errors"`
|
||||
}
|
||||
|
||||
payload.OK = false
|
||||
payload.Message = "failed validation"
|
||||
payload.Errors = errors
|
||||
app.writeJSON(w, http.StatusUnprocessableEntity, payload)
|
||||
}
|
||||
|
Reference in New Issue
Block a user