Skip to content

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

iamjustanotherrandom
Copy link

Fixes issue #151
Also adds a check that when using Length attribute with other attributes like MinLength the minimal length cannot exceed maximum length.

@roji

@ErikEJ ErikEJ requested review from roji and Copilot June 11, 2025 05:57
Copy link

@Copilot Copilot AI left a 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.

Comment on lines +95 to +111
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);
Copy link
Preview

Copilot AI Jun 11, 2025

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);
Copy link
Preview

Copilot AI Jun 11, 2025

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.

Suggested change
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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant