Conversation
There was a problem hiding this comment.
Pull Request Overview
This PR fixes issue #151 by adding a validation check to ensure that when using the Length attribute with additional attributes (MinLength, StringLength, and Required), the minimum length does not exceed the maximum length. It updates the constraint convention logic to aggregate minimum and maximum length values and adds thorough tests for various scenarios.
- Refactored minimum/maximum length aggregation logic in the convention class.
- Added unit tests for required and length attributes, including a test for exception throwing when the minimal length exceeds the maximum.
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| EFCore.CheckConstraints/Internal/ValidationCheckConstraintConvention.cs | Updated length constraint logic to aggregate attribute values and throw an exception if minimal length exceeds maximum. |
| EFCore.CheckConstraints.Test/ValidationCheckConstraintTest.cs | Added tests to verify that the combined constraints behave as expected and throw an error when invalid. |
| minLength = minLength is null ? a.Length : Math.Max(a.Length, minLength.Value); | ||
| continue; | ||
|
|
||
| case StringLengthAttribute a when _intTypeMapping is not null: | ||
| AddMinimumLengthConstraint(property, memberInfo, tableName, columnName, sql, a.MinimumLength); | ||
| minLength = minLength is null ? a.MinimumLength : Math.Max(a.MinimumLength, minLength.Value); | ||
| continue; | ||
|
|
||
| case RequiredAttribute { AllowEmptyStrings: false } when _intTypeMapping is not null: | ||
| AddMinimumLengthConstraint(property, memberInfo, tableName, columnName, sql, minLength: 1); | ||
| case RequiredAttribute { AllowEmptyStrings: false } | ||
| when _intTypeMapping is not null && memberInfo.GetMemberType() == typeof(string): | ||
| minLength = minLength is null ? 1 : Math.Max(1, minLength.Value); | ||
| continue; | ||
|
|
||
| case LengthAttribute a when _intTypeMapping is not null: | ||
| // Note: The max length should be enforced by the column schema definition in EF, | ||
| // see https://github.com/dotnet/efcore/issues/30754. While that isn't done, we enforce it via the check | ||
| // constraint. | ||
| AddStringLengthConstraint(property, memberInfo, tableName, columnName, sql, a.MinimumLength, a.MaximumLength); | ||
| minLength = minLength is null ? a.MinimumLength : Math.Max(a.MinimumLength, minLength.Value); |
There was a problem hiding this comment.
[nitpick] Consider renaming 'minLength' to 'computedMinLength' to better reflect that it aggregates values from multiple attributes, which could improve code clarity for future maintainers.
| // constraint. | ||
| AddStringLengthConstraint(property, memberInfo, tableName, columnName, sql, a.MinimumLength, a.MaximumLength); | ||
| minLength = minLength is null ? a.MinimumLength : Math.Max(a.MinimumLength, minLength.Value); | ||
| maxLength = maxLength is null ? a.MaximumLength : Math.Min(a.MaximumLength, maxLength.Value); |
There was a problem hiding this comment.
[nitpick] Consider renaming 'maxLength' to 'computedMaxLength' to clearly convey its role in aggregating maximum length limits from multiple attributes.
| maxLength = maxLength is null ? a.MaximumLength : Math.Min(a.MaximumLength, maxLength.Value); | |
| computedMaxLength = computedMaxLength is null ? a.MaximumLength : Math.Min(a.MaximumLength, computedMaxLength.Value); |
Fixes issue #151
Also adds a check that when using
Lengthattribute with other attributes likeMinLengththe minimal length cannot exceed maximum length.@roji