mustafasturan
mustafasturan

Reputation: 557

ServiceStack Validation Feature Throws Exception

I am trying to implement validation feature in ServiceStack to validate my RequestDTO's before calling db operations. When i try to validate request dto like

ValidationResult result = this.AddBookingLimitValidator.Validate(request);

the code automatically throws a validation error automatically. I can not even debug service what is happening behind the scenes ? Can i change that behaviour or am i doing something wrong here. Thanks.

My Request DTO :

[Route("/bookinglimit", "POST")]
[Authenticate]
public class AddBookingLimit : IReturn<AddBookingLimitResponse>
{       
  public int ShiftId { get; set; }
  public DateTime Date { get; set; }
  public int Limit { get; set; }
}

My Response DTO :

public class AddBookingLimitResponse
{
  public int Id { get; set; }
  public ResponseStatus ResponseStatus { get; set; }
}

Validation class :

public class AddBookingLimitValidator : AbstractValidator<AddBookingLimit>
{
  public AddBookingLimitValidator()
  {
    RuleFor(r => r.Limit).GreaterThan(0).WithMessage("Limit 0 dan büyük olmalıdır");
  }
}

Service Implementation :

public AddBookingLimitResponse Post(AddBookingLimit request)
{
  ValidationResult result = this.AddBookingLimitValidator.Validate(request);          

  Shift shift = new ShiftRepository().Get(request.ShiftId);
  BookingLimit bookingLimit = new BookingLimit
  {
    RestaurantId = base.UserSession.RestaurantId,
    ShiftId = request.ShiftId,
    StartDate = request.Date.AddHours(shift.StartHour.Hour).AddMinutes(shift.StartHour.Minute),
    EndDate = request.Date.AddHours(shift.EndHour.Hour).AddMinutes(shift.EndHour.Minute),
    Limit = request.Limit,
    CreateDate = DateTime.Now,
    CreatedBy = base.UserSession.UserId,
    Status = (byte)Status.Active
  };
  return new AddBookingLimitResponse
  {
    Id = new BookingLimitRepository().Add(bookingLimit)
  };
        }

AppHost code :

container.RegisterValidators(typeof(AddBookingLimitValidator).Assembly);
Plugins.Add(new ValidationFeature());

And i consume the service in c# code:

 try
 {
   AddBookingLimitResponse response = ClientHelper.JsonClient.Post(new AddBookingLimit
   {
     Date = DateTime.Parse(DailyBookingLimitDateTextBox.Text),
     Limit = Convert.ToInt32(DailyBookingLimitTextBox.Text),
     ShiftId = Convert.ToInt32(DailyDayTypeSelection.SelectedValue)
    });
    WebManager.ShowMessage(UserMessages.SaveSuccessful.FormatString(Fields.BookingLimit));
  }
  catch (WebServiceException ex)
  {
    WebManager.ShowMessage(ex.ResponseStatus.Message);           
  }

Upvotes: 1

Views: 634

Answers (1)

Arxisos
Arxisos

Reputation: 2729

Right, ServiceStack validates the request DTO before the service gets called if the ValidationFeature is enabled.

To manually invoke the validator in the service, you have to remove this line from your AppHost first:

Plugins.Add(new ValidationFeature());

Please make sure that the validator property in your service has the type IValidator<>, otherwise it won't be injected by the IoC container if you register your validators with container.RegisterValidators(typeof(AddBookingLimitValidator).Assembly).

public class TestService : Service
{
    public IValidator<Request> Validator { get; set; }

    public RequestResponse Post(Request request)
    {
        Validator.Validate(request); 
        ...
    }
}

Upvotes: 2

Related Questions