Last active
September 5, 2019 03:37
-
-
Save bruceharrison1984/6df9c14e0a2d4885f425de3d574479c2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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