diff --git a/README.md b/README.md index e270667..5945994 100644 --- a/README.md +++ b/README.md @@ -122,8 +122,8 @@ While not finished, go-jsonschema can be used today. Aside from some minor featu - [ ] `minimum` - [ ] `exclusiveMinimum` - [ ] String validation (§6.3) - - [ ] `maxLength` - - [ ] `minLength` + - [X] `maxLength` + - [X] `minLength` - [ ] `pattern` - [ ] Array validation (§6.4) - [X] `items` diff --git a/go.work.sum b/go.work.sum index 0c3fe38..80a53f3 100644 --- a/go.work.sum +++ b/go.work.sum @@ -1,6 +1,8 @@ github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= +golang.org/x/mod v0.13.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= golang.org/x/net v0.15.0/go.mod h1:idbUs1IY1+zTqbi8yxTbhexhEEk5ur9LInksu6HrEpk= golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y= golang.org/x/tools v0.13.0/go.mod h1:HvlwmtVNQAhOuCjW7xxvovg8wbNq7LwfXh/k7wXUl58= +golang.org/x/tools v0.14.0/go.mod h1:uYBEerGOWcJyEORxN+Ek8+TT266gXkNlHdJBwexUsBg= diff --git a/pkg/generator/validator.go b/pkg/generator/validator.go index b90263f..bd8d1fb 100644 --- a/pkg/generator/validator.go +++ b/pkg/generator/validator.go @@ -244,7 +244,7 @@ func (v *stringValidator) generate(out *codegen.Emitter) { } if v.maxLength != 0 { - out.Printlnf(`if %slen(%s%s) >= %d {`, checkPointer, pointerPrefix, value, v.maxLength) + out.Printlnf(`if %slen(%s%s) > %d {`, checkPointer, pointerPrefix, value, v.maxLength) out.Indent(1) out.Printlnf(`return fmt.Errorf("field %%s length: must be <= %%d", "%s", %d)`, fieldName, v.maxLength) out.Indent(-1) diff --git a/tests/data/validation/maxLength/maxLength.go b/tests/data/validation/maxLength/maxLength.go index 2538e9b..92cf124 100644 --- a/tests/data/validation/maxLength/maxLength.go +++ b/tests/data/validation/maxLength/maxLength.go @@ -27,10 +27,10 @@ func (j *MaxLength) UnmarshalJSON(b []byte) error { if err := json.Unmarshal(b, &plain); err != nil { return err } - if plain.MyNullableString != nil && len(*plain.MyNullableString) >= 10 { + if plain.MyNullableString != nil && len(*plain.MyNullableString) > 10 { return fmt.Errorf("field %s length: must be <= %d", "myNullableString", 10) } - if len(plain.MyString) >= 5 { + if len(plain.MyString) > 5 { return fmt.Errorf("field %s length: must be <= %d", "myString", 5) } *j = MaxLength(plain) diff --git a/tests/validation_test.go b/tests/validation_test.go index 1ba5121..d1f01d5 100644 --- a/tests/validation_test.go +++ b/tests/validation_test.go @@ -24,9 +24,9 @@ func TestMaxStringLength(t *testing.T) { wantErr: nil, }, { - desc: "myString too long", + desc: "myString has the max allowed length", data: `{"myString": "hello"}`, - wantErr: errors.New("field myString length: must be <= 5"), + wantErr: nil, }, { desc: "myString not present",