Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created February 2, 2023 02:33
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
/*
Valida que toda la lista de parametros exista en r.URL.Query. Si no encuentra alguno o
está vacío, regresa el error del parámetro faltante. En caso de éxito regresa una lista conteniendo
los parámetros en el mismo orden que fueron validados
*/
func validarParametrosGetDePeticionHttp(r *http.Request, parametros []string) ([]string, error) {
var valores []string
mapaDeValoresDeUrl := r.URL.Query()
for _, parametro := range parametros {
valorComoArreglo, ok := mapaDeValoresDeUrl[parametro]
if !ok {
return valores, fmt.Errorf("Falta el parámetro '%s'", parametro)
}
valor := valorComoArreglo[0]
if len(valor) <= 0 {
return valores, fmt.Errorf("El parámetro '%s' está vacío", parametro)
}
valores = append(valores, valor)
}
return valores, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment