Using ServiceStack.FluentValidation with Blazor WASM?

Previously, for TypeScript/Vue development, I put my service validators classes next to the my service definition class, inside my ServiceModel:

ServiceModel\
  BankAccount.cs
  BankAccountValidator.cs

As I used generated TypeScript DTOs, I never had issues having a dependency on the ServiceStack.dll.

For Blazor WASM, I understand that my ServiceModel must only reference ServiceStack.Interface.DLL to avoid dependencies that are incompatible with WASM (such as ServiceStack.DLL). I learnt this from Blazor wasm clash with ServiceStack.Script

My question is how can I use ServiceStack.FluentValidation in a way that is compatible with Blazor WASM, because it is not available via ServiceStack.Interfaces.

Should I simply relocate my service validators classes into the ServiceInterface layer?

Hi @Keith,

It is best that your ServiceModel project has as few dependencies as possible. If you are declaring your custom validator classes, they can live in your ServiceInterface project as you’ve suggested. The ServiceModel project should only contain the minimum amount of info needed for client integration, so putting your validators with your service implementation (ServiceInterface) will avoid this problem :+1:

1 Like

Hello @layoric,

that’s great, but how is the custom validator class associated with the request DTO class when this are in different projects?

When you declare a validator using the AbstractValidator<T>, the T is usually your Request DTO. Eg

public class MyRequestValidator : AbstractValidator<MyRequest>
{
    public MyRequestValidator()
    {
        RuleFor(x => x.Name).NotEmpty();
    }
}

If this isn’t applicable to you, could you please provide a concrete example of your validator and issue you are having with them living in your ServiceInterface project?

I do exactly what you describe, so it looks straightforward.

Worked perfectly.

Thank you for your support.

1 Like