# Custom Validations

Bashly supports custom validation functions for your arguments, and flag arguments. This is how it works:

  1. In your bashly configuration file, arguments and flags (with arguments) may have a validate: function_name option.
  2. Whenever your 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.
  3. If the function returns any string, it is considered an error. The string will be displayed on screen, as the error message.
bashly.yml
name: viewer

args:
- name: path
  validate: file_exists
src/lib/validate_file_exists.sh
validate_file_exists() {
  [[ -f "$1" ]] || echo "must be an existing file"
}

# 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

Validations Example