forked from TencentBlueKing/bk-cmdb
/
valid_basic.go
408 lines (363 loc) · 10.5 KB
/
valid_basic.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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/*
* Tencent is pleased to support the open source community by making 蓝鲸 available.
* Copyright (C) 2017-2018 THL A29 Limited, a Tencent company. All rights reserved.
* Licensed under the MIT License (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
* http://opensource.org/licenses/MIT
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific language governing permissions and
* limitations under the License.
*/
package validator
import (
"net/http"
"regexp"
"strconv"
"configcenter/src/common"
"configcenter/src/common/backbone"
"configcenter/src/common/blog"
"configcenter/src/common/metadata"
"configcenter/src/common/util"
)
// NewValidMap returns new NewValidMap
func NewValidMap(ownerID, objID string, pheader http.Header, engine *backbone.Engine) *ValidMap {
return &ValidMap{
Engine: engine,
pheader: pheader,
ownerID: ownerID,
objID: objID,
propertys: map[string]metadata.Attribute{},
require: map[string]bool{},
isOnly: map[string]bool{},
shouldIgnore: map[string]bool{},
}
}
// NewValidMapWithKeyFields returns new NewValidMap
func NewValidMapWithKeyFields(ownerID, objID string, ignoreFields []string, pheader http.Header, engine *backbone.Engine) *ValidMap {
tmp := NewValidMap(ownerID, objID, pheader, engine)
for _, item := range ignoreFields {
tmp.shouldIgnore[item] = true
}
return tmp
}
// Init init
func (valid *ValidMap) Init() error {
valid.errif = valid.CCErr.CreateDefaultCCErrorIf(util.GetLanguage(valid.pheader))
m := map[string]interface{}{
common.BKObjIDField: valid.objID,
common.BKOwnerIDField: valid.ownerID,
}
result, err := valid.CoreAPI.ObjectController().Meta().SelectObjectAttWithParams(valid.ctx, valid.pheader, m)
if nil != err {
return err
}
if !result.Result {
return valid.errif.Error(result.Code)
}
for _, attr := range result.Data {
if attr.PropertyID == common.BKChildStr || attr.PropertyID == common.BKParentStr {
continue
}
valid.propertys[attr.PropertyID] = attr
valid.propertyslice = append(valid.propertyslice, attr)
if attr.IsRequired {
valid.require[attr.PropertyID] = true
valid.requirefields = append(valid.requirefields, attr.PropertyID)
}
if attr.IsOnly {
valid.isOnly[attr.PropertyID] = true
}
}
return nil
}
// ValidMap basic valid
func (valid *ValidMap) ValidMap(valData map[string]interface{}, validType string, instID int64) error {
err := valid.Init()
if nil != err {
blog.Errorf("init validator faile %s", err.Error())
return err
}
//valid create request
if validType == common.ValidCreate {
FillLostedFieldValue(valData, valid.propertyslice, valid.requirefields)
}
for key, val := range valData {
if valid.shouldIgnore[key] {
// ignore the key field
continue
}
property, ok := valid.propertys[key]
if !ok {
blog.Error("params is not valid, the key is %s", key)
return valid.errif.Errorf(common.CCErrCommParamsIsInvalid, key)
}
fieldType := property.PropertyType
switch fieldType {
case common.FieldTypeSingleChar:
err = valid.validChar(val, key)
case common.FieldTypeLongChar:
err = valid.validLongChar(val, key)
case common.FieldTypeInt:
err = valid.validInt(val, key)
case common.FieldTypeEnum:
err = valid.validEnum(val, key)
case common.FieldTypeDate:
err = valid.validDate(val, key)
case common.FieldTypeTime:
err = valid.validTime(val, key)
case common.FieldTypeTimeZone:
err = valid.validTimeZone(val, key)
case common.FieldTypeBool:
err = valid.validBool(val, key)
default:
continue
}
if nil != err {
return err
}
}
if validType == common.ValidCreate {
return valid.validCreateUnique(valData)
}
return valid.validUpdateUnique(valData, instID)
}
//valid char
func (valid *ValidMap) validChar(val interface{}, key string) error {
if nil == val || "" == val {
if valid.require[key] {
blog.Error("params in need")
return valid.errif.Errorf(common.CCErrCommParamsNeedSet, key)
}
return nil
}
switch value := val.(type) {
case string:
if len(value) > common.FieldTypeSingleLenChar {
blog.Errorf("params over length %d", common.FieldTypeSingleLenChar)
return valid.errif.Errorf(common.CCErrCommOverLimit, key)
}
if 0 == len(value) {
if valid.require[key] {
blog.Error("params can not be empty")
return valid.errif.Errorf(common.CCErrCommParamsNeedSet, key)
}
return nil
}
if property, ok := valid.propertys[key]; ok && "" != val {
option, ok := property.Option.(string)
if !ok {
break
}
strReg, err := regexp.Compile(option)
if nil != err {
blog.Errorf(`params "%s" not match regexp "%s"`, val, option)
return valid.errif.Error(common.CCErrFieldRegValidFailed)
}
if !strReg.MatchString(value) {
blog.Errorf(`params "%s" not match regexp "%s"`, val, option)
return valid.errif.Error(common.CCErrFieldRegValidFailed)
}
}
default:
blog.Error("params should be string")
return valid.errif.Errorf(common.CCErrCommParamsNeedString, key)
}
return nil
}
//valid long char
func (valid *ValidMap) validLongChar(val interface{}, key string) error {
if nil == val || "" == val {
if valid.require[key] {
blog.Error("params in need")
return valid.errif.Errorf(common.CCErrCommParamsNeedSet, key)
}
return nil
}
switch value := val.(type) {
case string:
if len(value) > common.FieldTypeLongLenChar {
blog.Errorf("params over length %d", common.FieldTypeSingleLenChar)
return valid.errif.Errorf(common.CCErrCommOverLimit, key)
}
if 0 == len(value) {
if valid.require[key] {
blog.Error("params can not be empty")
return valid.errif.Errorf(common.CCErrCommParamsNeedSet, key)
}
return nil
}
if property, ok := valid.propertys[key]; ok && "" != val {
option, ok := property.Option.(string)
if !ok {
break
}
strReg, err := regexp.Compile(option)
if nil != err {
blog.Errorf(`params "%s" not match regexp "%s"`, val, option)
return valid.errif.Error(common.CCErrFieldRegValidFailed)
}
if !strReg.MatchString(value) {
blog.Errorf(`params "%s" not match regexp "%s"`, val, option)
return valid.errif.Error(common.CCErrFieldRegValidFailed)
}
}
default:
blog.Error("params should be string")
return valid.errif.Errorf(common.CCErrCommParamsNeedString, key)
}
return nil
}
// validInt valid int
func (valid *ValidMap) validInt(val interface{}, key string) error {
if nil == val {
if valid.require[key] {
blog.Error("params can not be null")
return valid.errif.Errorf(common.CCErrCommParamsNeedSet, key)
}
return nil
}
var value int64
value, err := util.GetInt64ByInterface(val)
if nil != err {
blog.Errorf("params %s:%#v not int", key, val)
return valid.errif.Errorf(common.CCErrCommParamsNeedInt, key)
}
property, ok := valid.propertys[key]
if !ok {
return nil
}
intObjOption := parseIntOption(property.Option)
if 0 == len(intObjOption.Min) || 0 == len(intObjOption.Max) {
return nil
}
maxValue, err := strconv.ParseInt(intObjOption.Max, 10, 64)
if nil != err {
maxValue = common.MaxInt64
}
minValue, err := strconv.ParseInt(intObjOption.Min, 10, 64)
if nil != err {
minValue = common.MinInt64
}
if value > maxValue || value < minValue {
blog.Errorf("params %s:%#v not valid", key, val)
return valid.errif.Errorf(common.CCErrCommParamsInvalid, key)
}
return nil
}
//valid char
func (valid *ValidMap) validTimeZone(val interface{}, key string) error {
if nil == val {
if valid.require[key] {
blog.Error("params can not be null")
return valid.errif.Errorf(common.CCErrCommParamsNeedSet, key)
}
return nil
}
switch value := val.(type) {
case string:
isMatch := util.IsTimeZone(value)
if false == isMatch {
blog.Error("params should be timezone")
return valid.errif.Errorf(common.CCErrCommParamsNeedTimeZone, key)
}
default:
blog.Error("params should be timezone")
return valid.errif.Errorf(common.CCErrCommParamsNeedTimeZone, key)
}
return nil
}
//validBool
func (valid *ValidMap) validBool(val interface{}, key string) error {
if nil == val {
if valid.require[key] {
blog.Error("params can not be null")
return valid.errif.Errorf(common.CCErrCommParamsNeedSet, key)
}
return nil
}
switch val.(type) {
case bool:
default:
blog.Error("params should be bool")
return valid.errif.Errorf(common.CCErrCommParamsNeedBool, key)
}
return nil
}
// validEnum valid enum
func (valid *ValidMap) validEnum(val interface{}, key string) error {
// validate require
if nil == val {
if valid.require[key] {
blog.Error("params can not be null")
return valid.errif.Errorf(common.CCErrCommParamsNeedSet, key)
}
return nil
}
// validate type
valStr, ok := val.(string)
if !ok {
return valid.errif.Errorf(common.CCErrCommParamsInvalid, key)
}
option, ok := valid.propertys[key]
if !ok {
return nil
}
// validate within enum
enumOption := ParseEnumOption(option.Option)
match := false
for _, k := range enumOption {
if k.ID == valStr {
match = true
break
}
}
if !match {
blog.V(3).Infof("params %s not valid, option %#v, raw option %#v, value: %#v", key, enumOption, option, val)
blog.Errorf("params %s not valid , enum value: %#v", key, val)
return valid.errif.Errorf(common.CCErrCommParamsInvalid, key)
}
return nil
}
//valid date
func (valid *ValidMap) validDate(val interface{}, key string) error {
if nil == val {
if valid.require[key] {
blog.Error("params can not be null")
return valid.errif.Errorf(common.CCErrCommParamsNeedSet, key)
}
return nil
}
valStr, ok := val.(string)
if false == ok {
blog.Error("date can shoule be string")
return valid.errif.Errorf(common.CCErrCommParamsShouldBeString, key)
}
result := util.IsDate(valStr)
if !result {
blog.Error("params is not valid")
return valid.errif.Errorf(common.CCErrCommParamsInvalid, key)
}
return nil
}
//valid time
func (valid *ValidMap) validTime(val interface{}, key string) error {
if nil == val {
if valid.require[key] {
blog.Error("params can not be null")
return valid.errif.Errorf(common.CCErrCommParamsNeedSet, key)
}
return nil
}
valStr, ok := val.(string)
if false == ok {
blog.Error("date can shoule be string")
return valid.errif.Errorf(common.CCErrCommParamsShouldBeString, key)
}
result := util.IsTime(valStr)
if !result {
blog.Error("params not valid")
return valid.errif.Errorf(common.CCErrCommParamsInvalid, key)
}
return nil
}