Friday 12 February 2021

Spring Shell: Validate command arguments

Spring Shell has built-in support with ‘Bean Validation API’. You can use all the validation constraints supported by ‘Bean Validation API’.

 

Example

@ShellComponent(value = "Validate Command Arguments")
public class CommandArgumentsValidation {
	
	@ShellMethod(value = "Check user Id", key = "check-user-id", prefix = "-")
	public String isUserIdValid(@Size(min = 8, max = 40) String userId) {
		return userId + "is a valid."; 
	}
}

 

Run 1: With userid that do not meet validation criteria

shell:>check-user-id krishna
[31mThe following constraints were not met:[0m
	-user-id string : size must be between 8 and 40 (You passed 'krishna')

 

Run 2: With userid that meet validation criteria

shell:>check-user-id krishna123
krishna123is a valid.

You can download complete application from this link.

https://github.com/harikrishna553/springboot/tree/master/shell/hello-world

 


 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment