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

[API-8] Commands - Support generic parameter validation #2156

Closed
ItsDoot opened this issue Jul 4, 2020 · 2 comments
Closed

[API-8] Commands - Support generic parameter validation #2156

ItsDoot opened this issue Jul 4, 2020 · 2 comments

Comments

@ItsDoot
Copy link
Member

ItsDoot commented Jul 4, 2020

It would be nice to have some way of validating a parameter's output during argument parse time, rather than at execution time.

An example:

Pattern nameRegex = Pattern.compile("^[a-zA-Z0-9]{2,32}$");

Parameter.string()
    .validate(name -> {
        if (!nameRegex.matcher(name).matches()) {
            // throw an exception maybe?
        }
    })
    .build();
@dualspiral
Copy link
Contributor

Why not just create your own ValueParser to do that instead?

@dualspiral
Copy link
Contributor

I've added this as a separate parameter, VariableValueParameters#createValidatedStringParameter(Pattern pattern). The impl is something like this:

if (pattern.matcher(input).matches()) {
    return input;
}
// exception thrown here.

It will return a string.

I do not want to add a generic "validate" to all parameters because that depends on parameters:

  • parsing a string
  • providing an appropriate point to hook into the system

It's not worth it with the number of caveats there will have to be. So, I'll add the validated string parser, but if you're wanting to create your own objects via a parser, you're better off just writing the value parser yourself.

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

No branches or pull requests

2 participants