Skip to content

fabasoad/sh-validators

Repository files navigation

Shell validators

Stand With Ukraine GitHub release security linting

Shell library to validate user inputs.

Supported validators

  1. Validate that directory exists
  2. Validate enum
  3. Validate that file exists
  4. Validate semver

Installation

bpkg install fabasoad/sh-validators

More information on installation options you can find here.

Examples

Use script by running file:

mkdir "this-dir-exists"
./fabasoad-validate-dir-exists "dir-param" "this-dir-exists"
echo $?
# Output
0

Use script by running function:

. "./validate-dir-exists"
mkdir "this-dir-exists"
fabasoad_validate_dir_exists "dir-param" "this-dir-exists"
echo $?
# Output
0

Negative use case:

./fabasoad-validate-dir-exists "dir-param" "this-dir-does-not-exist"
# Output
"dir-param" parameter is invalid. "<full-path>/this-dir-does-not-exist" is not a directory or does not exist.

If you want to redirect error message to your internal logic:

set +e
err_msg=$(./fabasoad-validate-dir-exists "dir-param" "this-dir-does-not-exist" 2>&1 >/dev/null)
if [ "$?" -ne 0 ]; then
  echo "[error] $(date +%s) $err_msg"
fi
# Output
[error] 1720946407 "dir-param" parameter is invalid. "<full-path>/this-dir-does-not-exist" is not a directory or does not exist.