-
Notifications
You must be signed in to change notification settings - Fork 44
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
add support for withValidatorMethod #77
Conversation
Fantastic addition IMO but comes with no tests whatsoever? |
Hi, I'm sorry for not adding tests... I'm using your project right now and this Unfortunately time is short as my deadline is the end of the week... I will Cheers
|
Not my project :) I'm just another self-interested party but I'm sure Michael would agree. I don't think there is any urgency here so write the tests when you can - it's the only way to ensure your feature stays functional over time! |
Thank you for this! |
I just reviewed your changes. I am not sure if it's an optimal choice, that the implementation of the "validate" method must be implemented by the base class of the generated builder. Here is a list of all 3 options: 1) Base ClassThis is your suggestion. To implement the validation method inside the base class you have to do something like this: @GeneratePojoBuilder(withValidatorMethod = true, withBaseclass = BuilderBase.class)
public class Pojo {
public String name;
} Then you have to implement the validation method inside the BuilderBase class like this: public class BuilderBase {
void validate(Pojo pojo) {
// TODO
}
} 2) SubclassAnother option would be to delegate the validation to the builders subclass by using PB's generation-gap-feature: @GeneratePojoBuilder(withValidatorMethod = true, withGenerationGap = true)
public class Pojo2 {
public String name;
} This generates two builder classes (one abstract with all the generated code, and another one for 'manual' extensions). @Generated("PojoBuilder")
public abstract class AbstractPojo2Builder
implements Cloneable {
[...]
protected abstract void validate(Pojo2 pojo); Then you have to implement the validation method inside the "manual" builder: @Generated("PojoBuilder")
public class Pojo2Builder extends AbstractPojo2Builder
implements Cloneable {
/**
* Creates a new {@link Pojo2Builder}.
*/
public Pojo2Builder() {
}
@Override
protected void validate(Pojo2 pojo) {
// TODO
}
} 3) Validator classThe third option is to delegate the validation to a totally different class that specifically is made for validating pojos: @GeneratePojoBuilder(withValidator = Pojo3Validator.class)
public class Pojo3 {
public String name;
} Then you have to implement the validaton method inside the custom PojoValidator: public class Pojo3Validator {
public void validate(Pojo3 pojo) {
// TODO
}
} I slightly tend to the third option. Comments? |
Hi there, Third is a great idea as it decouples the validation from the base class. I vote for 3 with a Validator interface. Regards |
Vote 3 |
Add support to withValidatorMethod so validation can be implemented in a builder base class (i.e. using OVal)