Write logic for createElection validation

This commit is contained in:
2024-12-28 17:13:22 +01:00
parent 3df4eac964
commit a22f3e5a71
7 changed files with 338 additions and 24 deletions

View File

@ -2,7 +2,7 @@
// Package api provides primitives to interact with the openapi HTTP API.
//
// Code generated by github.com/oapi-codegen/oapi-codegen/v2 version v2.4.1 DO NOT EDIT.
// Code generated by github.com/deepmap/oapi-codegen/v2 version v2.2.0 DO NOT EDIT.
package api
import (
@ -26,7 +26,7 @@ type CreateElectionRequest struct {
// ErrorResponse defines model for ErrorResponse.
type ErrorResponse struct {
// Code Machine-readable error code
Code string `json:"code"`
Code int `json:"code"`
// Details Additional error details when available
Details *map[string]interface{} `json:"details"`
@ -56,6 +56,7 @@ type MiddlewareFunc func(http.Handler) http.Handler
// CreateElection operation middleware
func (siw *ServerInterfaceWrapper) CreateElection(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
handler := http.Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
siw.Handler.CreateElection(w, r)
@ -65,7 +66,7 @@ func (siw *ServerInterfaceWrapper) CreateElection(w http.ResponseWriter, r *http
handler = middleware(handler)
}
handler.ServeHTTP(w, r)
handler.ServeHTTP(w, r.WithContext(ctx))
}
type UnescapedCookieParamError struct {
@ -142,27 +143,21 @@ func Handler(si ServerInterface) http.Handler {
return HandlerWithOptions(si, StdHTTPServerOptions{})
}
// ServeMux is an abstraction of http.ServeMux.
type ServeMux interface {
HandleFunc(pattern string, handler func(http.ResponseWriter, *http.Request))
ServeHTTP(w http.ResponseWriter, r *http.Request)
}
type StdHTTPServerOptions struct {
BaseURL string
BaseRouter ServeMux
BaseRouter *http.ServeMux
Middlewares []MiddlewareFunc
ErrorHandlerFunc func(w http.ResponseWriter, r *http.Request, err error)
}
// HandlerFromMux creates http.Handler with routing matching OpenAPI spec based on the provided mux.
func HandlerFromMux(si ServerInterface, m ServeMux) http.Handler {
func HandlerFromMux(si ServerInterface, m *http.ServeMux) http.Handler {
return HandlerWithOptions(si, StdHTTPServerOptions{
BaseRouter: m,
})
}
func HandlerFromMuxWithBaseURL(si ServerInterface, m ServeMux, baseURL string) http.Handler {
func HandlerFromMuxWithBaseURL(si ServerInterface, m *http.ServeMux, baseURL string) http.Handler {
return HandlerWithOptions(si, StdHTTPServerOptions{
BaseURL: baseURL,
BaseRouter: m,

View File

@ -0,0 +1,65 @@
package validator
import (
"regexp"
"slices"
"strings"
"time"
"unicode/utf8"
)
type Validator struct {
NonFieldErrors []string
FieldErrors map[string]string
}
func (v *Validator) Valid() bool {
return len(v.FieldErrors) == 0 && len(v.NonFieldErrors) == 0
}
func (v *Validator) AddNonFieldError(message string) {
v.NonFieldErrors = append(v.NonFieldErrors, message)
}
func (v *Validator) AddFieldError(key, message string) {
if v.FieldErrors == nil {
v.FieldErrors = make(map[string]string)
}
if _, exists := v.FieldErrors[key]; !exists {
v.FieldErrors[key] = message
}
}
func (v *Validator) CheckField(ok bool, key, message string) {
if !ok {
v.AddFieldError(key, message)
}
}
func NotBlank(value string) bool {
return strings.TrimSpace(value) != ""
}
func MinChars(value string, n int) bool {
return utf8.RuneCountInString(value) >= n
}
func MaxChars(value string, n int) bool {
return utf8.RuneCountInString(value) <= n
}
func PermittedValue[T comparable](value T, permittedValues ...T) bool {
return slices.Contains(permittedValues, value)
}
func Matches(value string, rx *regexp.Regexp) bool {
return rx.MatchString(value)
}
func GreaterThan(value int, n int) bool {
return value > n
}
func After(value time.Time, n time.Time) bool {
return value.After(n)
}