Skip to content

An attempt to automatically output the FluentValidation rule sets for a given project into a human readable form.

License

Notifications You must be signed in to change notification settings

JamieKeeling/ValiDoc

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ValiDoc Build Status

An unobtrusive extension to FluentValidations for documenting validation rules.

Usage

  1. Install ValiDoc via NuGet
Install-Package ValiDoc -Pre
  1. Configure Dependency Injection
public void ConfigureServices(IServiceCollection services)
{
    // Add ValiDoc dependencies
    services.AddValiDoc();
}
  1. Reference ValiDoc namespace
using ValiDoc;
  1. Constructor Inject the RuleDescriptor
private readonly IRuleBuilder _ruleBuilder;

public ValidationController(IRuleBuilder ruleBuilder)
{
    _ruleBuilder = ruleBuilder;
}
  1. Create an instance of DocBuilder that will be used to build the documentation
var docBuilder = new DocBuilder(_ruleBuilder);
  1. Invoke the .Document() method on the DocBuilder, passing in an implementation of AbstractValidator<T>
var ruleDocumentation = docBuilder.Document(myValidatorInstance);

Example

Implementation of AbstractValidator<T>
public class MultipleRuleSingleChildValidator : AbstractValidator<Person>
{
	public MultipleRuleSingleChildValidator()
	{
	    RuleFor(p => p.FirstName).NotEmpty();
	    RuleFor(p => p.LastName).NotEmpty().MaximumLength(20);
        RuleFor(p => p.Address).SetValidator(new AddressValidator());
	}
}
Dependency Injection and Invocation
[Route("api/[controller]")]
public class ValidationController : Controller
{
    private readonly IRuleBuilder _ruleBuilder;

    public ValidationController(IRuleBuilder ruleBuilder)
    {
        _ruleBuilder = ruleBuilder;
    }

    [HttpGet]
    public IEnumerable<RuleDescriptor> ValidatorDocumentation()
    {
        var validator = new MultipleRuleSingleChildValidator();

        var docBuilder = new ValiDoc.DocBuilder(_ruleBuilder);

        return docBuilder.Document(validator);
    }
}
Output
[
    {
        "memberName": "First Name",
        "rules": [
            {
                "validatorName": "NotEmptyValidator",
                "failureSeverity": "Error",
                "onFailure": "Continue",
                "validationMessage": "'First Name' should not be empty."
            }
        ]
    },
    {
        "memberName": "Last Name",
        "rules": [
            {
                "validatorName": "NotEmptyValidator",
                "failureSeverity": "Error",
                "onFailure": "Continue",
                "validationMessage": "'Last Name' should not be empty."
            },
            {
                "validatorName": "MaximumLengthValidator",
                "failureSeverity": "Error",
                "onFailure": "Continue",
                "validationMessage": "'Last Name' must be less than {MaxLength} characters. You entered {TotalLength} characters."
            }
        ]
    },
    {
        "memberName": "Address",
        "rules": [
            {
                "validatorName": "AddressValidator",
                "failureSeverity": "Error",
                "onFailure": "Continue",
                "validationMessage": "N/A - Refer to specific AddressValidator documentation"
            }
        ]
    }
]

Supported Scenarios

  1. Extraction of validation messages (Configured argument values not included)
  2. Built in validators
  3. Chained validators for the same property
  4. Complex properties
  5. Cascade behaviour

Future Roadmap

  1. Collections
  2. RuleSets
  3. .WithMessage()
  4. .WithName()
  5. Validation arguments (Greater than 10 characters for example)
  6. Custom validators
  7. .Must()

About

An attempt to automatically output the FluentValidation rule sets for a given project into a human readable form.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages