Skip to content

Feature/switch to system component model data annotations#132

Merged
cs-util merged 11 commits intomasterfrom
feature/switchToSystemComponentModelDataAnnotations
Jul 5, 2025
Merged

Feature/switch to system component model data annotations#132
cs-util merged 11 commits intomasterfrom
feature/switchToSystemComponentModelDataAnnotations

Conversation

@cs-util
Copy link
Collaborator

@cs-util cs-util commented Jul 5, 2025

This change is Reviewable

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Summary of Changes

Hello @cs-util, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request primarily focuses on modernizing the codebase by transitioning from custom data annotation attributes to the standard System.ComponentModel.DataAnnotations framework. This change improves code maintainability and leverages built-in .NET capabilities. Additionally, the pull request includes significant updates to Unity project files, reflecting an upgrade to a newer Unity editor version, and introduces a new Xunit mock attribute.

Highlights

  • Standardized Data Annotations: Migrated custom data annotation attributes (such as Description, Regex, MinMaxRange, Required, and InputLength) to leverage the standard System.ComponentModel.DataAnnotations namespace. This improves consistency and aligns with .NET best practices.
  • Backward Compatibility: Ensured existing custom attributes remain functional by marking them [Obsolete] and having them inherit from and wrap their System.ComponentModel.DataAnnotations counterparts, providing a smooth transition path for consumers.
  • Unity Project Upgrade: Updated Unity project settings, package versions, and logs, indicating a significant upgrade of the Unity editor version. This includes numerous changes to player settings and package dependencies.
  • New Xunit Mock Attribute: Introduced a mock Xunit.Trait attribute to facilitate testing, particularly for categorizing tests.
  • Enhanced GUID Documentation: Added detailed documentation to the GuidV2.NewGuid() method, explaining its part-random, part-sequential generation strategy, uniqueness properties, and important considerations for its use.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in issue comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist is currently in preview and may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments to provide feedback.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request refactors the codebase to use the standard System.ComponentModel.DataAnnotations attributes instead of custom-defined ones. This is a great improvement for standardization and maintainability.

I've found a few issues:

  • A critical issue where RangeAttribute is used for string length validation instead of StringLengthAttribute.
  • A high-severity issue where a minimum length constraint was dropped during the attribute migration.
  • A medium-severity issue regarding adding a new custom attribute to a system namespace.

public string id { get; private set; } = Guid.NewGuid().ToString();

[InputLength(2, 30)]
[System.ComponentModel.DataAnnotations.Range(2, 30)]

Choose a reason for hiding this comment

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

critical

The [InputLength(2, 30)] attribute on the name string property was replaced with [System.ComponentModel.DataAnnotations.Range(2, 30)]. The RangeAttribute is intended for numeric types, not for string length validation. For string length, you should use StringLengthAttribute.

            [System.ComponentModel.DataAnnotations.StringLength(30, MinimumLength = 2)]


[Content(ContentFormat.password, "Lenght >= 6 & has A-Z a-z 0-9 ?!..")]
[InputLength(min: 6)]
[System.ComponentModel.DataAnnotations.Range(minimum: 6, maximum: 100)]

Choose a reason for hiding this comment

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

critical

The [InputLength(min: 6)] attribute on the password string property was replaced with [System.ComponentModel.DataAnnotations.Range(minimum: 6, maximum: 100)]. The RangeAttribute is for numeric types and will not validate string length. You should use StringLengthAttribute instead.

            [System.ComponentModel.DataAnnotations.StringLength(100, MinimumLength = 6)]

[Required]
[InputLength(2, 30)]
[System.ComponentModel.DataAnnotations.Required]
[System.ComponentModel.DataAnnotations.StringLength(30)]

Choose a reason for hiding this comment

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

high

The original [InputLength(2, 30)] attribute specified a minimum length of 2. The new [System.ComponentModel.DataAnnotations.StringLength(30)] only specifies the maximum length, so the minimum length constraint of 2 has been lost. To preserve the original validation logic, you should also set the MinimumLength property.

            [System.ComponentModel.DataAnnotations.StringLength(30, MinimumLength = 2)]

Comment on lines +114 to +117
namespace System.ComponentModel.DataAnnotations {

[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, AllowMultiple = false)]
public class MaxWordsAttribute : RegularExpressionAttribute {

Choose a reason for hiding this comment

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

medium

The new MaxWordsAttribute is defined inside the System.ComponentModel.DataAnnotations namespace. It's generally not recommended to add custom types to system namespaces as this can lead to naming conflicts and make the code harder to understand and maintain.

cs-util added 2 commits July 5, 2025 11:59
This commit adds a new `ValidateObjectAttribute` that extends
`ValidationAttribute` to support validation of nested objects and collection
elements when using `Validator.TryValidateObject`.

Key points
----------
* Recursively invokes the Data-Annotations pipeline on referenced objects and
  (optionally) every `IEnumerable` element.
* Propagates validation errors with fully-qualified property paths
  (e.g. `bestFriend.user.name`) so MVC/Blazor can map them to UI fields.
* Handles both public properties **and** public fields, closing a gap in the
  default validator.
* Exposes `ValidateCollectionElements` switch (default: `true`).
* Sets `RequiresValidationContext = true` to preserve DI services and `Items`
  dictionary.
* Added unit tests in `JsonSchemaTests.TestModelValidation` to cover:
  – valid model,
  – invalid simple properties,
  – invalid nested objects,
  – invalid collection elements.
@cs-util cs-util merged commit 74f790c into master Jul 5, 2025
5 checks passed
@cs-util cs-util deleted the feature/switchToSystemComponentModelDataAnnotations branch July 5, 2025 10:33
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