-
Notifications
You must be signed in to change notification settings - Fork 418
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
jakarta.validation-api with picocli #1798
Comments
I suspect that is not possible but you can ask the maintainers of the jakarta.validation-api project, they may be better positioned to answer that question. |
@remkop I see that hibernate-validator is an implementation that uses validation but there is need to create |
There are several ways to think about this. However, the example in the user manual (calling a helper method from the Perhaps a nicer way is to have a custom Such a custom class ValidatingExecutionStrategy implements IExecutionStrategy {
public int execute(ParseResult parseResult) {
validate(parseResult.commandSpec());
return new CommandLine.RunLast().execute(parseResult); // default execution strategy
}
void validate(CommandSpec spec) {
Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
Set<ConstraintViolation<Object>> violations = validator.validate(spec.userObject());
if (!violations.isEmpty()) {
String errorMsg = "";
for (ConstraintViolation<?> violation : violations) {
errorMsg += "ERROR: " + violation.getMessage() + "\n";
}
throw new ParameterException(spec.commandLine(), errorMsg);
}
}
} The application would wire it in like this: public static void main(String[] args) {
MyApp app = new MyApp();
new CommandLine(app)
.setExecutionStrategy(new ValidatingExecutionStrategy())
.execute(args);
} I have added this example to the user manual (the HTML will be published with the next release), |
@remkop thanks 😃 Logger.getLogger("org.hibernate").setLevel(Level.OFF);
final CommandLine commandLine = new CommandLine(DiceRoller.class);
CommandLine.IExecutionStrategy executionStrategy = commandLine.getExecutionStrategy();
commandLine.setExecutionStrategy(parseResult ->
{
try (ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory())
{
Set<ConstraintViolation<Object>> violations = validatorFactory.getValidator().validate(parseResult.commandSpec().userObject());
if (!violations.isEmpty())
{
final StringBuilder errorMsg = new StringBuilder();
for (ConstraintViolation<?> violation : violations)
errorMsg.append("ERROR: ").append(violation.getPropertyPath()).append(' ').append(violation.getMessage()).append(System.lineSeparator());
throw new CommandLine.ParameterException(parseResult.commandSpec().commandLine(), errorMsg.toString());
}
}
return executionStrategy.execute(parseResult);
})
.execute(args); |
is there a way to use jakarta.validation-api with plain picocli (without Springboot/Hibernate etc.)? it may be silly but I'm trying to use it as it is but is doesn't work.
Example:
The text was updated successfully, but these errors were encountered: