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

.addValidator after .default removes default setting #18

Closed
PvdBerg1998 opened this issue Jun 10, 2017 · 4 comments
Closed

.addValidator after .default removes default setting #18

PvdBerg1998 opened this issue Jun 10, 2017 · 4 comments
Assignees
Labels

Comments

@PvdBerg1998
Copy link

The following code

private val x by parser.storing(
		"-x",
		help = "",
		transform = String::toInt
).addValidator {
	// Validate here
}.default(0)

receives the default value of 0 and gets labeled as optional, while

private val x by parser.storing(
		"-x",
		help = "",
		transform = String::toInt
).default(0).addValidator {
	// Validate here
}

does not, and gets labeled as required parameter.

@PvdBerg1998
Copy link
Author

Note that the first snippet produces a NPE when the optional property gets left out.

@xenomachina xenomachina self-assigned this Jun 12, 2017
@xenomachina
Copy link
Owner

Thanks for reporting this issue. I'm working on a fix and should have something ready shortly.

xenomachina added a commit that referenced this issue Jun 12, 2017
@xenomachina
Copy link
Owner

Ok, I have released a fix for this. The second test case, .default(...).addValidator(...) now works correctly.

The first test case, .addValidator(...).default(...), will now throw an IllegalStateException, as this is a potentially unsafe operation, since .default can actually change the type (for example, making it nullable, or by making it a supertype), and then there's no way to pass the default value to the validator.

I'm debating whether to make it possible to add a validator before a default, and simply have the validator not called when the default is used. This is potentially surprising behavior.

@PvdBerg1998
Copy link
Author

Nice, thanks for the support.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants