Skip to content

A Roslyn analyzer that helps enforce string length constraints in your C# code.

License

Notifications You must be signed in to change notification settings

TarasKovalenko/EntityLengths.Analyzer

Repository files navigation

EntityLengths.Analyzer

Made in Ukraine build EntityLengths.Generator NuGet current

Goals

A Roslyn analyzer that helps enforce string length constraints in your C# code. It analyzes property assignments and verifies they comply with length restrictions defined through attributes or Entity Framework configurations.

Terms of use

By using this project or its source code, for any purpose and in any shape or form, you grant your implicit agreement to all of the following statements:

  • You unequivocally condemn Russia and its military aggression against Ukraine
  • You recognize that Russia is an occupant that unlawfully invaded a sovereign state
  • You agree that Russia is a terrorist state
  • You fully support Ukraine's territorial integrity, including its claims over temporarily occupied territories
  • You reject false narratives perpetuated by Russian state propaganda

To learn more about the war and how you can help, click here. Glory to Ukraine! 🇺🇦

Features

  • Detects potential string length violations at compile time
  • Supports multiple ways of defining length constraints:
    • EF Core Fluent API configurations (HasMaxLength)
    • Data Annotations
      • [MaxLength]
      • [StringLength]
    • Column type definitions
      • [Column(TypeName = "varchar(200)")]
      • [Column(TypeName = "nvarchar(200)")]
      • [Column(TypeName = "char(200)")]

Installation

Install the library via NuGet Package Manager:

dotnet add package EntityLengths.Analyzer

Usage

Using Attributes

public class User
{
    [MaxLength(50)]
    public string Name { get; set; }  // Will show info/error if assigned string > 50 chars

    [StringLength(100)]
    public string Description { get; set; }  // Will show info/error if assigned string > 100 chars

    [Column(TypeName = "varchar(200)")]
    public string Url { get; set; }  // Will show info/error if assigned string > 200 chars
}

Using Entity Framework Fluent API

public class UserConfiguration : IEntityTypeConfiguration<User>
{
    public void Configure(EntityTypeBuilder<User> builder)
    {
        builder.Property(p => p.Name)
            .HasMaxLength(50);  // Will show warning/info if assigned string > 50 chars
    }
}

// Or in DbContext
public class UserDbContext : DbContext
{
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<User>()
            .Property(b => b.Name)
            .HasMaxLength(50);
    }
}

Diagnostics

The analyzer provides two types of diagnostics:

  • ML001 (Info): Warns about potential length violations when assigning non-literal values
[MaxLength(5)]
public string Name { get; set; }

string value = GetValue();
Name = value;  // ML001: Property 'Name' has a maximum length of 5. Consider adding length validation.
  • ML002 (Error): Reports definite length violations with string literals
[MaxLength(5)]
public string Name { get; set; }

Name = "This is too long";  // ML002: String length (14) exceeds maximum length of 5 for property 'Name'

About

A Roslyn analyzer that helps enforce string length constraints in your C# code.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages