| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440 |
- package validator
- import (
- "fmt"
- "google.golang.org/protobuf/reflect/protoreflect"
- validate "git.ikuban.com/server/kubanapis/kuban/api/validate"
- )
- // validateInt32 验证 int32
- func (e *Engine) validateInt32(
- vctx *ValidationContext,
- fieldRules *validate.FieldRules,
- rules *validate.Int32Rules,
- value int32,
- fieldPath string,
- errors *ValidationErrors,
- ) error {
- if rules.Const != nil && value != *rules.Const {
- defaultMsg := fmt.Sprintf("字段 %s 必须等于 %d", fieldPath, *rules.Const)
- errors.Add(NewValidationError(fieldPath, ErrCodeInvalidValue,
- e.getErrorMessage(fieldRules, vctx, "const", defaultMsg)))
- }
- if rules.Lt != nil && value >= *rules.Lt {
- defaultMsg := fmt.Sprintf("字段 %s 必须小于 %d", fieldPath, *rules.Lt)
- errors.Add(NewValidationError(fieldPath, ErrCodeOutOfRange,
- e.getErrorMessage(fieldRules, vctx, "range", defaultMsg)))
- }
- if rules.Lte != nil && value > *rules.Lte {
- defaultMsg := fmt.Sprintf("字段 %s 必须小于等于 %d", fieldPath, *rules.Lte)
- errors.Add(NewValidationError(fieldPath, ErrCodeOutOfRange,
- e.getErrorMessage(fieldRules, vctx, "range", defaultMsg)))
- }
- if rules.Gt != nil && value <= *rules.Gt {
- defaultMsg := fmt.Sprintf("字段 %s 必须大于 %d", fieldPath, *rules.Gt)
- errors.Add(NewValidationError(fieldPath, ErrCodeOutOfRange,
- e.getErrorMessage(fieldRules, vctx, "range", defaultMsg)))
- }
- if rules.Gte != nil && value < *rules.Gte {
- defaultMsg := fmt.Sprintf("字段 %s 必须大于等于 %d", fieldPath, *rules.Gte)
- errors.Add(NewValidationError(fieldPath, ErrCodeOutOfRange,
- e.getErrorMessage(fieldRules, vctx, "range", defaultMsg)))
- }
- return e.validateInt32In(value, rules, fieldPath, errors, fieldRules, vctx)
- }
- func (e *Engine) validateInt32In(value int32, rules *validate.Int32Rules, fieldPath string, errors *ValidationErrors, fieldRules *validate.FieldRules, vctx *ValidationContext) error {
- if len(rules.In) > 0 {
- found := false
- for _, v := range rules.In {
- if value == v {
- found = true
- break
- }
- }
- if !found {
- defaultMsg := fmt.Sprintf("字段 %s 的值不在允许的列表中", fieldPath)
- errors.Add(NewValidationError(fieldPath, ErrCodeInvalidValue,
- e.getErrorMessage(fieldRules, vctx, "in", defaultMsg)))
- }
- }
- if len(rules.NotIn) > 0 {
- for _, v := range rules.NotIn {
- if value == v {
- defaultMsg := fmt.Sprintf("字段 %s 的值在禁止列表中", fieldPath)
- errors.Add(NewValidationError(fieldPath, ErrCodeInvalidValue,
- e.getErrorMessage(fieldRules, vctx, "not_in", defaultMsg)))
- break
- }
- }
- }
- return nil
- }
- // validateInt64 验证 int64
- func (e *Engine) validateInt64(
- vctx *ValidationContext,
- fieldRules *validate.FieldRules,
- rules *validate.Int64Rules,
- value int64,
- fieldPath string,
- errors *ValidationErrors,
- ) error {
- if rules.Const != nil && value != *rules.Const {
- defaultMsg := fmt.Sprintf("字段 %s 必须等于 %d", fieldPath, *rules.Const)
- errors.Add(NewValidationError(fieldPath, ErrCodeInvalidValue,
- e.getErrorMessage(fieldRules, vctx, "const", defaultMsg)))
- }
- if rules.Lt != nil && value >= *rules.Lt {
- defaultMsg := fmt.Sprintf("字段 %s 必须小于 %d", fieldPath, *rules.Lt)
- errors.Add(NewValidationError(fieldPath, ErrCodeOutOfRange,
- e.getErrorMessage(fieldRules, vctx, "range", defaultMsg)))
- }
- if rules.Lte != nil && value > *rules.Lte {
- defaultMsg := fmt.Sprintf("字段 %s 必须小于等于 %d", fieldPath, *rules.Lte)
- errors.Add(NewValidationError(fieldPath, ErrCodeOutOfRange,
- e.getErrorMessage(fieldRules, vctx, "range", defaultMsg)))
- }
- if rules.Gt != nil && value <= *rules.Gt {
- defaultMsg := fmt.Sprintf("字段 %s 必须大于 %d", fieldPath, *rules.Gt)
- errors.Add(NewValidationError(fieldPath, ErrCodeOutOfRange,
- e.getErrorMessage(fieldRules, vctx, "range", defaultMsg)))
- }
- if rules.Gte != nil && value < *rules.Gte {
- defaultMsg := fmt.Sprintf("字段 %s 必须大于等于 %d", fieldPath, *rules.Gte)
- errors.Add(NewValidationError(fieldPath, ErrCodeOutOfRange,
- e.getErrorMessage(fieldRules, vctx, "range", defaultMsg)))
- }
- return e.validateInt64In(value, rules, fieldPath, errors, fieldRules, vctx)
- }
- func (e *Engine) validateInt64In(value int64, rules *validate.Int64Rules, fieldPath string, errors *ValidationErrors, fieldRules *validate.FieldRules, vctx *ValidationContext) error {
- if len(rules.In) > 0 {
- found := false
- for _, v := range rules.In {
- if value == v {
- found = true
- break
- }
- }
- if !found {
- defaultMsg := fmt.Sprintf("字段 %s 的值不在允许的列表中", fieldPath)
- errors.Add(NewValidationError(fieldPath, ErrCodeInvalidValue,
- e.getErrorMessage(fieldRules, vctx, "in", defaultMsg)))
- }
- }
- if len(rules.NotIn) > 0 {
- for _, v := range rules.NotIn {
- if value == v {
- defaultMsg := fmt.Sprintf("字段 %s 的值在禁止列表中", fieldPath)
- errors.Add(NewValidationError(fieldPath, ErrCodeInvalidValue,
- e.getErrorMessage(fieldRules, vctx, "not_in", defaultMsg)))
- break
- }
- }
- }
- return nil
- }
- // validateUInt32 验证 uint32
- func (e *Engine) validateUInt32(
- vctx *ValidationContext,
- fieldRules *validate.FieldRules,
- rules *validate.UInt32Rules,
- value uint32,
- fieldPath string,
- errors *ValidationErrors,
- ) error {
- if rules.Const != nil && value != *rules.Const {
- defaultMsg := fmt.Sprintf("字段 %s 必须等于 %d", fieldPath, *rules.Const)
- errors.Add(NewValidationError(fieldPath, ErrCodeInvalidValue,
- e.getErrorMessage(fieldRules, vctx, "const", defaultMsg)))
- }
- if rules.Lt != nil && value >= *rules.Lt {
- defaultMsg := fmt.Sprintf("字段 %s 必须小于 %d", fieldPath, *rules.Lt)
- errors.Add(NewValidationError(fieldPath, ErrCodeOutOfRange,
- e.getErrorMessage(fieldRules, vctx, "range", defaultMsg)))
- }
- if rules.Lte != nil && value > *rules.Lte {
- defaultMsg := fmt.Sprintf("字段 %s 必须小于等于 %d", fieldPath, *rules.Lte)
- errors.Add(NewValidationError(fieldPath, ErrCodeOutOfRange,
- e.getErrorMessage(fieldRules, vctx, "range", defaultMsg)))
- }
- if rules.Gt != nil && value <= *rules.Gt {
- defaultMsg := fmt.Sprintf("字段 %s 必须大于 %d", fieldPath, *rules.Gt)
- errors.Add(NewValidationError(fieldPath, ErrCodeOutOfRange,
- e.getErrorMessage(fieldRules, vctx, "range", defaultMsg)))
- }
- if rules.Gte != nil && value < *rules.Gte {
- defaultMsg := fmt.Sprintf("字段 %s 必须大于等于 %d", fieldPath, *rules.Gte)
- errors.Add(NewValidationError(fieldPath, ErrCodeOutOfRange,
- e.getErrorMessage(fieldRules, vctx, "range", defaultMsg)))
- }
- return e.validateUInt32In(value, rules, fieldPath, errors, fieldRules, vctx)
- }
- func (e *Engine) validateUInt32In(value uint32, rules *validate.UInt32Rules, fieldPath string, errors *ValidationErrors, fieldRules *validate.FieldRules, vctx *ValidationContext) error {
- if len(rules.In) > 0 {
- found := false
- for _, v := range rules.In {
- if value == v {
- found = true
- break
- }
- }
- if !found {
- defaultMsg := fmt.Sprintf("字段 %s 的值不在允许的列表中", fieldPath)
- errors.Add(NewValidationError(fieldPath, ErrCodeInvalidValue,
- e.getErrorMessage(fieldRules, vctx, "in", defaultMsg)))
- }
- }
- if len(rules.NotIn) > 0 {
- for _, v := range rules.NotIn {
- if value == v {
- defaultMsg := fmt.Sprintf("字段 %s 的值在禁止列表中", fieldPath)
- errors.Add(NewValidationError(fieldPath, ErrCodeInvalidValue,
- e.getErrorMessage(fieldRules, vctx, "not_in", defaultMsg)))
- break
- }
- }
- }
- return nil
- }
- // validateUInt64 验证 uint64
- func (e *Engine) validateUInt64(
- vctx *ValidationContext,
- fieldRules *validate.FieldRules,
- rules *validate.UInt64Rules,
- value uint64,
- fieldPath string,
- errors *ValidationErrors,
- ) error {
- if rules.Const != nil && value != *rules.Const {
- defaultMsg := fmt.Sprintf("字段 %s 必须等于 %d", fieldPath, *rules.Const)
- errors.Add(NewValidationError(fieldPath, ErrCodeInvalidValue,
- e.getErrorMessage(fieldRules, vctx, "const", defaultMsg)))
- }
- if rules.Lt != nil && value >= *rules.Lt {
- defaultMsg := fmt.Sprintf("字段 %s 必须小于 %d", fieldPath, *rules.Lt)
- errors.Add(NewValidationError(fieldPath, ErrCodeOutOfRange,
- e.getErrorMessage(fieldRules, vctx, "range", defaultMsg)))
- }
- if rules.Lte != nil && value > *rules.Lte {
- defaultMsg := fmt.Sprintf("字段 %s 必须小于等于 %d", fieldPath, *rules.Lte)
- errors.Add(NewValidationError(fieldPath, ErrCodeOutOfRange,
- e.getErrorMessage(fieldRules, vctx, "range", defaultMsg)))
- }
- if rules.Gt != nil && value <= *rules.Gt {
- defaultMsg := fmt.Sprintf("字段 %s 必须大于 %d", fieldPath, *rules.Gt)
- errors.Add(NewValidationError(fieldPath, ErrCodeOutOfRange,
- e.getErrorMessage(fieldRules, vctx, "range", defaultMsg)))
- }
- if rules.Gte != nil && value < *rules.Gte {
- defaultMsg := fmt.Sprintf("字段 %s 必须大于等于 %d", fieldPath, *rules.Gte)
- errors.Add(NewValidationError(fieldPath, ErrCodeOutOfRange,
- e.getErrorMessage(fieldRules, vctx, "range", defaultMsg)))
- }
- return e.validateUInt64In(value, rules, fieldPath, errors, fieldRules, vctx)
- }
- func (e *Engine) validateUInt64In(value uint64, rules *validate.UInt64Rules, fieldPath string, errors *ValidationErrors, fieldRules *validate.FieldRules, vctx *ValidationContext) error {
- if len(rules.In) > 0 {
- found := false
- for _, v := range rules.In {
- if value == v {
- found = true
- break
- }
- }
- if !found {
- defaultMsg := fmt.Sprintf("字段 %s 的值不在允许的列表中", fieldPath)
- errors.Add(NewValidationError(fieldPath, ErrCodeInvalidValue,
- e.getErrorMessage(fieldRules, vctx, "in", defaultMsg)))
- }
- }
- if len(rules.NotIn) > 0 {
- for _, v := range rules.NotIn {
- if value == v {
- defaultMsg := fmt.Sprintf("字段 %s 的值在禁止列表中", fieldPath)
- errors.Add(NewValidationError(fieldPath, ErrCodeInvalidValue,
- e.getErrorMessage(fieldRules, vctx, "not_in", defaultMsg)))
- break
- }
- }
- }
- return nil
- }
- // validateFloat 验证 float32
- func (e *Engine) validateFloat(
- vctx *ValidationContext,
- fieldRules *validate.FieldRules,
- rules *validate.FloatRules,
- value float32,
- fieldPath string,
- errors *ValidationErrors,
- ) error {
- if rules.Const != nil && value != *rules.Const {
- defaultMsg := fmt.Sprintf("字段 %s 必须等于 %f", fieldPath, *rules.Const)
- errors.Add(NewValidationError(fieldPath, ErrCodeInvalidValue,
- e.getErrorMessage(fieldRules, vctx, "const", defaultMsg)))
- }
- if rules.Lt != nil && value >= *rules.Lt {
- defaultMsg := fmt.Sprintf("字段 %s 必须小于 %f", fieldPath, *rules.Lt)
- errors.Add(NewValidationError(fieldPath, ErrCodeOutOfRange,
- e.getErrorMessage(fieldRules, vctx, "range", defaultMsg)))
- }
- if rules.Lte != nil && value > *rules.Lte {
- defaultMsg := fmt.Sprintf("字段 %s 必须小于等于 %f", fieldPath, *rules.Lte)
- errors.Add(NewValidationError(fieldPath, ErrCodeOutOfRange,
- e.getErrorMessage(fieldRules, vctx, "range", defaultMsg)))
- }
- if rules.Gt != nil && value <= *rules.Gt {
- defaultMsg := fmt.Sprintf("字段 %s 必须大于 %f", fieldPath, *rules.Gt)
- errors.Add(NewValidationError(fieldPath, ErrCodeOutOfRange,
- e.getErrorMessage(fieldRules, vctx, "range", defaultMsg)))
- }
- if rules.Gte != nil && value < *rules.Gte {
- defaultMsg := fmt.Sprintf("字段 %s 必须大于等于 %f", fieldPath, *rules.Gte)
- errors.Add(NewValidationError(fieldPath, ErrCodeOutOfRange,
- e.getErrorMessage(fieldRules, vctx, "range", defaultMsg)))
- }
- return nil
- }
- // validateDouble 验证 float64
- func (e *Engine) validateDouble(
- vctx *ValidationContext,
- fieldRules *validate.FieldRules,
- rules *validate.DoubleRules,
- value float64,
- fieldPath string,
- errors *ValidationErrors,
- ) error {
- if rules.Const != nil && value != *rules.Const {
- defaultMsg := fmt.Sprintf("字段 %s 必须等于 %f", fieldPath, *rules.Const)
- errors.Add(NewValidationError(fieldPath, ErrCodeInvalidValue,
- e.getErrorMessage(fieldRules, vctx, "const", defaultMsg)))
- }
- if rules.Lt != nil && value >= *rules.Lt {
- defaultMsg := fmt.Sprintf("字段 %s 必须小于 %f", fieldPath, *rules.Lt)
- errors.Add(NewValidationError(fieldPath, ErrCodeOutOfRange,
- e.getErrorMessage(fieldRules, vctx, "range", defaultMsg)))
- }
- if rules.Lte != nil && value > *rules.Lte {
- defaultMsg := fmt.Sprintf("字段 %s 必须小于等于 %f", fieldPath, *rules.Lte)
- errors.Add(NewValidationError(fieldPath, ErrCodeOutOfRange,
- e.getErrorMessage(fieldRules, vctx, "range", defaultMsg)))
- }
- if rules.Gt != nil && value <= *rules.Gt {
- defaultMsg := fmt.Sprintf("字段 %s 必须大于 %f", fieldPath, *rules.Gt)
- errors.Add(NewValidationError(fieldPath, ErrCodeOutOfRange,
- e.getErrorMessage(fieldRules, vctx, "range", defaultMsg)))
- }
- if rules.Gte != nil && value < *rules.Gte {
- defaultMsg := fmt.Sprintf("字段 %s 必须大于等于 %f", fieldPath, *rules.Gte)
- errors.Add(NewValidationError(fieldPath, ErrCodeOutOfRange,
- e.getErrorMessage(fieldRules, vctx, "range", defaultMsg)))
- }
- return nil
- }
- // validateBool 验证 bool
- func (e *Engine) validateBool(
- vctx *ValidationContext,
- fieldRules *validate.FieldRules,
- rules *validate.BoolRules,
- value bool,
- fieldPath string,
- errors *ValidationErrors,
- ) error {
- if rules.Const != nil && value != *rules.Const {
- defaultMsg := fmt.Sprintf("字段 %s 必须等于 %t", fieldPath, *rules.Const)
- errors.Add(NewValidationError(fieldPath, ErrCodeInvalidValue,
- e.getErrorMessage(fieldRules, vctx, "const", defaultMsg)))
- }
- return nil
- }
- // validateRepeated 验证 repeated 字段
- func (e *Engine) validateRepeated(
- vctx *ValidationContext,
- fieldRules *validate.FieldRules,
- rules *validate.RepeatedRules,
- list protoreflect.List,
- field protoreflect.FieldDescriptor,
- fieldPath string,
- errors *ValidationErrors,
- ) error {
- if list == nil {
- return nil
- }
- size := list.Len()
- // 验证元素数量
- if rules.MinItems != nil && uint64(size) < *rules.MinItems {
- defaultMsg := fmt.Sprintf("字段 %s 至少需要 %d 个元素", fieldPath, *rules.MinItems)
- errors.Add(NewValidationError(fieldPath, ErrCodeInvalidLength,
- e.getErrorMessage(fieldRules, vctx, "min_items", defaultMsg)))
- }
- if rules.MaxItems != nil && uint64(size) > *rules.MaxItems {
- defaultMsg := fmt.Sprintf("字段 %s 最多允许 %d 个元素", fieldPath, *rules.MaxItems)
- errors.Add(NewValidationError(fieldPath, ErrCodeInvalidLength,
- e.getErrorMessage(fieldRules, vctx, "max_items", defaultMsg)))
- }
- // 验证唯一性
- if rules.Unique != nil && *rules.Unique {
- seen := make(map[interface{}]bool)
- for i := 0; i < size; i++ {
- item := list.Get(i).Interface()
- if seen[item] {
- defaultMsg := fmt.Sprintf("字段 %s 包含重复元素", fieldPath)
- errors.Add(NewValidationError(fieldPath, ErrCodeInvalidValue,
- e.getErrorMessage(fieldRules, vctx, "unique", defaultMsg)))
- break
- }
- seen[item] = true
- }
- }
- // TODO: 验证每个元素(需要递归调用validateFieldType)
- return nil
- }
- // validateMap 验证 map 字段
- func (e *Engine) validateMap(
- vctx *ValidationContext,
- fieldRules *validate.FieldRules,
- rules *validate.MapRules,
- mapValue protoreflect.Map,
- field protoreflect.FieldDescriptor,
- fieldPath string,
- errors *ValidationErrors,
- ) error {
- if mapValue == nil {
- return nil
- }
- size := mapValue.Len()
- // 验证键值对数量
- if rules.MinPairs != nil && uint64(size) < *rules.MinPairs {
- defaultMsg := fmt.Sprintf("字段 %s 至少需要 %d 个键值对", fieldPath, *rules.MinPairs)
- errors.Add(NewValidationError(fieldPath, ErrCodeInvalidLength,
- e.getErrorMessage(fieldRules, vctx, "min_pairs", defaultMsg)))
- }
- if rules.MaxPairs != nil && uint64(size) > *rules.MaxPairs {
- defaultMsg := fmt.Sprintf("字段 %s 最多允许 %d 个键值对", fieldPath, *rules.MaxPairs)
- errors.Add(NewValidationError(fieldPath, ErrCodeInvalidLength,
- e.getErrorMessage(fieldRules, vctx, "max_pairs", defaultMsg)))
- }
- // TODO: 验证 keys 和 values
- return nil
- }
|