{"id":13771553,"url":"https://github.com/pkyosx/believe","last_synced_at":"2025-05-11T04:30:46.517Z","repository":{"id":40470705,"uuid":"286930150","full_name":"pkyosx/believe","owner":"pkyosx","description":"A python package for json validation","archived":false,"fork":false,"pushed_at":"2024-04-08T07:28:48.000Z","size":55,"stargazers_count":4,"open_issues_count":1,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-14T09:15:50.234Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","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/pkyosx.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-08-12T06:02:06.000Z","updated_at":"2024-08-03T02:54:15.000Z","dependencies_parsed_at":"2024-08-03T17:04:46.979Z","dependency_job_id":null,"html_url":"https://github.com/pkyosx/believe","commit_stats":{"total_commits":49,"total_committers":3,"mean_commits":"16.333333333333332","dds":0.5102040816326531,"last_synced_commit":"eaea29c5f87abb2bff133ade3059ac99a5e28929"},"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pkyosx%2Fbelieve","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pkyosx%2Fbelieve/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pkyosx%2Fbelieve/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pkyosx%2Fbelieve/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pkyosx","download_url":"https://codeload.github.com/pkyosx/believe/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253518941,"owners_count":21921074,"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":[],"created_at":"2024-08-03T17:00:52.697Z","updated_at":"2025-05-11T04:30:46.238Z","avatar_url":"https://github.com/pkyosx.png","language":"Python","funding_links":[],"categories":["Assertions"],"sub_categories":[],"readme":"# Believe\n## Motivation\n* We often need to compare expected results in our test. It's lousy to check expected result under the following way.\n\n```\nimport uuid\n\nclass TestCases:\n    def test_user(self):\n        user = {\n                    \"name\": \"John\",\n                    \"id\": \"30e7b1e2-4c80-44c2-8fe5-23bad73ed8f2\"\n                }\n        assert set(\"name\", \"id\") == set(user.keys())\n        assert isinstance(user[\"name\"], str)\n        assert 0 \u003c len(user[\"name\"]) \u003c 64\n        assert isinstance(user[\"id\"], str)\n        uuid.UUID(user[\"id\"])\n```\n\n* By using this package, we could compare the value using the following way. It's easier to read and maintain.\n\n```\nimport believe as B\n\nclass TestCases:\n    def test_user(self):\n        user = {\n                    \"name\": \"John\",\n                    \"id\": \"30e7b1e2-4c80-44c2-8fe5-23bad73ed8f2\"\n                }\n        assert user == {\"name\": B.AnyStr(min_len=1, max_len=63),\n                        \"id\": B.AnyUUID()}\n\n```\n\n* If you are looking for web framework input validation, I suggest use [FastAPI](https://fastapi.tiangolo.com/). It properly integrate with openapi as well.\n\n## Installation\n```\npip install believe\n```\n\n## Basic usage\n```\nimport believe as B\nimport time\n\n# Match any string\nassert B.AnyStr() == \"any_str\"\n\n# Match string length \u003e= 1 and \u003c= 10\nassert B.AnyStr(min_len=1, max_len=10) == \"a\"\n\n# Match any string that can be converted to int\nassert B.AnyIntStr() == \"123\"\n\n# Match any UUID format string\nassert B.AnyUUID() == \"732c0743-2638-47d5-902d-0daa3080348b\"\n\n# Match any sha1 string\nassert B.AnySHA1() == \"b130c4b97d0640eaf3f45f7360a5b4dbbf561f58\"\n\n# Match any IPv4 string\nassert B.AnyIPV4() == \"1.2.3.4\"\n\n# Match integer that is \u003e=1 and \u003c= 10\nassert B.AnyInt(min_value=1, max_value=10) == 5\n\n# Match any float that is \u003e= 1.0 and \u003c= 10.0\nassert B.AnyFloat(min_value=1.0, max_value=10.0) == 5.0 # 1.0 \u003c= X \u003c= 10.0\n\n# Match if values is \"one\" or \"two\"\nassert B.OneOf(\"one\", \"two\") == \"one\"\n\n# Sometimes we don't care about the order, we can use AnyOrder\nassert B.AnyOrder([1, 2, 3]) == [2, 1, 3]\n\n# Sometimes we assign value as timestamp but test cases takes more than 1 sec\n# We can use almost to accept a range of values\nassert B.Almost(time.time(), ts_range=3) == time.time() # Allow 3 sec gap\n\n# If we allow None or any string\nassert B.Nullable(B.AnyStr()) == None\nassert B.Nullable(B.AnyStr()) == \"123\"\n\n# Only check type\nassert B.Any(bytes) == b'123'\n\n# Reverse check result, anything but \"A\" or \"B\"\nassert B.Not(B.OneOf(\"A\", \"B\")) == \"C\"\n\n# Match list\nassert B.ListOf(B.AnyStr()) == [\"A\", \"B\", \"C\"]\nassert B.ListOf(B.AnyStr(), n_item=3) == [\"A\", \"B\", \"C\"]  # exact 3 items\nassert B.ListOf(B.AnyStr(), min_item=1) == [\"A\", \"B\", \"C\"]  # \u003e= 1 items\nassert B.ListOf(B.AnyStr(), max_item=5) == [\"A\", \"B\", \"C\"]  # \u003c= 5 items\n```\n\n## Advance Usage\n```\n# If we don't want to use json.load('{\"foo\": \"bar\"}') == {\"foo\": \"bar\"}, we can use the following way\nassert B.AnyJsonStr({\"foo\": \"bar\"}) == '{\"foo\": \"bar\"}'\n\n# We can use AnyUrl to compare the normalized url\n# 1. We can compare one with default port and one without, they are identical\nassert B.AnyUrl(\"https://foo.com/\") == \"https://foo.com:443/\"\nassert B.AnyUrl(\"http://foo.com/\") == \"http://foo.com:80/\"\n# 2. We can ignore the order in query string\nassert B.AnyUrl(\"https://foo.com/bar?p1=1\u0026p2=2\") == \"https://foo.com/bar?p2=2\u0026p1=1\"\n\n# We can use Dict to compare a dict with Optional field\nassert B.Dict({\"name\": B.AnyStr(), \"value\": B.Optional(B.AnyStr())}) == {\"name\": \"abc\"}\nassert B.Dict({\"name\": B.AnyStr(), \"value\": B.Optional(B.AnyStr())}) == {\"name\": \"abc\", \"value\": \"def\"}\n\n# If key is a dynamic value, we can use DictOf(\u003ckey_matcher\u003e, \u003cvalue_matcher\u003e)\n# i.e. We want to match a dict with random uuid as key\nassert B.DictOf(B.AnyUUID(), B.OneOf(\"ok\", \"fail\")) == {\"732c0743-2638-47d5-902d-0daa3080348b\": \"ok\",\n                                                        \"5cfd50ba-c3d3-4fb7-b2fe-e9a6e039ad29\": \"fail\"}\n```\n\n## Use Validate Function\n```\n# validate with error exception\nimport believe as B\nvalidator = B.Dict({\"name\": B.AnyInt()})\n\nB.validate(validator, {\"name\": \"ken\"})  # believe.error.ValidateError: [e_path=$.name] 'ken' != AnyInt()\n```\n\n## A Complex Example\n```\nimport believe as B\nimport time\n\nresult_json = {\"name\": \"john\",\n               \"age\": 32,\n               \"download_link\": \"https://download.server.com/?name=john\u0026id=abc\",\n               \"role\": \"admin\",\n               \"address\": \"10.1.2.3\",\n               \"updated_at\": int(time.time()),\n               \"tags\": [\"admin\", \"john\"]}\n\nexp_result = B.Dict({\"name\": B.AnyStr(min_len=1, max_len=64),\n                     \"age\": B.AnyInt(min_value=0, max_value=200),\n                     \"download_link\": B.AnyUrl(\"https://download.server.com/?id=abc\u0026name=john\"),\n                     \"role\": B.OneOf(\"admin\", \"user\"),\n                     \"address\": B.AnyIPV4(),\n                     \"updated_at\": B.Almost(int(time.time())),\n                     \"tags\": B.ListOf(B.AnyStr()),\n                     \"extra\": B.Optional(B.Nullable(B.AnyStr()))})\nB.validate(exp_result, result_json)\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpkyosx%2Fbelieve","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpkyosx%2Fbelieve","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpkyosx%2Fbelieve/lists"}