Stand With Ukraine GitHub release security linting
Shell library to validate user inputs.
The following tools have to be available on a machine prior using this library:
bpkg install fabasoad/sh-validators
See installation options for more details.
It is possible to set preferred custom exit code on failure via environment variable:
- Name:
FABASOAD_VALIDATORS_CONFIG_EXIT_CODE. - Type:
number - Default:
1
Examples:
# Default exit code $ ./fabasoad-validate-dir-exists "./abc" "./abc" is not a directory or does not exist. $ echo $? 1 # Custom exit code $ FABASOAD_VALIDATORS_CONFIG_EXIT_CODE=32 ./fabasoad-validate-dir-exists "./abc" "./abc" is not a directory or does not exist. $ echo $? 32 # Invalid exit code $ FABASOAD_VALIDATORS_CONFIG_EXIT_CODE=invalid ./fabasoad-validate-dir-exists "./abc" "./abc" is not a directory or does not exist. $ echo $? 1
Validates that passed value is a path to an existing directory. Examples:
# Positive case $ ./fabasoad-validate-dir-exists "this-dir-exists" $ echo $? 0 # Negative case with passing a file instead of a directory $ ./fabasoad-validate-dir-exists "this-file-exists.txt" "this-file-exists.txt" is not a directory or does not exist. $ echo $? 1 # Negative case without setting parameter name $ ./fabasoad-validate-dir-exists "this-dir-does-not-exist" "this-dir-does-not-exist" is not a directory or does not exist. $ echo $? 1 # Negative case with setting parameter name $ ./fabasoad-validate-dir-exists "this-dir-does-not-exist" "my-param" "my-param" parameter is invalid. "this-dir-does-not-exist" is not a directory or does not exist. $ echo $? 1
Validates string to be one of the possible values (emulating enum data type). Examples:
# Positive case $ ./fabasoad-validate-enum "true" "true,false" $ echo $? 0 # Negative case without setting parameter name $ ./fabasoad-validate-enum "wrong" "true,false" "wrong" is invalid. Possible values: true, false. $ echo $? 1 # Negative case with setting parameter name $ ./fabasoad-validate-enum "wrong" "true,false" "my-param" "my-param" parameter is invalid. "wrong" is invalid. Possible values: true, false. $ echo $? 1
Validates that passed value is a path to an existing file. Examples:
# Positive case $ ./fabasoad-validate-file-exists "this-file-exists.txt" $ echo $? 0 # Negative case with passing a directory instead of a file $ ./fabasoad-validate-file-exists "this-dir-exists" "this-dir-exists" is not a file or does not exist. $ echo $? 1 # Negative case without setting parameter name $ ./fabasoad-validate-file-exists "this-file-does-not-exist.txt" "this-file-does-not-exist.txt" is not a file or does not exist. $ echo $? 1 # Negative case with setting parameter name $ ./fabasoad-validate-file-exists "this-file-does-not-exist.txt" "my-param" "my-param" parameter is invalid. "this-file-does-not-exist.txt" is not a file or does not exist. $ echo $? 1
Validates value to be a valid semver string. Examples:
# Positive case $ ./fabasoad-validate-semver "1.2.3" $ echo $? 0 # Negative case without setting parameter name $ ./fabasoad-validate-semver "alpha.beta.1" "alpha.beta.1" is not a valid semver. $ echo $? 1 # Negative case with setting parameter name $ ./fabasoad-validate-semver "alpha.beta.1" "my-param" "my-param" parameter is invalid. "alpha.beta.1" is not a valid semver. $ echo $? 1
Validates that tool is installed on the machine. Examples:
# Positive case (assuming jq is installed) $ ./fabasoad-validate-tool-installed "jq" $ echo $? 0 # Negative case without setting parameter name $ ./fabasoad-validate-tool-installed "abc" "abc" is not installed on the current machine. $ echo $? 1 # Negative case with setting parameter name $ ./fabasoad-validate-tool-installed "abc" "my-param" "my-param" parameter is invalid. "abc" is not installed on the current machine. $ echo $? 1
If you want to redirect error message to your internal logic:
set +e err_msg=$(./fabasoad-validate-dir-exists "abc" 2>&1 >/dev/null) exit_code="$?" if [ "${exit_code}" -ne 0 ]; then printf "[ERR] [%s] %s\n" "$(date +'%Y-%m-%d %T')" "${err_msg}" fi set -e exit "${exit_code}" # Output [ERR] [2025年01月25日 19:11:36] "abc" is not a directory or does not exist.