Abp validation

From burakdmir/abp-skills · View interactive version

Abp validation is an AI agent skill from the GitHub repository burakdmir/abp-skills. ABP Framework v10.x (10.4–10.6) validation: DTO validation, Data Annotations, FluentValidation, IValidatableObject, AbpValidationException. Use when you need input validation or DTO validation in ABP.

How to use this skill

Skill file

ABP Validation Skill

Trigger

Validation, DTO validation, FluentValidation, IValidatableObject, validation errors, AbpValidationException, input validation.


Quick Reference

Data Annotations

public class CreateBookDto
{
    [Required] [StringLength(100)] public string Name { get; set; }
    [Range(0, 999.99)] public decimal Price { get; set; }
}

Auto-validated on app service/controller call.

IValidatableObject

public class CreateBookDto : IValidatableObject
{
    public IEnumerable<ValidationResult> Validate(ValidationContext ctx)
    {
        var results = new List<ValidationResult>();
        if (Price <= 0) results.Add(new ValidationResult("Price > 0", new[] { nameof(Price) }));
        return results;
    }
}

Resolve services: ctx.GetRequiredService<IMyService>()

FluentValidation

abp add-package Volo.Abp.FluentValidation
public class CreateBookDtoValidator : AbstractValidator<CreateBookDto>
{
    public CreateBookDtoValidator()
    {
        RuleFor(x => x.Name).Length(3, 10);
        RuleFor(x => x.Price).ExclusiveBetween(0.0f, 999.0f);
    }
}

Auto-discovered, associated with DTO type.

IValidationEnabled

public class MyService : ITransientDependency, IValidationEnabled
{
    public virtual async Task DoItAsync(MyInput input) { }
}

Method must be virtual or use interface injection.

Validation Error Response

{
  "error": {
    "code": "App:010046",
    "message": "Your request is not valid, please correct and try again!",
    "validationErrors": [
      { "message": "Username min length 3.", "members": ["userName"] }
    ]
  }
}

Best Practices

Related

DDD · Exception Handling · Localization · Docs: https://abp.io/docs/latest/framework/fundamentals/validation

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.