Skip to content

Instantly share code, notes, and snippets.

@bruceharrison1984
Last active September 5, 2019 03:37
Show Gist options
  • Save bruceharrison1984/6df9c14e0a2d4885f425de3d574479c2 to your computer and use it in GitHub Desktop.
Save bruceharrison1984/6df9c14e0a2d4885f425de3d574479c2 to your computer and use it in GitHub Desktop.
using System.Net.Http;
using FluentValidation;
using WidgetApi.Models;
namespace WidgetApi.FunctionHelpers
{
public class BaseValidator<T> : AbstractValidator<T> where T : ModelBase
{
public BaseValidator()
{
RuleSet(HttpMethod.Post.Method, () =>
{
RuleFor(x => x.Id).Empty()
.WithMessage($"Id field cannot be specified when POSTing a new item '{typeof(T).Name}'");
});
RuleSet(HttpMethod.Patch.Method, () =>
{
RuleFor(x => x.Id).NotEmpty();
});
RuleSet("audit", () =>
{
RuleFor(x => x.CreatedBy).Empty();
RuleFor(x => x.CreatedOn).Empty();
RuleFor(x => x.UpdatedBy).Empty();
RuleFor(x => x.UpdatedOn).Empty();
});
}
}
public class WidgetValidator : BaseValidator<Widget>
{
public WidgetValidator()
{
RuleSet(HttpMethod.Post.Method, () =>
{
RuleFor(x => x.Title).NotEmpty();
RuleFor(x => x.Description).NotEmpty();
});
}
}
public class UserValidator : BaseValidator<User> { }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment