user2958608
user2958608

Reputation: 71

How to override ServiceStack RegistrationService Validator?

How to override ServiceStack RegistrationService Validator and add some new rules to it?

And what needs to be done to intercept the UserAuthService validation?

Here is the AppHost Config:

  Plugins.Add(new CorsFeature()); //Registers global CORS Headers

  RequestFilters.Add((httpReq, httpRes, requestDto) =>
  {
    // Handles Request and closes Responses after emitting global HTTP Headers
    if (httpReq.HttpMethod == "OPTIONS")
      httpRes.EndRequest();
  });

  // Enable the validation feature
  Plugins.Add(new ValidationFeature());

  // This method scans the assembly for validators
  container.RegisterValidators(typeof(AppHost).Assembly);

  container.Register<ICacheClient>(new MemoryCacheClient());

  //var dbFactory = new OrmLiteConnectionFactory(connectionString, SqlServerDialect.Provider);
  var dbFactory = new OrmLiteConnectionFactory(connectionString, SqliteDialect.Provider);

  container.Register<IDbConnectionFactory>(dbFactory);

  // Enable Authentication
  Plugins.Add(new AuthFeature(() => new CustomUserSession(),
    new IAuthProvider[] {
            new CustomAuthProvider(), 
        }));

  // Provide service for new users to register so they can login with supplied credentials.
  Plugins.Add(new RegistrationFeature());

  // Override the default registration validation with your own custom implementation
  container.RegisterAs<CustomRegistrationValidator, IValidator<Registration>>();

  container.Register<IUserAuthRepository>(c => new CustomAuthRepository(c.Resolve<IDbConnectionFactory>()));

Upvotes: 4

Views: 544

Answers (1)

Darren Reid
Darren Reid

Reputation: 2322

ServiceStack validators are pretty easy to use. The 'SocialBootstrap' example shows how to use custom validators for registration in its AppHost.cs.

//Provide extra validation for the registration process
public class CustomRegistrationValidator : RegistrationValidator
{
    public CustomRegistrationValidator()
    {
        RuleSet(ApplyTo.Post, () => {
            RuleFor(x => x.DisplayName).NotEmpty();
        });
    }
}

Remember to register your custom validator as well.

//override the default registration validation with your own custom implementation
container.RegisterAs<CustomRegistrationValidator, IValidator<Registration>>();

Add more rules by using 'RuleSet'. Hope that helps.

EDIT It seems there might be a bug in the current v3 version of ServiceStack that is preventing the validator from being called. I did a quick test with the Social Bootstrap project and could reproduce what you are experiencing, eg the CustomRegistrationValidator not firing its rules. Other validators seem to be working fine, so not sure what the cause might be at the moment. I will pull down the source to debug when I get time. If you happen to do it before hand, please post up what you find as it might help others.

Update This problem is due to the order of ops for plugins and registration. The Registration plugin is running it's Register function after the CustomRegistrationValidator has been registered and overrides the type registered as IValidator<Registration>.

Simplest way around this is to creator your own RegistrationFeature as it is pretty simple in itself.

public class MyRegistrationFeature : IPlugin
{
    public string AtRestPath { get; set; }

    public RegistrationFeature()
    {
        this.AtRestPath = "/register";
    }

    public void Register(IAppHost appHost)
    {
        appHost.RegisterService<RegisterService>(AtRestPath);
        appHost.RegisterAs<CustomRegistrationValidator, IValidator<Registration>>();
    }
}

Upvotes: 4

Related Questions