-
Notifications
You must be signed in to change notification settings - Fork 15
Fix for issue #151 #179
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Fix for issue #151 #179
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] Consider renaming 'minLength' to 'computedMinLength' to better reflect that it aggregates values from multiple attributes, which could improve code clarity for future maintainers.
Copilot uses AI. Check for mistakes.
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); | ||
maxLength = maxLength is null ? a.MaximumLength : Math.Min(a.MaximumLength, maxLength.Value); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[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); |
Copilot uses AI. Check for mistakes.
Fixes issue #151
Also adds a check that when using
Length
attribute with other attributes likeMinLength
the minimal length cannot exceed maximum length.@roji