Skip to content
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

QW0101: Required attribute cannot invalidate value types #58

Merged
merged 2 commits into from
Mar 24, 2025
Merged

Conversation

Corniel
Copy link
Contributor

@Corniel Corniel commented Mar 23, 2025

The [Required] attribute checks if the value is null. This is always true for value types except for Nullable<T>. The use of the [Required] attribute is most likely not doing what might be expected, and should be prevented.

Non-compliant

class Model
{
    [Required]
    public int Number { get; init; }
}

Compliant

If the default value was is not allowed:

class Model
{
    [Qowaiv.Valdation.DataAnnotations.Mandatory]
    public int Number { get; init; }
}

If not set should be able possible (but is invalid):

class Model
{
    [Required]
    public int? Number { get; init; }
}

If it must only be assured that the value is set in code.

class Model
{
    public required int Number { get; init; }
}

@Corniel Corniel added the new rule New Rule label Mar 23, 2025
@Corniel Corniel requested a review from LauraXiulan March 23, 2025 15:33
@Corniel Corniel self-assigned this Mar 23, 2025
Copy link
Contributor

@LauraXiulan LauraXiulan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See comments

@Corniel Corniel merged commit 52a998c into main Mar 24, 2025
1 check passed
@Corniel Corniel deleted the qw0101 branch March 24, 2025 09:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new rule New Rule
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants