Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More than one validator was found for the request dto [Project.Request]. Specify the exact validator to register using the Validator() method in endpoint configuration. #394

Closed
sandelirious7 opened this issue Mar 1, 2023 · 1 comment
Labels
bug Something isn't working fixed the bug has been squashed

Comments

@sandelirious7
Copy link

sandelirious7 commented Mar 1, 2023

Though I've explicitly mentioned the validator in my Endpoint, I still see this error in 5.7.0

CreateEndpoint.cs
public override void Configure() { Verbs(Http.POST); Routes("/streaming"); AllowAnonymous(); Description( req => req .Accepts<Request>("application/json") .Produces((int)HttpStatusCode.Accepted) .Produces<ErrorResponse>((int)HttpStatusCode.BadRequest, "application/json+problem") .Produces<ErrorResponse>((int)HttpStatusCode.NotFound, "application/json+problem"), clearDefaults: true ); Validator<CreateRequestValidator>(); }

UpdateEndpoint.cs
public override void Configure() { Verbs(Http.PATCH); Routes("/streaming"); AllowAnonymous(); Description( req => req .Accepts<Request>("application/json") .Produces((int)HttpStatusCode.Accepted) .Produces<ErrorResponse>((int)HttpStatusCode.BadRequest, "application/json+problem") .Produces<ErrorResponse>((int)HttpStatusCode.NotFound, "application/json+problem"), clearDefaults: true ); Validator<UpdateRequestValidator>(); }

`public class CreateStreamingDrupalAssetsRequestValidator : Validator
{
public CreateRequestValidator()
{
RuleFor(req => req.Doc.Name)
.NotNull()
.NotEmpty()
.WithMessage("Name is required");
}
}

public class UpdateRequestValidator : CreateRequestValidator
{
public UpdateStreamingDrupalAssetsRequestValidator() : base()
{
RuleFor(req => req.Doc.Uuid)
.NotNull()
.NotEmpty()
.WithMessage("UUID is required");
}
}`

@dj-nitehawk dj-nitehawk added bug Something isn't working fixed the bug has been squashed labels Mar 2, 2023
@dj-nitehawk
Copy link
Member

pls upgrade to 5.7.2.12-beta
and do validator inheritance like shown below:

public class Update : Endpoint<Request>
{
    public override void Configure()
    {
        Post("/streaming");
        AllowAnonymous();
        Description(x => x
            .ProducesProblemFE(400)
            .ProducesProblemFE(404));
        Validator<UpdateRequestValidator>();
    }

    public override async Task HandleAsync(Request req, CancellationToken ct)
    {
        await SendAsync(req);
    }
}

public class CreateRequestValidator : Validator<Request>
{
    public CreateRequestValidator()
    {
        RuleFor(req => req.Doc.Name)
            .NotEmpty().WithMessage("Name is required");
    }
}

public class UpdateRequestValidator : Validator<Request>
{
    public UpdateRequestValidator()
    {
        Include(new CreateRequestValidator()); //this is how you inherit rules written in other validators
        RuleFor(req => req.Doc.Uuid)
            .NotEmpty().WithMessage("UUID is required");
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fixed the bug has been squashed
Development

No branches or pull requests

2 participants