-
-
Save hferentschik/ea002c2d4f1427226a8b to your computer and use it in GitHub Desktop.
Type annotations and Hibernate Validator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Foo { | |
@Valid // !? needed or not? Atm this is needed to trigger iteration | |
private List<@Email String> myEmailList; // that's the case we should primarily address | |
// ... | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface Tuple<V1, V2> { | |
V1 getValue1(); | |
V2 getValue2(); | |
// ... | |
} | |
public interface NonNullTuple<@NotNull V1, @NotNull V2> extends Tuple<V1, V2> { | |
// ... | |
} | |
public class StringTuple implements NonNullTuple<String, String> { | |
@Override | |
public String getValue1() { // so here we safe an @NotNull which we basically inherit | |
return null; | |
} | |
@Override | |
public String getValue2() { | |
return null; | |
} | |
// ... | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface Producer<@NotNull V> { | |
V create(); | |
} | |
public class StringProducer implements Producer<String> { | |
String create(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface Tuple<V1, V2> { | |
V1 getValue1(); | |
V2 getValue2(); | |
// ... | |
} | |
public class StringIntegerTuple implements Tuple<@NotNull String, @NotNull @Max(10) Number>> { | |
@Override | |
public String getValue1() { | |
return null; | |
} | |
@Override | |
public Number getValue2() { | |
return null; | |
} | |
} | |
// but would it not just be easier to do: | |
public class StringIntegerTuple implements Tuple<String, Number> { | |
@Override | |
@NotNull | |
public String getValue1() { | |
return null; | |
} | |
@Override | |
@Max(10) | |
public Number getValue2() { | |
return null; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class TupleUser { | |
@Valid | |
private Tuple<@NotNull String, @NotNull, @Max(10) Number> myStringNumberTuple; // how will we map this to the actual values to be validated? | |
// ... | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class TupleWithTypeParameters<@NotNull V1, @NotNull, @Max(10) V2 extends Number> { // Hmm, why not apply constraints on the fields directly? | |
V1 value1; | |
V2 value2; | |
V1 getValue1() { // What do we do? Do we do field or getter validation? We probably would need another annotation to define this | |
return value1; | |
} | |
// ... | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
So when retrieving the property meta-data during cascaded validation of
myStringNumberTuple
, we'd need to pass in a context, which basically has the following contents:Then, when accessing the state of the object in
myStringNumberTuple
- e.g. when validating the propertygetValue1()
, we'd detect it's of type V1 and apply the statically defined meta-data as well as the one passed in from the usage site (as you see we need to define how to access the state, i.e. via getter or field).