Book Image

ServiceStack 4 Cookbook

Book Image

ServiceStack 4 Cookbook

Overview of this book

Table of Contents (18 chapters)
ServiceStack 4 Cookbook
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Creating static validation rules using fluent syntax


ServiceStack offers a number of ways to do validation of client requests. One of the easiest and the most powerful is the Fluent Validation syntax—we can use Language-Integrated Query (LINQ) to create expressions that describe the rules we want ServiceStack to follow when validating request DTOs. ServiceStack includes the type IRuleBuilder that has built-in things such as NotEmpty, NotEqual, LessThan, and so on, along with more advanced features, such as validating credit cards and e-mail addresses.

How to do it…

What we're going to do is add a validation for Greeting in order to show how validation works:

  1. The first thing we're going to need to do is add the ValidationFeature plugin, as follows:

    Plugins.Add(new ValidationFeature());
  2. Next, we'll create a GreetingRequestValidator class that extends the AbstractValidator<T> class. In the constructor, we'll create rules for the Greeting objects that our service processes, as follows:

    public...