{"id":16019823,"url":"https://github.com/fabasoad/sh-validators","last_synced_at":"2025-10-06T19:06:53.365Z","repository":{"id":242215371,"uuid":"808967993","full_name":"fabasoad/sh-validators","owner":"fabasoad","description":"bpkg package with validators for different types","archived":false,"fork":false,"pushed_at":"2025-03-23T22:03:11.000Z","size":37,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-28T05:28:51.765Z","etag":null,"topics":["bpkg-package","shell","validate","validation","validator"],"latest_commit_sha":null,"homepage":"","language":"Shell","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/fabasoad.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":".github/CODEOWNERS","security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"custom":["https://www.bitcoinqrcodemaker.com/?style=bitcoin\u0026address=145HwyQAcv4vrzUumJhu7nWGAVBysX9jJH\u0026prefix=on","https://paypal.me/fabasoad"],"github":["fabasoad"],"ko_fi":"fabasoad","liberapay":"fabasoad"}},"created_at":"2024-06-01T09:57:30.000Z","updated_at":"2025-03-23T22:03:14.000Z","dependencies_parsed_at":"2025-01-25T00:23:03.953Z","dependency_job_id":"60d714dd-a9d3-43bc-a15f-a489fd454516","html_url":"https://github.com/fabasoad/sh-validators","commit_stats":null,"previous_names":["fabasoad/sh-validators"],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fabasoad%2Fsh-validators","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fabasoad%2Fsh-validators/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fabasoad%2Fsh-validators/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fabasoad%2Fsh-validators/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fabasoad","download_url":"https://codeload.github.com/fabasoad/sh-validators/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247284083,"owners_count":20913684,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bpkg-package","shell","validate","validation","validator"],"created_at":"2024-10-08T17:05:32.902Z","updated_at":"2025-10-06T19:06:53.348Z","avatar_url":"https://github.com/fabasoad.png","language":"Shell","funding_links":["https://www.bitcoinqrcodemaker.com/?style=bitcoin\u0026address=145HwyQAcv4vrzUumJhu7nWGAVBysX9jJH\u0026prefix=on","https://paypal.me/fabasoad","https://github.com/sponsors/fabasoad","https://ko-fi.com/fabasoad","https://liberapay.com/fabasoad"],"categories":[],"sub_categories":[],"readme":"# Shell validators\n\n[![Stand With Ukraine](https://raw.githubusercontent.com/vshymanskyy/StandWithUkraine/main/badges/StandWithUkraine.svg)](https://stand-with-ukraine.pp.ua)\n![GitHub release](https://img.shields.io/github/v/release/fabasoad/sh-validators?include_prereleases)\n![security](https://github.com/fabasoad/sh-validators/actions/workflows/security.yml/badge.svg)\n![linting](https://github.com/fabasoad/sh-validators/actions/workflows/linting.yml/badge.svg)\n\nShell library to validate user inputs.\n\n## Table of Contents\n\n- [Shell validators](#shell-validators)\n  - [Table of Contents](#table-of-contents)\n  - [How to use?](#how-to-use)\n    - [Prerequisites](#prerequisites)\n    - [Installation](#installation)\n  - [Features](#features)\n    - [Customization](#customization)\n      - [Define exit code](#define-exit-code)\n    - [Validators](#validators)\n      - [Validate that directory exists](#validate-that-directory-exists)\n      - [Validate enum](#validate-enum)\n      - [Validate that file exists](#validate-that-file-exists)\n      - [Validate semver](#validate-semver)\n      - [Validate tool installed](#validate-tool-installed)\n  - [Hints](#hints)\n  - [Contributions](#contributions)\n\n## How to use?\n\n### Prerequisites\n\nThe following tools have to be available on a machine prior using this library:\n\n- [bash \u003e=4.0](https://www.gnu.org/software/bash/)\n\n### Installation\n\n```shell\nbpkg install fabasoad/sh-validators\n```\n\nSee [installation options](https://github.com/bpkg/bpkg?tab=readme-ov-file#installing-packages)\nfor more details.\n\n## Features\n\n### Customization\n\n#### Define exit code\n\nIt is possible to set preferred custom exit code on failure via environment variable:\n\n- **Name**: `FABASOAD_VALIDATORS_CONFIG_EXIT_CODE`.\n- **Type**: `number`\n- **Default**: `1`\n\nExamples:\n\n```shell\n# Default exit code\n$ ./fabasoad-validate-dir-exists \"./abc\"\n\"./abc\" is not a directory or does not exist.\n$ echo $?\n1\n# Custom exit code\n$ FABASOAD_VALIDATORS_CONFIG_EXIT_CODE=32 ./fabasoad-validate-dir-exists \"./abc\"\n\"./abc\" is not a directory or does not exist.\n$ echo $?\n32\n# Invalid exit code\n$ FABASOAD_VALIDATORS_CONFIG_EXIT_CODE=invalid ./fabasoad-validate-dir-exists \"./abc\"\n\"./abc\" is not a directory or does not exist.\n$ echo $?\n1\n```\n\n### Validators\n\n#### Validate that directory exists\n\nValidates that passed value is a path to an existing directory. Examples:\n\n```shell\n# Positive case\n$ ./fabasoad-validate-dir-exists \"this-dir-exists\"\n$ echo $?\n0\n# Negative case with passing a file instead of a directory\n$ ./fabasoad-validate-dir-exists \"this-file-exists.txt\"\n\"this-file-exists.txt\" is not a directory or does not exist.\n$ echo $?\n1\n# Negative case without setting parameter name\n$ ./fabasoad-validate-dir-exists \"this-dir-does-not-exist\"\n\"this-dir-does-not-exist\" is not a directory or does not exist.\n$ echo $?\n1\n# Negative case with setting parameter name\n$ ./fabasoad-validate-dir-exists \"this-dir-does-not-exist\" \"my-param\"\n\"my-param\" parameter is invalid. \"this-dir-does-not-exist\" is not a directory or does not exist.\n$ echo $?\n1\n```\n\n#### Validate enum\n\nValidates string to be one of the possible values (emulating enum data type).\nExamples:\n\n```shell\n# Positive case\n$ ./fabasoad-validate-enum \"true\" \"true,false\"\n$ echo $?\n0\n# Negative case without setting parameter name\n$ ./fabasoad-validate-enum \"wrong\" \"true,false\"\n\"wrong\" is invalid. Possible values: true, false.\n$ echo $?\n1\n# Negative case with setting parameter name\n$ ./fabasoad-validate-enum \"wrong\" \"true,false\" \"my-param\"\n\"my-param\" parameter is invalid. \"wrong\" is invalid. Possible values: true, false.\n$ echo $?\n1\n```\n\n#### Validate that file exists\n\nValidates that passed value is a path to an existing file. Examples:\n\n```shell\n# Positive case\n$ ./fabasoad-validate-file-exists \"this-file-exists.txt\"\n$ echo $?\n0\n# Negative case with passing a directory instead of a file\n$ ./fabasoad-validate-file-exists \"this-dir-exists\"\n\"this-dir-exists\" is not a file or does not exist.\n$ echo $?\n1\n# Negative case without setting parameter name\n$ ./fabasoad-validate-file-exists \"this-file-does-not-exist.txt\"\n\"this-file-does-not-exist.txt\" is not a file or does not exist.\n$ echo $?\n1\n# Negative case with setting parameter name\n$ ./fabasoad-validate-file-exists \"this-file-does-not-exist.txt\" \"my-param\"\n\"my-param\" parameter is invalid. \"this-file-does-not-exist.txt\" is not a file or does not exist.\n$ echo $?\n1\n```\n\n#### Validate semver\n\nValidates value to be a valid semver string. Examples:\n\n```shell\n# Positive case\n$ ./fabasoad-validate-semver \"1.2.3\"\n$ echo $?\n0\n# Negative case without setting parameter name\n$ ./fabasoad-validate-semver \"alpha.beta.1\"\n\"alpha.beta.1\" is not a valid semver.\n$ echo $?\n1\n# Negative case with setting parameter name\n$ ./fabasoad-validate-semver \"alpha.beta.1\" \"my-param\"\n\"my-param\" parameter is invalid. \"alpha.beta.1\" is not a valid semver.\n$ echo $?\n1\n```\n\n#### Validate tool installed\n\nValidates that tool is installed on the machine. Examples:\n\n```shell\n# Positive case (assuming jq is installed)\n$ ./fabasoad-validate-tool-installed \"jq\"\n$ echo $?\n0\n# Negative case without setting parameter name\n$ ./fabasoad-validate-tool-installed \"abc\"\n\"abc\" is not installed on the current machine.\n$ echo $?\n1\n# Negative case with setting parameter name\n$ ./fabasoad-validate-tool-installed \"abc\" \"my-param\"\n\"my-param\" parameter is invalid. \"abc\" is not installed on the current machine.\n$ echo $?\n1\n```\n\n## Hints\n\nIf you want to redirect error message to your internal logic:\n\n```shell\nset +e\nerr_msg=$(./fabasoad-validate-dir-exists \"abc\" 2\u003e\u00261 \u003e/dev/null)\nexit_code=\"$?\"\nif [ \"${exit_code}\" -ne 0 ]; then\n  printf \"[ERR] [%s] %s\\n\" \"$(date +'%Y-%m-%d %T')\" \"${err_msg}\"\nfi\nset -e\nexit \"${exit_code}\"\n# Output\n[ERR] [2025-01-25 19:11:36] \"abc\" is not a directory or does not exist.\n```\n\n## Contributions\n\n![Alt](https://repobeats.axiom.co/api/embed/edbb31d44e161d6596d5cdd26004b9926e5b49f6.svg \"Repobeats analytics image\")\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffabasoad%2Fsh-validators","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffabasoad%2Fsh-validators","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffabasoad%2Fsh-validators/lists"}