Reputation: 324
I am using ServiceStack (with the new API) and trying to validate a DTO. Just created some simple code to simulate a validation but it's apparently not firing or at least it's not showing the errors on the response as expected. My code is as follows:
DTO:
[Route("/users/login")]
public class UserLogin
{
public string Email { get; set; }
public string Password { get; set; }
}
The validator itself:
public class UserLoginValidator : AbstractValidator<UserLogin>
{
public UserLoginValidator()
{
RuleSet(ApplyTo.Get, () =>
{
RuleFor(x => x.Email).NotEmpty().WithMessage("Please enter your e-mail.");
RuleFor(x => x.Email).EmailAddress().WithMessage("Invalid e-mail.");
RuleFor(x => x.Password).NotEmpty().WithMessage("Please enter your password.");
});
}
}
Configuring the validation in the host:
Plugins.Add(new ValidationFeature());
container.RegisterValidators(typeof(UserLoginValidator).Assembly);
And the service:
public class LoginService : Service
{
public object Get(UserLogin request)
{
var response = new { SessionId = Guid.NewGuid() };
return response;
}
}
Is there any other config or adjustment that needs to be made to make it work?
Thanks!
Upvotes: 3
Views: 563
Reputation: 1127
From the documentation
Note: The response DTO must follow the {Request DTO}Response naming convention and has to be in the same namespace as the request DTO!
Try creating a class for the response
public class UserLoginResponse
{
public UserLogin Result { get; set; }
}
And return it
public class LoginService : Service
{
public object Get(UserLogin request)
{
var response = new UserLoginResponse { Result = request };
return response;
}
}
Upvotes: 2