From 8e88b25705bd1be9c1c0e3dd2f938f53a3f2dac2 Mon Sep 17 00:00:00 2001 From: ko_oler <kooler89@gmail.com> Date: Fri, 29 Sep 2023 13:51:53 +0300 Subject: [PATCH] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B0=20=D0=BE=D1=88=D0=B8=D0=B1=D0=BA=D0=B0=20?= =?UTF-8?q?=D0=B2=D0=B0=D0=BB=D0=B8=D0=B4=D0=B0=D1=86=D0=B8=D0=B8=20=D0=BF?= =?UTF-8?q?=D1=80=D0=B8=20=D0=BF=D0=BE=D0=BF=D1=8B=D1=82=D0=BA=D0=B5=20?= =?UTF-8?q?=D1=81=D0=BE=D1=85=D1=80=D0=B0=D0=BD=D0=B8=D1=82=D1=8C=20=D1=8D?= =?UTF-8?q?=D0=BB=D0=B5=D0=BC=D0=B5=D0=BD=D1=82=20=D0=BA=D0=BE=D0=BB=D0=BB?= =?UTF-8?q?=D0=B5=D0=BA=D1=86=D0=B8=D0=B8,=20=D0=B5=D1=81=D0=BB=D0=B8=20?= =?UTF-8?q?=D0=BD=D0=B5=20=D0=B7=D0=B0=D0=BF=D0=BE=D0=BB=D0=BD=D0=B8=D1=82?= =?UTF-8?q?=D1=8C=20=D0=B7=D0=BD=D0=B0=D1=87=D0=B5=D0=BD=D0=B8=D0=B5=20?= =?UTF-8?q?=D0=B2=20=D1=81=D1=82=D1=80=D0=BE=D0=BA=D0=BE=D0=B2=D0=BE=D0=BC?= =?UTF-8?q?=20=D0=BF=D0=BE=D0=BB=D0=B5,=20=D0=B4=D0=BB=D1=8F=20=D0=BA?= =?UTF-8?q?=D0=BE=D1=82=D0=BE=D1=80=D0=BE=D0=B3=D0=BE=20=D0=B7=D0=B0=D0=B4?= =?UTF-8?q?=D0=B0=D0=BD=D0=B0=20=D0=BD=D0=B0=D1=81=D1=82=D1=80=D0=BE=D0=B9?= =?UTF-8?q?=D0=BA=D0=B0=20=D0=B4=D0=BB=D0=B8=D0=BD=D1=8B=20=D0=B2=20=D1=81?= =?UTF-8?q?=D1=85=D0=B5=D0=BC=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/schema/validate/string.go | 6 ++++++ pkg/schema/validate/string_test.go | 3 +++ 2 files changed, 9 insertions(+) diff --git a/pkg/schema/validate/string.go b/pkg/schema/validate/string.go index f05ca540..33fa453d 100644 --- a/pkg/schema/validate/string.go +++ b/pkg/schema/validate/string.go @@ -18,6 +18,9 @@ func MaxLength(max int) Validator { } func (t maxLength) Validate(_ context.Context, field *field.Field, value interface{}) error { + if value == nil { + return nil + } if s, ok := value.(string); ok { n := utf8.RuneCountInString(s) if t > 0 && n > int(t) { @@ -36,6 +39,9 @@ func MinLength(max int) Validator { } func (t minLength) Validate(_ context.Context, field *field.Field, value interface{}) error { + if value == nil { + return nil + } if s, ok := value.(string); ok { n := utf8.RuneCountInString(s) if n < int(t) { diff --git a/pkg/schema/validate/string_test.go b/pkg/schema/validate/string_test.go index b67e8c8d..156b5f63 100644 --- a/pkg/schema/validate/string_test.go +++ b/pkg/schema/validate/string_test.go @@ -92,9 +92,12 @@ func TestString(t *testing.T) { wantErr bool }{ {"Length Max", field.String().AddOptions(MaxLength(5)), "1234567", true}, + {"Length Max with <nil>", field.String().AddOptions(MaxLength(5)), nil, false}, {"Length Min", field.String().AddOptions(MinLength(10)), "1234", true}, + {"Length Min with <nil>", field.String().AddOptions(MinLength(10)), nil, false}, {"Length MinMax", field.String().AddOptions(MaxLength(6), MinLength(2)), "1234567", true}, {"Length MinMax", field.String().AddOptions(MaxLength(10), MinLength(7)), "123456", true}, + {"Length MinMax with <nil>", field.String().AddOptions(MaxLength(10), MinLength(7)), nil, false}, {"Enum miss", field.String().AddOptions(Enum(EnumOpt{Name: "N 1", Value: "n1"}, EnumOpt{Name: "N 2", Value: "n2"})), "n3", true}, {"Enum match", field.String().AddOptions(Enum(EnumOpt{Name: "N 1", Value: "n1"}, EnumOpt{Name: "N 2", Value: "n2"})), "n2", false}, {"Invalid Schema Options", field.String().AddOptions(Schema()), invalidOptionsSchema, true}, -- GitLab