From 7454036bf5f24857e921adfeab9d4b0752db8ee3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Mon, 29 Apr 2024 14:08:50 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=A7=84=E5=88=99=E9=BB=98?= =?UTF-8?q?=E8=AE=A4=E5=AE=9E=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- define/rule.go | 11 +++-- go.mod | 2 + go.sum | 2 + rule_instance.go | 107 +++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 118 insertions(+), 4 deletions(-) create mode 100644 go.sum create mode 100644 rule_instance.go diff --git a/define/rule.go b/define/rule.go index 2027320..c7bd260 100644 --- a/define/rule.go +++ b/define/rule.go @@ -17,6 +17,9 @@ type FieldRule struct { Type string `json:"type"` // 数据类型, 具体枚举值参见 git.zhangdeman.cn/zhangdeman/consts DefaultValue string `json:"default_value"` // 默认值, 统一以字符串传入, 会转为最终设置的类型 IsRequired bool `json:"is_required"` // 是否必传 + AllowEmpty bool `json:"allow_empty"` // 必传时空字符串是否合法 + AllowZero bool `json:"allow_zero"` // 必传数字, 0 是否合法 + AllowNil bool `json:"allow_nil"` // 必传时, nil值是否合法 RequiredConditionGroup [][]RequiredCondition `json:"required_condition_group"` // 满足何种条件,字段必传,不配置则为无差别必传, 组之间是或条件, 满足一组即命中, 组之内为与条件 ValueLimit *ValueLimit `json:"value_limit"` // 数据值的限制 } @@ -63,8 +66,8 @@ type StringValueLimit struct { // // Date : 11:22 2024/4/29 type IntValueLimit struct { - Min int64 `json:"min"` // 最小值(包含) - Max int64 `json:"max"` // 最大值(不包含) + Min *int64 `json:"min"` // 最小值(包含) + Max *int64 `json:"max"` // 最大值(不包含) EnumList []int64 `json:"enum_list"` // 枚举值列表 } @@ -74,8 +77,8 @@ type IntValueLimit struct { // // Date : 11:22 2024/4/29 type FloatValueLimit struct { - Min float64 `json:"min"` // 最小值(包含) - Max float64 `json:"max"` // 最大值(不包含) + Min *float64 `json:"min"` // 最小值(包含) + Max *float64 `json:"max"` // 最大值(不包含) EnumList []float64 `json:"enum_list"` // 枚举值列表 } diff --git a/go.mod b/go.mod index adc8973..a6aa713 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,5 @@ module git.zhangdeman.cn/gateway/validator go 1.22.2 + +require git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240429055735-7d7191726f0c // indirect diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..b860184 --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240429055735-7d7191726f0c h1:n+n49onVpIgemvRdX4XnUB5psWh/NZ/qYkapCbJ4AYA= +git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240429055735-7d7191726f0c/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k= diff --git a/rule_instance.go b/rule_instance.go new file mode 100644 index 0000000..db9d357 --- /dev/null +++ b/rule_instance.go @@ -0,0 +1,107 @@ +// Package validator ... +// +// Description : validator ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2024-04-29 12:18 +package validator + +import "git.zhangdeman.cn/gateway/validator/define" + +// NewDefaultFieldRule ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 14:01 2024/4/29 +func NewDefaultFieldRule(path string, dataType string, isRequired bool, defaultValue string) *define.FieldRule { + r := &define.FieldRule{ + Path: path, + Type: dataType, + DefaultValue: defaultValue, + IsRequired: isRequired, + AllowNil: false, + AllowEmpty: false, + AllowZero: true, + RequiredConditionGroup: make([][]define.RequiredCondition, 0), + ValueLimit: &define.ValueLimit{ + String: nil, + Int: nil, + Float: nil, + Slice: nil, + Map: nil, + }, + } + return r +} + +// NewDefaultStringValueLimit ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 12:22 2024/4/29 +func NewDefaultStringValueLimit(minLength int64, maxLength int64) *define.StringValueLimit { + l := &define.StringValueLimit{ + MinLength: minLength, + MaxLength: maxLength, + AutoTrimSpace: true, + IncludeSubStrList: make([]string, 0), + EnumList: make([]string, 0), + } + return l +} + +// NewDefaultIntValueLimit ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 12:24 2024/4/29 +func NewDefaultIntValueLimit(min *int64, max *int64) *define.IntValueLimit { + l := &define.IntValueLimit{ + Min: min, + Max: max, + EnumList: make([]int64, 0), + } + return l +} + +// NewDefaultFloatValueLimit ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 12:27 2024/4/29 +func NewDefaultFloatValueLimit(min *float64, max *float64) *define.FloatValueLimit { + l := &define.FloatValueLimit{ + Min: min, + Max: max, + EnumList: make([]float64, 0), + } + return l +} + +// NewDefaultSliceValueLimit ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 12:30 2024/4/29 +func NewDefaultSliceValueLimit(minLength int64, maxLength int64) *define.SliceValueLimit { + l := &define.SliceValueLimit{ + MinLength: minLength, + MaxLength: maxLength, + } + return l +} + +// NewDefaultMapValueLimit ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 12:30 2024/4/29 +func NewDefaultMapValueLimit(minLength int64, maxLength int64) *define.MapValueLimit { + l := &define.MapValueLimit{ + MinLength: minLength, + MaxLength: maxLength, + IncludeFieldList: make([]string, 0), + } + return l +}