Write validation for createVotesRequest
This commit is contained in:
@ -101,11 +101,32 @@ func randomVoterIdentity() string {
|
|||||||
return string(b)
|
return string(b)
|
||||||
}
|
}
|
||||||
|
|
||||||
type createVoteRequestWithValidator struct {
|
type createVotesRequestWithValidator struct {
|
||||||
api.CreateVotesRequest
|
api.CreateVotesRequest
|
||||||
validator.Validator
|
validator.Validator
|
||||||
}
|
}
|
||||||
|
|
||||||
func (app *application) createVote(w http.ResponseWriter, r *http.Request) {
|
func (r *createVotesRequestWithValidator) isValid() bool {
|
||||||
|
r.CheckField(validator.NotBlank(*r.VoterIdentity), "voterIdentity", "must not be blank")
|
||||||
|
r.CheckField(validator.GreaterThan(*r.ElectionId, 0), "electionId", "must be greater than 0")
|
||||||
|
|
||||||
|
for _, choice := range *r.Choices {
|
||||||
|
r.CheckField(validator.NotBlank(*choice.ChoiceText), "choiceText", "must not be blank")
|
||||||
|
}
|
||||||
|
|
||||||
|
return r.Valid()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (app *application) createVote(w http.ResponseWriter, r *http.Request) {
|
||||||
|
var request createVotesRequestWithValidator
|
||||||
|
|
||||||
|
if err := app.unmarshalRequest(r, &request); err != nil {
|
||||||
|
app.clientError(w, http.StatusBadRequest, err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if !request.isValid() {
|
||||||
|
app.unprocessableEntityError(w, request.Validator)
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -236,7 +236,6 @@ components:
|
|||||||
minLength: 1
|
minLength: 1
|
||||||
tokens:
|
tokens:
|
||||||
type: integer
|
type: integer
|
||||||
minimum: 1
|
|
||||||
|
|
||||||
ErrorResponse:
|
ErrorResponse:
|
||||||
type: object
|
type: object
|
||||||
|
Reference in New Issue
Block a user