Skip to content
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

Allow an array of Classes in validateWith and validateValueWith #342

Merged
merged 3 commits into from Mar 26, 2017

Conversation

lindenb
Copy link
Contributor

@lindenb lindenb commented Mar 25, 2017

Here is a PR where I've changed the definition of Parameter and DynamicParameter from:

  Class<? extends IParameterValidator> validateWith() default NoValidator.class;
  Class<? extends IValueValidator> validateValueWith() default NoValueValidator.class;

to

  Class<? extends IParameterValidator>[] validateWith() default NoValidator.class;
  Class<? extends IValueValidator>[] validateValueWith() default NoValueValidator.class;

this allow to specifiy multiple validator without creating a new class:

  @Parameter(names = "-age",
		  validateWith = {PositiveInteger.class,OddIntegerParameterValidator.class},
		  validateValueWith={GreaterTha0ValueValidator.class,LowerThan100ValueValidator.class})
  public int age=29;

I've added a test and a paragraph in the documentation

@cbeust
Copy link
Owner

cbeust commented Mar 25, 2017

I'm guessing this is backward compatible because the annotation syntax for attributes a = b allows b to be either a single value or an array?

@lindenb
Copy link
Contributor Author

lindenb commented Mar 25, 2017

@cbeust

I'm guessing this is backward compatible because the annotation syntax for attributes a = b allows b to be either a single value or an array?

I'm new to jcommander. How should I test this ? can you please give me an example ?

P.

@cbeust
Copy link
Owner

cbeust commented Mar 25, 2017

It's not a JCommander thing: if all the tests pass with your change, you're good. I was just initially surprised how such a change could be backward compatible and then I saw that you're only modifying the attribute type from a single value to an array.

I was part of the annotation JSR and we specifically allowed "a = b" to be either a single value or an array, and that's probably why your code is backward compatible. Will review shortly.

if (validator != null) {
validateParameter(this, validator, name, value);
Class<? extends IParameterValidator> validators[] = wrappedParameter.validateWith();
if (validators != null && validators.length>0) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spaces: length > 0.

if (validator != null) {
validateValueParameter(validator, name, value);
Class<? extends IValueValidator> validators[] = wrappedParameter.validateValueWith();
if (validators != null && validators.length>0) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spaces.

public void multipleValidatorsFails1()
{
ArgsMultiValidate a = new ArgsMultiValidate();
JCommander jc = new JCommander(a);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indentation.

public void multipleValidatorsFails2()
{
ArgsMultiValidate a = new ArgsMultiValidate();
JCommander jc = new JCommander(a);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indentation.


public class ArgsMultiValidate {
public static class OddIntegerParameterValidator implements IParameterValidator {
@Override
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Multiple indentations.

public static class OddIntegerParameterValidator implements IParameterValidator {
@Override
public void validate(String name, String value) throws ParameterException {
if(Integer.parseInt(value)%2!=1) throw new ParameterException("param "+name+"="+value+" is not odd");
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spaces.

@lindenb
Copy link
Contributor Author

lindenb commented Mar 26, 2017

back to you @cbeust. I hope I've fixed all the formatting problems :-)

@cbeust cbeust merged commit 60bd04c into cbeust:master Mar 26, 2017
@cbeust
Copy link
Owner

cbeust commented Mar 26, 2017

Looks good, thanks!

@lindenb lindenb deleted the multiplevalidators branch March 26, 2017 16:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants