While there's still work to complete support for "is-required" for
non-creator properties, I tried to think through various related use cases,
and one common theme seems to be that beyond basic "is-given vs not-given"
for properties, there are follow-up checks like "can not be `null`" and
various other constraints checks. Although Jackson itself tries to steer
clear of becoming validation system (that's a whole domain in itself,
better served by others), it would make sense to allow some pluggable
validation by calling code. And maybe even some form of extension libraries.
Right now the main (and only) way is to specify a setter, like:
public class POJO {
public void setName(String name) {
if (name == null || name.length() == 0 ) { // null or empty, complain
} else if (name.length() > 80) { // too long
}
}
}
While this gives full power, it does not lend itself to easy reuse, and is
unnecessarily intrusive.
But a simple step above this could be to allow plugging of a validator of
some kind. So;
public class POJO {
@JsonValidate(MyStringValidator.class)
public String name;
}
@JsonValidat(OtherPOJOValidator.class)
public class OtherPOJO {
}
// or
mapper.configOverrides(YetAnotherPOJO.class)
.setValidator(new YetAnotherValidator());
would allow assigning a component to call before assigning a value to
1. Single property (first case)
2. Property of specific type (unless per-property overrides)
3. Or, per-property override, but not via annotations
There are many other aspects; including thorny question of whether "does
not exist" should still trigger validation (would be nice but is it
easy/possible to pipe?). But first things first... would this be useful to
include in 2.9?
-+ Tatu +-
ps. Another low-tech approach available now is to specify "dummy"
Converter, that takes same time, and does validation. Not very convenient
could work.
--
You received this message because you are subscribed to the Google Groups
"jackson-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.