diff --git a/diff/checks.go b/diff/checks.go index eac39e8..c054a54 100644 --- a/diff/checks.go +++ b/diff/checks.go @@ -225,7 +225,7 @@ func CheckStringTypeChanges(diffs []TypeDiff, type1, type2 *spec.SchemaProps) [] type2.Type[0] == StringType { minLengthDiffs := CompareIntValues("MinLength", type1.MinLength, type2.MinLength, NarrowedType, WidenedType) diffs = append(diffs, minLengthDiffs...) - maxLengthDiffs := CompareIntValues("MaxLength", type1.MinLength, type2.MinLength, WidenedType, NarrowedType) + maxLengthDiffs := CompareIntValues("MaxLength", type1.MaxLength, type2.MaxLength, WidenedType, NarrowedType) diffs = append(diffs, maxLengthDiffs...) if type1.Pattern != type2.Pattern { diffs = addTypeDiff(diffs, TypeDiff{Change: ChangedType, Description: fmt.Sprintf("Pattern Changed:%s->%s", type1.Pattern, type2.Pattern)}) diff --git a/diff/checks_test.go b/diff/checks_test.go index 38cf757..511ce8e 100644 --- a/diff/checks_test.go +++ b/diff/checks_test.go @@ -231,6 +231,60 @@ func Test_checkNumericTypeChanges(t *testing.T) { } } +func TestCheckStringTypeChanges(t *testing.T) { + tests := []struct { + name string + type1 *spec.SchemaProps + type2 *spec.SchemaProps + want []TypeDiff + }{ + { + name: "MaxLength added", + type1: &spec.StringProperty().SchemaProps, + type2: &spec.StringProperty().WithMaxLength(255).SchemaProps, + want: []TypeDiff{{Change: AddedConstraint, Description: "MaxLength(255)"}}, + }, + { + name: "MinLength and MaxLength added", + type1: &spec.StringProperty().SchemaProps, + type2: &spec.StringProperty().WithMinLength(1).WithMaxLength(255).SchemaProps, + want: []TypeDiff{ + {Change: AddedConstraint, Description: "MinLength(1)"}, + {Change: AddedConstraint, Description: "MaxLength(255)"}, + }, + }, + { + name: "MaxLength narrowed", + type1: &spec.StringProperty().WithMaxLength(1000).SchemaProps, + type2: &spec.StringProperty().WithMaxLength(255).SchemaProps, + want: []TypeDiff{{Change: NarrowedType, Description: "MaxLength 1000->255"}}, + }, + { + name: "MaxLength widened", + type1: &spec.StringProperty().WithMaxLength(255).SchemaProps, + type2: &spec.StringProperty().WithMaxLength(1000).SchemaProps, + want: []TypeDiff{{Change: WidenedType, Description: "MaxLength 255->1000"}}, + }, + { + name: "MaxLength removed", + type1: &spec.StringProperty().WithMaxLength(255).SchemaProps, + type2: &spec.StringProperty().SchemaProps, + want: []TypeDiff{{Change: DeletedConstraint, Description: "MaxLength(255)"}}, + }, + { + name: "MinLength changed with MaxLength unchanged", + type1: &spec.StringProperty().WithMinLength(1).WithMaxLength(255).SchemaProps, + type2: &spec.StringProperty().WithMinLength(2).WithMaxLength(255).SchemaProps, + want: []TypeDiff{{Change: NarrowedType, Description: "MinLength 1->2"}}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + assert.Equal(t, tt.want, CheckStringTypeChanges(nil, tt.type1, tt.type2)) + }) + } +} + type compareValueCase struct { name string fieldName string