Skip to content

Commit

Permalink
Only perform minItems validation when field is provided
Browse files Browse the repository at this point in the history
  • Loading branch information
nick-jones authored and omissis committed Sep 28, 2023
1 parent 0f951fd commit 8c546e6
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 7 deletions.
2 changes: 1 addition & 1 deletion pkg/generator/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ func (v *arrayValidator) generate(out *codegen.Emitter) {
}

if v.minItems != 0 {
out.Printlnf(`if len(%s) < %d {`, value, v.minItems)
out.Printlnf(`if %s != nil && len(%s) < %d {`, value, value, v.minItems)
out.Indent(1)
out.Printlnf(`return fmt.Errorf("field %%s length: must be >= %%d", %s, %d)`, fieldName, v.minItems)
out.Indent(-1)
Expand Down
6 changes: 3 additions & 3 deletions tests/data/validation/5.11_minItems.go.output
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ func (j *A511MinItems) UnmarshalJSON(b []byte) error {
if err := json.Unmarshal(b, &plain); err != nil {
return err
}
if len(plain.MyNestedArray) < 5 {
if plain.MyNestedArray != nil && len(plain.MyNestedArray) < 5 {
return fmt.Errorf("field %s length: must be >= %d", "myNestedArray", 5)
}
for i1 := range plain.MyNestedArray {
if len(plain.MyNestedArray[i1]) < 5 {
if plain.MyNestedArray[i1] != nil && len(plain.MyNestedArray[i1]) < 5 {
return fmt.Errorf("field %s length: must be >= %d", fmt.Sprintf("myNestedArray[%d]", i1), 5)
}
}
if len(plain.MyStringArray) < 5 {
if plain.MyStringArray != nil && len(plain.MyStringArray) < 5 {
return fmt.Errorf("field %s length: must be >= %d", "myStringArray", 5)
}
*j = A511MinItems(plain)
Expand Down
6 changes: 3 additions & 3 deletions tests/data/validation/5.1x_minMaxItems.go.output
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,21 @@ func (j *A51XMinMaxItems) UnmarshalJSON(b []byte) error {
if err := json.Unmarshal(b, &plain); err != nil {
return err
}
if len(plain.MyNestedArray) < 1 {
if plain.MyNestedArray != nil && len(plain.MyNestedArray) < 1 {
return fmt.Errorf("field %s length: must be >= %d", "myNestedArray", 1)
}
if len(plain.MyNestedArray) > 5 {
return fmt.Errorf("field %s length: must be <= %d", "myNestedArray", 5)
}
for i1 := range plain.MyNestedArray {
if len(plain.MyNestedArray[i1]) < 1 {
if plain.MyNestedArray[i1] != nil && len(plain.MyNestedArray[i1]) < 1 {
return fmt.Errorf("field %s length: must be >= %d", fmt.Sprintf("myNestedArray[%d]", i1), 1)
}
if len(plain.MyNestedArray[i1]) > 5 {
return fmt.Errorf("field %s length: must be <= %d", fmt.Sprintf("myNestedArray[%d]", i1), 5)
}
}
if len(plain.MyStringArray) < 1 {
if plain.MyStringArray != nil && len(plain.MyStringArray) < 1 {
return fmt.Errorf("field %s length: must be >= %d", "myStringArray", 1)
}
if len(plain.MyStringArray) > 3 {
Expand Down

0 comments on commit 8c546e6

Please sign in to comment.