Last active
June 30, 2021 07:14
-
-
Save gistlyn/a1e8cc8d14f234bb9c5f8b15862ea631 to your computer and use it in GitHub Desktop.
validation
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
public class AppHost : AppHostBase | |
{ | |
public AppHost() : base("Web",typeof(MyServices).Assembly){} | |
public override void Configure(Container container) | |
{ | |
Plugins.Add(new ValidationFeature()); | |
// Register custom validator | |
container.Register<IAddressValidator>( | |
new AddressValidator()); | |
} | |
} |
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
public class MyServices : Service | |
{ | |
public object Post(CreateUser request) | |
{ | |
var id = Db.Insert(request.ConvertTo<User>()); | |
var user = Db.SingleById<User>(id); | |
return user; | |
} | |
} |
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
[Route("/users", "POST")] | |
public class CreateUser : IReturn<User> | |
{ | |
public string Name { get; set; } | |
public string Company { get; set; } | |
public int Age { get; set; } | |
public int Count { get; set; } | |
public string Address { get; set; } | |
} | |
public class User | |
{ | |
[AutoIncrement] | |
public int Id { get; set; } | |
public string Name { get; set; } | |
public string Company { get; set; } | |
public int Age { get; set; } | |
public int Count { get; set; } | |
public string Address { get; set; } | |
} | |
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
public interface IAddressValidator | |
{ | |
bool ValidAddress(string address); | |
} | |
public class AddressValidator : IAddressValidator | |
{ | |
public bool ValidAddress(string address) | |
{ | |
return address != null | |
&& address.Length >= 20 | |
&& address.Length <= 250; | |
} | |
} | |
public class UserValidator : AbstractValidator<CreateUser> | |
{ | |
// Resolved from IoC | |
public IAddressValidator AddressValidator { get; set; } | |
public UserValidator() | |
{ | |
//Validation rules for all requests | |
RuleFor(r => r.Name).NotEmpty(); | |
RuleFor(r => r.Age).GreaterThan(0); | |
RuleFor(x => x.Address).Must(x => | |
AddressValidator.ValidAddress(x)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment