Skip to content

Possibility to nest a validator without prefixing the name #497

Description

@fredrik-forssen-cint

If I have some objects similar to

public record OtherThing(
    Integer number
) { }

public record Thing(
    OtherThing otherThing
) { } 

And then I have validators for these similar to

public static final Validator<OtherThing> otherThingValidator = ValidatorBuilder.<OtherThing>of()
    .constraint(OtherThing::number, "number", c -> c.notNull().positive())
    .build();

public static final Validator<Thing> thingValidator = ValidatorBuilder.<Thing>of()
    .nest(Thing::otherThing, "", otherThingValidator)
    .build();

Then if I validate an object that looks like

var myThing = new Thing(new OtherThing(-1)); // This will fail due to -1 not being positive

Then the name that ends up in the constraintviolation for this will be .number. I can see why this happens since we are doing .nest, but I was hoping that since we provide "" as the name yavi would omit appending the leading . there. The outcome that I would like to have is to just get number as the name in the constraintviolation.

Is it possible to achieve this and still be able to nest the validators like this?

(Of course, in this simple case you might ask why I dont just put the number directly on the "Thing" object, that would resolve the problem. But in "real life" the objects are larger and contain more fields and I would very much like to separate the validators like this to let me reuse them easier. :) )

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions