forked from sundowndev/phoneinfoga
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidators.go
42 lines (32 loc) · 836 Bytes
/
validators.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package api
import (
"net/http"
"github.com/gin-gonic/gin"
"gopkg.in/sundowndev/phoneinfoga.v2/pkg/scanners"
)
// JSONResponse is the default API response type
type JSONResponse struct {
Success bool `json:"success"`
Error string `json:"error"`
}
type scanURL struct {
Number uint `uri:"number" binding:"required,min=2"`
}
// ValidateScanURL validates scan URLs
func ValidateScanURL(c *gin.Context) {
var v scanURL
if err := c.ShouldBindUri(&v); err != nil {
errorHandling(c, "Parameter 'number' must be a valid integer.")
return
}
number, err := scanners.LocalScan(c.Param("number"))
if err != nil {
errorHandling(c, err.Error())
return
}
c.Set("number", number)
}
func errorHandling(c *gin.Context, msg string) {
c.JSON(http.StatusBadRequest, JSONResponse{Success: false, Error: msg})
c.Abort()
}