-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrestrict_number.go
More file actions
148 lines (128 loc) · 4.63 KB
/
Copy pathrestrict_number.go
File metadata and controls
148 lines (128 loc) · 4.63 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// Copyright (c) 2024 H0llyW00dz All rights reserved.
//
// License: BSD 3-Clause License
package validator
import (
"encoding/xml"
"fmt"
"reflect"
"strconv"
"strings"
"github.com/gofiber/fiber/v2"
"golang.org/x/text/cases"
"golang.org/x/text/language"
)
// RestrictNumberOnly is a Restrictor implementation that restricts fields to contain only numbers
// and allows setting an optional maximum value and maximum number of digits.
type RestrictNumberOnly struct {
// Fields specifies the fields to check for number-only validation.
Fields []string
// Max specifies the maximum allowed value for the fields (optional).
Max *int
// MaxDigits specifies the maximum number of digits allowed in the field value (optional).
MaxDigits *int
}
// Restrict implements the Restrictor interface for RestrictNumberOnly.
// It checks the specified fields in the request body for numeric values and maximum limit based on the content type.
func (r RestrictNumberOnly) Restrict(c *fiber.Ctx) error {
return restrictByContentType(c, r.restrictJSON, r.restrictXML, r.restrictOther)
}
// restrictJSON checks the specified fields in the JSON request body for numeric values and maximum limit.
func (r RestrictNumberOnly) restrictJSON(c *fiber.Ctx) error {
var body map[string]interface{}
if err := c.BodyParser(&body); err != nil {
return NewError(fiber.StatusBadRequest, ErrInvalidJSONBody)
}
var invalidFields []string
for _, field := range r.Fields {
value, ok := body[field]
if ok {
var num int
var numStr string
switch v := value.(type) {
case string:
if !isNumberOnly(v) {
invalidFields = append(invalidFields, field)
continue
}
numStr = v
num, _ = strconv.Atoi(v)
case float64:
num = int(v)
numStr = strconv.Itoa(num)
default:
invalidFields = append(invalidFields, field)
continue
}
if r.MaxDigits != nil && len(numStr) > *r.MaxDigits {
return NewError(fiber.StatusBadRequest, fmt.Sprintf(ErrFieldExceedsMaximumDigits, field, *r.MaxDigits))
}
if r.Max != nil && num > *r.Max {
return NewError(fiber.StatusBadRequest, fmt.Sprintf(ErrFieldExceedsMaximumValue, field, *r.Max))
}
}
}
if len(invalidFields) > 0 {
return NewError(fiber.StatusBadRequest, fmt.Sprintf(ErrFieldMustContainNumbersOnly, strings.Join(invalidFields, "', '")))
}
return nil
}
// restrictXML checks the specified fields in the XML request body for numeric values and maximum limit.
func (r RestrictNumberOnly) restrictXML(c *fiber.Ctx) error {
fields := make([]reflect.StructField, len(r.Fields))
caser := cases.Title(language.English)
for i, field := range r.Fields {
fields[i] = reflect.StructField{
Name: caser.String(field),
Type: reflect.TypeOf(""),
Tag: reflect.StructTag(`xml:"` + field + `"`),
}
}
bodyType := reflect.StructOf(fields)
bodyValue := reflect.New(bodyType).Elem()
if err := xml.Unmarshal(c.Body(), bodyValue.Addr().Interface()); err != nil {
return NewError(fiber.StatusBadRequest, ErrInvalidXMLBody)
}
var invalidFields []string
for _, field := range r.Fields {
value := bodyValue.FieldByName(caser.String(field)).String()
if !isNumberOnly(value) {
invalidFields = append(invalidFields, field)
} else {
num, _ := strconv.Atoi(value)
if r.MaxDigits != nil && len(value) > *r.MaxDigits {
return NewError(fiber.StatusBadRequest, fmt.Sprintf(ErrFieldExceedsMaximumDigits, field, *r.MaxDigits))
}
if r.Max != nil && num > *r.Max {
return NewError(fiber.StatusBadRequest, fmt.Sprintf(ErrFieldExceedsMaximumValue, field, *r.Max))
}
}
}
if len(invalidFields) > 0 {
return NewError(fiber.StatusBadRequest, fmt.Sprintf(ErrFieldMustContainNumbersOnly, strings.Join(invalidFields, "', '")))
}
return nil
}
// restrictOther checks the specified fields in the request body of other content types for numeric values and maximum limit.
func (r RestrictNumberOnly) restrictOther(c *fiber.Ctx) error {
body := string(c.Body())
var invalidFields []string
for _, field := range r.Fields {
fieldValue := extractFieldValueForNumberOnly(body, field)
if !isNumberOnly(fieldValue) {
invalidFields = append(invalidFields, field)
} else {
num, _ := strconv.Atoi(fieldValue)
if r.MaxDigits != nil && len(fieldValue) > *r.MaxDigits {
return NewError(fiber.StatusBadRequest, fmt.Sprintf(ErrFieldExceedsMaximumDigits, field, *r.MaxDigits))
}
if r.Max != nil && num > *r.Max {
return NewError(fiber.StatusBadRequest, fmt.Sprintf(ErrFieldExceedsMaximumValue, field, *r.Max))
}
}
}
if len(invalidFields) > 0 {
return NewError(fiber.StatusBadRequest, fmt.Sprintf(ErrFieldMustContainNumbersOnly, strings.Join(invalidFields, "', '")))
}
return nil
}