There's path traversal API (see https://docs.jboss.org/hibernate/stable/validator/reference/en-US/html_single/#section-validator-factory-traversable-resolver). Perhaps that could be of use to you?
You might also want to look into the constraint violation api. As far as I remember (atleast in hibernate's implementation) you can get a reference to the failing value. Double check if you can get a reference to the field instance so you could reflect the jackson annotations. On Tue, Jul 26, 2022, 00:15 Tatu Saloranta <[email protected]> wrote: > On Tue, Jul 5, 2022 at 11:34 AM João Cabrita <[email protected]> > wrote: > > > > Hi, > > > > I've been working on a module to integrate Bean Validation with Jackson: > https://github.com/kewne/jackson-validation > > > > The basic ideas are: > > 1. Without integrating validation into `@JsonCreator` calls, you can't > then enforce invariants in class instances, e.g. this.name = > Objects.requireNonNull(name); > > with validation support, you can annotate params with @NotNull. > > 2. Bean Validation inspects the class and knows nothing about Jackson, > so it generates paths using the Java definition; > > with this module , you would be able to declare @JsonProperty("abc") > String name and get violation paths with the correct names (instead of > "name"). > > > > My approach so far has been to create my own value instantiator and > perform validations before and after instantiation, but this fails because, > if an object deep in the tree fails its validation, I don't know how to > catch it so it shows up properly in violations tree: > > > > class Root { > > private Nested nested; > > } > > > > class Nested { > > @NotNull private String name; > > } > > > > ObjectMapper.readValue(""" > > { > > "nested": {} > > } > > """, Root.class); //throws validation exception with Nested as root... > > > > Any hints on how I might handle the nesting? > > > > Thanks! > > It seems to me that the options really come down to 2 approaches for > combining Jackson and Bean Validation API: > > 1. Run them separately: Jackson binds data first, Validator then > validates the result. Less integrated so error messages do not tie > back to input offsets. > 2. Implement custom version of Bean Validation API Validator that > somehow integrates into Jackson's handling > > So far I have assumed that (1) is a reasonable path as although in > theory (2) could provide more meaningful information, the effort to > make that actually work well seems very high. > At least for the general case. Perhaps it would be more practical if > it's ok to modify POJOs to participate in the validation process. > > As to nesting, the problem really is that your POJOs will not get any > chance to participate in traversal, or have access to context. > > So unfortunately I can't think of much to help you here. But maybe others > do. > > Good luck! > > -+ Tatu +- > > -- > You received this message because you are subscribed to the Google Groups > "jackson-user" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > To view this discussion on the web visit > https://groups.google.com/d/msgid/jackson-user/CAL4a10jCNsUNaWAw3n8skGdQM%2Bd%2BarfsmeEyPqEuaMswY79KEg%40mail.gmail.com > . > -- You received this message because you are subscribed to the Google Groups "jackson-user" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/jackson-user/CAE9hW8Qntr9sUkd1cFBY7gk385w1BBGKX0fpHY79MPanuOBeyA%40mail.gmail.com.
