-
Notifications
You must be signed in to change notification settings - Fork 0
/
validate.go
774 lines (659 loc) · 23 KB
/
validate.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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
package fastapi
import (
"context"
"errors"
"fmt"
"github.com/Chendemo12/fastapi/openapi"
"github.com/Chendemo12/fastapi/utils"
"github.com/go-playground/validator/v10"
jsoniter "github.com/json-iterator/go"
"reflect"
"strconv"
"strings"
"time"
)
const ( // error message
ModelNotDefine = "Data model is undefined"
ModelNotMatch = "Value type mismatch"
ModelCannotString = "The return value cannot be a string"
ModelCannotNumber = "The return value cannot be a number"
ModelCannotInteger = "The return value cannot be a integer"
ModelCannotBool = "The return value cannot be a boolean"
ModelCannotArray = "The return value cannot be a array"
PathPsIsEmpty = "Path must not be empty"
QueryPsIsEmpty = "Query must not be empty"
)
const ( // json序列化错误, 关键信息的序号
jsoniterUnmarshalErrorSeparator = "|" // 序列化错误信息分割符, 定义于 validator/validator_instance.orSeparator
jsonErrorFieldMsgIndex = 0 // 错误原因
jsonErrorFieldNameFormIndex = 1 // 序列化错误的字段和值
jsonErrorFormIndex = 3 // 接收到的数据
)
var defaultValidator *validator.Validate
var structQueryBind *StructQueryBind
var emptyLocList = []string{"response"}
var modelDescLabel = "param description"
var whereErrorLabel = "where error"
var validateErrorTagLabel = "tag"
var whereServerError = map[string]any{whereErrorLabel: "server"}
var whereClientError = map[string]any{whereErrorLabel: "client"}
type ModelBindMethod interface {
Name() string // 名称
// Validate
// 校验方法,对于响应首先校验,然后在 Marshal;对于请求,首先 Unmarshal 然后再校验
// 对于不需要ctx参数的校验方法可默认设置为nil
// data 为需要验证的数据模型,如果验证通过,则第一个返回值为做了类型转换的data
Validate(ctx context.Context, data any) (any, []*openapi.ValidationError)
Marshal(obj any) ([]byte, error) // 序列化方法,通过 ContentType 确定响应体类型
Unmarshal(stream []byte, obj any) (ves []*openapi.ValidationError) // 反序列化方法,通过 "http:header:Content-Type" 推断内容类型
New() any // 创建一个新实例
}
// UnsignedInteger 无符号数字约束
type UnsignedInteger interface {
~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uint
}
// SignedInteger 有符号数字约束
type SignedInteger interface {
~int8 | ~int16 | ~int32 | ~int64 | ~int
}
// NothingBindMethod 空实现,用于什么也不做
type NothingBindMethod struct{}
func (m *NothingBindMethod) Name() string { return "NothingBindMethod" }
func (m *NothingBindMethod) Validate(ctx context.Context, data any) (any, []*openapi.ValidationError) {
return data, nil
}
func (m *NothingBindMethod) Marshal(obj any) ([]byte, error) {
return []byte{}, nil
}
func (m *NothingBindMethod) Unmarshal(stream []byte, obj any) (ves []*openapi.ValidationError) {
return
}
func (m *NothingBindMethod) New() any {
return nil
}
// IntBindMethod 有符号数字验证
type IntBindMethod struct {
Title string `json:"title,omitempty"`
Kind reflect.Kind `json:"kind,omitempty"`
Maximum int64 `json:"maximum,omitempty"`
Minimum int64 `json:"minimum,omitempty"`
}
func (m *IntBindMethod) Name() string { return "IntBindMethod" }
func (m *IntBindMethod) Validate(ctx context.Context, data any) (any, []*openapi.ValidationError) {
var ves []*openapi.ValidationError
// 首先 data 必须是字符串类型
sv, ok := data.(string)
if !ok {
ves = append(ves, &openapi.ValidationError{
Loc: []string{"query", m.Title},
Msg: fmt.Sprintf("value: '%s' is not an integer", sv),
Type: string(openapi.IntegerType),
Ctx: whereClientError,
})
return nil, ves
}
atoi, err := strconv.ParseInt(sv, 10, 0)
if err != nil { // 无法转换为数字
ves = append(ves, &openapi.ValidationError{
Loc: []string{"query", m.Title},
Msg: fmt.Sprintf("value: '%s' is not a signed integer", sv),
Type: string(openapi.IntegerType),
Ctx: whereClientError,
})
return nil, ves
}
if atoi > m.Maximum || atoi < m.Minimum {
ves = append(ves, &openapi.ValidationError{
Ctx: map[string]any{"where error": "client"},
Msg: fmt.Sprintf("value: %s not <= %d and >= %d", sv, m.Maximum, m.Minimum),
Type: string(openapi.IntegerType),
Loc: []string{"param"},
})
return nil, ves
}
return atoi, ves
}
func (m *IntBindMethod) Marshal(obj any) ([]byte, error) {
// 目前无实际作用,不实现
return []byte{}, nil
}
func (m *IntBindMethod) Unmarshal(stream []byte, obj any) (ves []*openapi.ValidationError) {
// 可以通过 binary.BigEndian.Int64 实现,目前不实现
return
}
// New 返回int的零值
func (m *IntBindMethod) New() any {
return 0
}
// UintBindMethod 无符号数字验证
type UintBindMethod struct {
Title string `json:"title,omitempty"`
Kind reflect.Kind `json:"kind,omitempty"`
Maximum uint64 `json:"maximum,omitempty"`
Minimum uint64 `json:"minimum,omitempty"`
}
func (m *UintBindMethod) Name() string { return "UintBindMethod" }
func (m *UintBindMethod) Validate(ctx context.Context, data any) (any, []*openapi.ValidationError) {
var ves []*openapi.ValidationError
// 首先 data 必须是字符串类型
sv, ok := data.(string)
if !ok {
ves = append(ves, &openapi.ValidationError{
Loc: []string{"query", m.Title},
Msg: fmt.Sprintf("value: '%s' is not an integer", sv),
Type: string(openapi.IntegerType),
Ctx: whereClientError,
})
return nil, ves
}
atoi, err := strconv.ParseUint(sv, 10, 0)
if err != nil { // 无法转换为数字
ves = append(ves, &openapi.ValidationError{
Loc: []string{"query", m.Title},
Msg: fmt.Sprintf("value: '%s' is not an unsigned integer", sv),
Type: string(openapi.IntegerType),
Ctx: whereClientError,
})
return nil, ves
}
if atoi > m.Maximum || atoi < m.Minimum {
ves = append(ves, &openapi.ValidationError{
Ctx: map[string]any{"where error": "client"},
Msg: fmt.Sprintf("value: %s not <= %d and >= %d", sv, m.Maximum, m.Minimum),
Type: string(openapi.IntegerType),
Loc: []string{"param"},
})
return nil, ves
}
return atoi, ves
}
func (m *UintBindMethod) Marshal(obj any) ([]byte, error) {
// 目前无实际作用,不实现
return []byte{}, nil
}
func (m *UintBindMethod) Unmarshal(stream []byte, obj any) (ves []*openapi.ValidationError) {
// 可以通过 binary.BigEndian.Uint64 实现,目前不实现
return
}
// New 返回uint的零值
func (m *UintBindMethod) New() any {
return uint(0)
}
type FloatBindMethod struct {
Title string `json:"title,omitempty"`
Kind reflect.Kind `json:"kind,omitempty"`
}
func (m *FloatBindMethod) Name() string { return "FloatBindMethod" }
// Validate 验证字符串data是否是一个float类型,data 应为string类型
func (m *FloatBindMethod) Validate(ctx context.Context, data any) (any, []*openapi.ValidationError) {
var ves []*openapi.ValidationError
sv := data.(string)
// 对于float64类型暂不验证范围是否合理
atof, err := strconv.ParseFloat(sv, 64)
if err != nil {
ves = append(ves, &openapi.ValidationError{
Loc: []string{"query", m.Title},
Msg: fmt.Sprintf("value: '%s' is not a number", sv),
Type: string(openapi.NumberType),
Ctx: whereClientError,
})
return nil, ves
}
return atof, nil
}
func (m *FloatBindMethod) Marshal(obj any) ([]byte, error) {
return []byte{}, nil
}
func (m *FloatBindMethod) Unmarshal(stream []byte, obj any) (ves []*openapi.ValidationError) {
return
}
// New 返回float64的零值
func (m *FloatBindMethod) New() any {
return float64(0)
}
type BoolBindMethod struct {
Title string `json:"title,omitempty"`
}
func (m *BoolBindMethod) Name() string { return "BoolBindMethod" }
// Validate data 为字符串类型
func (m *BoolBindMethod) Validate(ctx context.Context, data any) (any, []*openapi.ValidationError) {
sv := data.(string)
atob, err := strconv.ParseBool(sv)
if err != nil {
var ves []*openapi.ValidationError
ves = append(ves, &openapi.ValidationError{
Loc: []string{"query", m.Title},
Msg: fmt.Sprintf("value: '%s' is not a bool", sv),
Type: string(openapi.BoolType),
Ctx: whereClientError,
})
return nil, ves
}
return atob, nil
}
func (m *BoolBindMethod) Marshal(obj any) ([]byte, error) {
return []byte{}, nil
}
func (m *BoolBindMethod) Unmarshal(stream []byte, obj any) (ves []*openapi.ValidationError) {
return ves
}
// New 返回 bool类型而零值false
func (m *BoolBindMethod) New() any {
return false
}
// JsonBindMethod json数据类型验证器,适用于泛型路由
type JsonBindMethod[T any] struct {
Title string `json:"title,omitempty"`
RouteParamType openapi.RouteParamType
}
func (m *JsonBindMethod[T]) where(key, value string) map[string]any {
var where = make(map[string]any)
if m.RouteParamType == openapi.RouteParamResponse {
where[whereErrorLabel] = whereServerError[whereErrorLabel]
} else {
where[whereErrorLabel] = whereClientError[whereErrorLabel]
}
if key != "" {
where[key] = value
}
return where
}
func (m *JsonBindMethod[T]) Name() string { return "JsonBindMethod" }
func (m *JsonBindMethod[T]) Validate(ctx context.Context, data T) (T, []*openapi.ValidationError) {
var vErr validator.ValidationErrors // validator的校验错误信息
err := defaultValidator.StructCtx(ctx, data)
if ok := errors.As(err, &vErr); ok { // 模型验证错误
ves := make([]*openapi.ValidationError, 0)
for _, verr := range vErr {
ves = append(ves, &openapi.ValidationError{
Ctx: m.where(validateErrorTagLabel, verr.Tag()),
Msg: verr.Error(),
Type: verr.Type().String(),
Loc: []string{"body", m.Title, verr.Field()},
})
}
var n T
return n, ves
}
return data, nil
}
func (m *JsonBindMethod[T]) Marshal(obj T) ([]byte, error) {
return utils.JsonMarshal(obj)
}
func (m *JsonBindMethod[T]) Unmarshal(stream []byte, obj T) (ves []*openapi.ValidationError) {
err := utils.JsonUnmarshal(stream, obj)
if err != nil {
ve := ParseJsoniterError(err, m.RouteParamType, m.Title)
ves = append(ves, ve)
}
return
}
func (m *JsonBindMethod[T]) New() any {
var value = new(T)
return value
}
// TimeBindMethod 时间校验方法
type TimeBindMethod struct {
Title string `json:"title,omitempty" description:"查询参数名"`
}
func (m *TimeBindMethod) Name() string { return "TimeBindMethod" }
// Validate 验证一个字符串是否是一个有效的时间字符串
// @return time.Time
func (m *TimeBindMethod) Validate(ctx context.Context, data any) (any, []*openapi.ValidationError) {
sv := data.(string) // 肯定是string类型
var err error
var t time.Time
layouts := []string{time.TimeOnly, time.Kitchen}
for _, layout := range layouts {
t, err = time.Parse(layout, sv)
if err == nil {
return t, nil
}
}
var ves []*openapi.ValidationError
ves = append(ves, &openapi.ValidationError{
Loc: []string{"query", m.Title},
Msg: fmt.Sprintf("value: '%s' is not a time, err:%v", sv, err),
Type: string(openapi.StringType),
Ctx: whereClientError,
})
return nil, ves
}
func (m *TimeBindMethod) Marshal(obj any) ([]byte, error) {
t, ok := obj.(time.Time)
if ok {
return []byte(t.Format(time.TimeOnly)), nil
}
return nil, errors.New("obj is not a time")
}
// Unmarshal time 类型不支持反序列化
func (m *TimeBindMethod) Unmarshal(stream []byte, obj any) (ves []*openapi.ValidationError) {
return nil
}
func (m *TimeBindMethod) New() any { return nil }
// DateBindMethod 日期校验
type DateBindMethod struct {
Title string `json:"title,omitempty" description:"查询参数名"`
}
func (m *DateBindMethod) Name() string { return "DateBindMethod" }
func (m *DateBindMethod) Validate(ctx context.Context, data any) (any, []*openapi.ValidationError) {
sv := data.(string) // 肯定是string类型
var err error
var t time.Time
layouts := []string{time.DateOnly}
for _, layout := range layouts {
t, err = time.Parse(layout, sv)
if err == nil {
return t, nil
}
}
var ves []*openapi.ValidationError
ves = append(ves, &openapi.ValidationError{
Loc: []string{"query", m.Title},
Msg: fmt.Sprintf("value: '%s' is not a date, err:%v", sv, err),
Type: string(openapi.StringType),
Ctx: whereClientError,
})
return nil, ves
}
func (m *DateBindMethod) Marshal(obj any) ([]byte, error) {
t, ok := obj.(time.Time)
if ok {
return []byte(t.Format(time.DateOnly)), nil
}
return nil, errors.New("obj is not a date")
}
func (m *DateBindMethod) Unmarshal(stream []byte, obj any) (ves []*openapi.ValidationError) {
//TODO implement me
panic("implement me")
}
func (m *DateBindMethod) New() any { return nil }
// DateTimeBindMethod 日期时间校验
type DateTimeBindMethod struct {
Title string `json:"title,omitempty" description:"查询参数名"`
}
func (m *DateTimeBindMethod) Name() string { return "DateTimeBindMethod" }
func (m *DateTimeBindMethod) Validate(ctx context.Context, data any) (any, []*openapi.ValidationError) {
sv := data.(string) // 肯定是string类型
var err error
var t time.Time
// 按照常用频率排序
layouts := []string{time.DateTime, time.RFC3339, time.DateOnly, time.TimeOnly, time.Kitchen, time.RFC3339Nano,
time.RFC822, time.ANSIC, time.UnixDate, time.RubyDate, time.RFC822Z, time.RFC850,
time.RFC1123, time.RFC1123Z, time.Stamp, time.StampMilli, time.StampMicro, time.StampNano,
}
for _, layout := range layouts {
t, err = time.Parse(layout, sv)
if err == nil {
return t, nil
}
}
var ves []*openapi.ValidationError
var timeErr *time.ParseError
if errors.As(err, &timeErr) {
ves = append(ves, &openapi.ValidationError{
Loc: []string{"query", m.Title},
Msg: fmt.Sprintf("value: '%s' is not a datetime, err:%s", sv, err.Error()),
Type: string(openapi.StringType),
Ctx: map[string]any{
whereErrorLabel: whereClientError[whereErrorLabel],
"layout": timeErr.Layout,
},
})
} else {
ves = append(ves, &openapi.ValidationError{
Loc: []string{"query", m.Title},
Msg: fmt.Sprintf("value: '%s' is not a datetime, err:%s", sv, err.Error()),
Type: string(openapi.StringType),
Ctx: whereClientError,
})
}
return nil, ves
}
func (m *DateTimeBindMethod) Marshal(obj any) ([]byte, error) {
t, ok := obj.(time.Time)
if ok {
return []byte(t.Format(time.DateTime)), nil
}
return nil, errors.New("obj is not a datetime")
}
func (m *DateTimeBindMethod) Unmarshal(stream []byte, obj any) (ves []*openapi.ValidationError) {
return nil
}
func (m *DateTimeBindMethod) New() any { return nil }
// StructQueryBind 结构体查询参数验证器
type StructQueryBind struct {
json jsoniter.API
}
func (m *StructQueryBind) Unmarshal(params map[string]any, obj any) *openapi.ValidationError {
s, err := m.json.Marshal(params)
if err != nil {
return ParseJsoniterError(err, openapi.RouteParamQuery, "")
}
err = m.json.Unmarshal(s, obj)
if err != nil {
return ParseJsoniterError(err, openapi.RouteParamQuery, "")
}
return nil
}
func (m *StructQueryBind) Validate(obj any) []*openapi.ValidationError {
err := defaultValidator.StructCtx(context.Background(), obj)
if err != nil {
ves := ParseValidatorError(err, openapi.RouteParamQuery, "")
return ves
}
return nil
}
func (m *StructQueryBind) Bind(params map[string]any, obj any) []*openapi.ValidationError {
ve := m.Unmarshal(params, obj)
if ve != nil {
return []*openapi.ValidationError{ve}
}
return m.Validate(obj)
}
// =================================== 👇 以下用于泛型的返回值校验 ===================================
// objectModelNotMatchResponse 结构体不匹配的错误返回体
//
// @param name ...string 注册的返回体,实际的返回体名称
func objectModelNotMatchResponse(name ...string) *openapi.ValidationError {
ve := &openapi.ValidationError{
Ctx: map[string]any{
"where error": "server",
"msg": fmt.Sprintf(
"response model should be '%s', but '%s' returned", name[0], name[1],
),
},
Msg: ModelNotMatch,
Type: string(openapi.ObjectType),
Loc: []string{"response", name[0]},
}
if len(name) > 0 {
ve.Ctx["msg"] = fmt.Sprintf(
"response model should be '%s', but 'string' returned", name[0],
)
}
return ve
}
func modelCannotBeStringResponse(name ...string) *openapi.ValidationError {
ve := &openapi.ValidationError{
Ctx: map[string]any{"where error": "server", "msg": ModelCannotString},
Msg: ModelNotMatch,
Type: string(openapi.StringType),
Loc: emptyLocList,
}
if len(name) > 0 {
ve.Ctx["msg"] = fmt.Sprintf(
"response model should be '%s', but 'string' returned", name[0],
)
}
return ve
}
func modelCannotBeNumberResponse(name ...string) *openapi.ValidationError {
ve := &openapi.ValidationError{
Ctx: map[string]any{"where error": "server", "msg": ModelCannotNumber},
Msg: ModelNotMatch,
Type: string(openapi.NumberType),
Loc: emptyLocList,
}
if len(name) > 0 {
ve.Ctx["msg"] = fmt.Sprintf(
"response model should be '%s', but 'string' returned", name[0],
)
}
return ve
}
func modelCannotBeBoolResponse(name ...string) *openapi.ValidationError {
ve := &openapi.ValidationError{
Ctx: map[string]any{"where error": "server", "msg": ModelCannotBool},
Msg: ModelNotMatch,
Type: string(openapi.BoolType),
Loc: emptyLocList,
}
if len(name) > 0 {
ve.Ctx["msg"] = fmt.Sprintf(
"response model should be '%s', but 'string' returned", name[0],
)
}
return ve
}
func modelCannotBeIntegerResponse(name ...string) *openapi.ValidationError {
ve := &openapi.ValidationError{
Ctx: map[string]any{"where error": "server", "msg": ModelCannotInteger},
Msg: ModelNotMatch,
Type: string(openapi.IntegerType),
Loc: emptyLocList,
}
if len(name) > 0 {
ve.Ctx["msg"] = fmt.Sprintf(
"response model should be '%s', but 'string' returned", name[0],
)
}
return ve
}
func modelCannotBeArrayResponse(name ...string) *openapi.ValidationError {
ve := &openapi.ValidationError{
Ctx: map[string]any{"where error": "server", "msg": ModelCannotArray},
Msg: ModelNotMatch,
Type: string(openapi.ArrayType),
Loc: emptyLocList,
}
if len(name) > 0 {
ve.Ctx["msg"] = fmt.Sprintf(
"response model should be '%s', but 'string' returned", name[0],
)
}
return ve
}
// =================================== 👇 methods ===================================
func newValidateErrorCtx(where map[string]any, key, value string) map[string]any {
m := map[string]any{}
m[whereErrorLabel] = where[whereErrorLabel]
m[key] = value
return m
}
// ParseJsoniterError 将jsoniter 的反序列化错误转换成 接口错误类型
func ParseJsoniterError(err error, loc openapi.RouteParamType, objName string) *openapi.ValidationError {
if err == nil {
return nil
}
// jsoniter 的反序列化错误格式:
//
// jsoniter.iter.ReportError():224
//
// iter.Error = fmt.Errorf("%s: %s, error found in #%v byte of ...|%s|..., bigger context ...|%s|...",
// operation, msg, iter.head-peekStart, parsing, context)
//
// err.Error():
//
// main.SimpleForm.name: ReadString: expmuxCtxts " or n, but found 2, error found in #10 byte of ...| "name": 23,
// "a|..., bigger context ...|{
// "name": 23,
// "age": "23",
// "sex": "F"
// }|...
msg := err.Error()
var where = make(map[string]any)
if loc == openapi.RouteParamResponse {
where = whereServerError
} else {
where = whereClientError
}
ve := &openapi.ValidationError{Loc: []string{string(loc)}, Ctx: where}
if objName != "" {
ve.Loc = append(ve.Loc, objName)
}
for i := 0; i < len(msg); i++ {
if msg[i:i+1] == ":" {
ve.Loc = append(ve.Loc, msg[:i])
break
}
}
if msgs := strings.Split(msg, jsoniterUnmarshalErrorSeparator); len(msgs) > 0 {
err = utils.JsonUnmarshal([]byte(msgs[jsonErrorFormIndex]), &ve.Ctx)
if err == nil {
ve.Msg = msgs[jsonErrorFieldMsgIndex][len(ve.Loc[1])+2:]
if s := strings.Split(ve.Msg, ":"); len(s) > 0 {
ve.Type = s[0]
}
}
}
return ve
}
// ParseValidatorError 解析Validator的错误消息
// 如果不存在错误,则返回nil; 如果 err 是 validator.ValidationErrors 的错误, 则解析并返回详细的错误原因,反之则返回模糊的错误原因
func ParseValidatorError(err error, loc openapi.RouteParamType, objName string) []*openapi.ValidationError {
if err == nil {
return nil
}
var vErr validator.ValidationErrors // validator的校验错误信息
var ves []*openapi.ValidationError
var where = make(map[string]any)
if loc == openapi.RouteParamResponse {
where = whereServerError
} else {
where = whereClientError
}
if ok := errors.As(err, &vErr); ok { // Validator的模型验证错误
for _, verr := range vErr {
ve := &openapi.ValidationError{
Ctx: newValidateErrorCtx(where, validateErrorTagLabel, verr.Tag()),
Msg: verr.Error(),
Type: verr.Type().String(),
}
if objName != "" {
ve.Loc = []string{string(loc), objName, verr.Field()}
} else {
ve.Loc = []string{string(loc), verr.Field()}
}
ves = append(ves, ve)
}
} else {
ves = append(ves, &openapi.ValidationError{
Ctx: where,
Msg: err.Error(),
Type: string(openapi.ObjectType),
Loc: []string{string(loc)},
})
}
return ves
}
func LazyInit() {
// 初始化默认结构体验证器
defaultValidator = validator.New()
defaultValidator.SetTagName(openapi.ValidateTagName)
// 初始化结构体查询参数方法
var queryStructJsonConf = jsoniter.Config{
IndentionStep: 0, // 指定格式化序列化输出时的空格缩进数量
EscapeHTML: false, // 转义输出HTML
MarshalFloatWith6Digits: true, // 指定浮点数序列化输出时最多保留6位小数
ObjectFieldMustBeSimpleString: true, // 开启该选项后,反序列化过程中不会对你的json串中对象的字段字符串可能包含的转义进行处理,因此你应该保证你的待解析json串中对象的字段应该是简单的字符串(不包含转义)
SortMapKeys: false, // 指定map类型序列化输出时按照其key排序
UseNumber: false, // 指定反序列化时将数字(整数、浮点数)解析成json.Number类型
DisallowUnknownFields: false, // 当开启该选项时,反序列化过程如果解析到未知字段,即在结构体的schema定义中找不到的字段时,不会跳过然后继续解析,而会返回错误
TagKey: openapi.QueryTagName, // 指定tag字符串,默认情况为"json"
OnlyTaggedField: false, // 当开启该选项时,只有带上tag的结构体字段才会被序列化输出
ValidateJsonRawMessage: false, // json.RawMessage类型的字段在序列化时会原封不动地进行输出。开启这个选项后,json-iterator会校验这种类型的字段包含的是否一个合法的json串,如果合法,原样输出;否则会输出"null"
CaseSensitive: false, // 开启该选项后,你的待解析json串中的对象的字段必须与你的schema定义的字段大小写严格一致
}
structQueryBind = &StructQueryBind{json: queryStructJsonConf.Froze()}
}