#
Custom Validations
Bashly supports custom validation functions for your arguments, flag arguments, and environment variables. This is how it works:
In your bashly configuration file, arguments, flags (with arguments) and environment variables may have a
validate: function_name
option.Whenever you run your script, it will look for a function with that name, prefixed by
validate_
and will run it before allowing the user input to pass.If the function returns any string, it is considered an error. The string will be displayed on screen, as the error message.
name: viewer
args:
- name: path
validate: file_exists
validate_file_exists() {
[[ -f "$1" ]] || echo "must be an existing file"
}
Tip
You may specify multiple validation functions by supplying an array:
validate: [file_exists, file_is_writable]
#
Built-in Custom Validations
In addition, bashly comes with several built-in custom validations for common tasks:
file_exists
- Ensures that the argument points to a file.dir_exists
- Ensures that the argument points to a directory.integer
- Ensures that the argument is an integer.not_empty
- Ensures that the argument is not empty.
In order to add these validations to your code, run:
$ bashly add validations