{"id":13501695,"url":"https://github.com/spy16/pyschemes","last_synced_at":"2025-04-06T16:14:54.718Z","repository":{"id":62583314,"uuid":"86912382","full_name":"spy16/pyschemes","owner":"spy16","description":"PySchemes is a library for validating data structures in python","archived":false,"fork":false,"pushed_at":"2022-09-12T12:21:05.000Z","size":19,"stargazers_count":364,"open_issues_count":1,"forks_count":17,"subscribers_count":12,"default_branch":"master","last_synced_at":"2024-05-23T04:34:07.517Z","etag":null,"topics":["python","pythonic","schema","validation"],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"wtfpl","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/spy16.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}},"created_at":"2017-04-01T12:37:46.000Z","updated_at":"2024-03-05T01:33:43.000Z","dependencies_parsed_at":"2022-11-03T21:37:09.653Z","dependency_job_id":null,"html_url":"https://github.com/spy16/pyschemes","commit_stats":null,"previous_names":["shivylp/pyschemes"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spy16%2Fpyschemes","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spy16%2Fpyschemes/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spy16%2Fpyschemes/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spy16%2Fpyschemes/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/spy16","download_url":"https://codeload.github.com/spy16/pyschemes/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247509238,"owners_count":20950232,"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":["python","pythonic","schema","validation"],"created_at":"2024-07-31T22:01:46.655Z","updated_at":"2025-04-06T16:14:54.693Z","avatar_url":"https://github.com/spy16.png","language":"Python","funding_links":[],"categories":["Python"],"sub_categories":[],"readme":"# PySchemes\nPySchemes is a library for validating data structures in Python. PySchemes is designed to be simple and Pythonic.\n\n![https://travis-ci.org/shivylp/pyschemes](https://secure.travis-ci.org/shivylp/pyschemes.svg?branch=master)\n\n\n\n## Features\n* Simple representation of schema using primitive Python types (Or Complex types as well)\n* Sane Schema Structures\n* Sane errors\n* Power of PySchemes lies in its validators (take a look at tests to understand usage of various validators)\n* Lambda functions or any callable can be easily used as a validator\n\n## Examples\n\n```python\n# Execute this before executing any of the examples\n\u003e\u003e\u003e from pyschemes import Scheme, validators\n```\n\n1. Simple TypeChecking\n```python\n\u003e\u003e\u003e Scheme(int).validate(10)\n10\n\n\u003e\u003e\u003e Scheme(int).validate(10.3)\nTraceback (most recent call last):\n  File \"\u003cstdin\u003e\", line 1, in \u003cmodule\u003e\nTypeError: expected type: 'int', got 'float'\n\n\u003e\u003e\u003e from collections import Iterable\n\u003e\u003e\u003e Scheme(Iterable).validate([1, 2])\n[1, 2]\n\n\u003e\u003e\u003e Scheme(Iterable).validate((\"hello\", ))\n(\"hello\", )\n\n\u003e\u003e\u003e Scheme(Iterable).validate({\"a\": \"b\", \"c\": \"d\"})\n{\"a\": \"b\", \"c\": \"d\"}\n\n\u003e\u003e\u003e Scheme(Iterable).validate(range(100))\nrange(0, 100)\n\n\u003e\u003e\u003e Scheme(Iterable).validate(lambda x: x)\nTraceback (most recent call last):\n  File \"\u003cstdin\u003e\", line 1, in \u003cmodule\u003e\nTypeError: expected type: 'Iterable', got 'function'\n```\n\n2. Simple Value Validation\n```python\n\u003e\u003e\u003e Scheme(10).validate(10)\n10\n\n\u003e\u003e\u003e Scheme(10).validate(10.3)\nTraceback (most recent call last):\n  File \"\u003cstdin\u003e\", line 1, in \u003cmodule\u003e\nValueError: expected value '10', got '10.3'\n```\n\n3. Simple Choices Validation\n```python\n\u003e\u003e\u003e choices = validators.Any([\"choiceA\", \"choiceB\", \"choiceC\"])\n\n\u003e\u003e\u003e Scheme(choices).validate(\"choiceA\")\n'choiceA'\n\n\u003e\u003e\u003e Scheme(choices).validate(\"hello\")\nTraceback (most recent call last):\n  File \"\u003cstdin\u003e\", line 1, in \u003cmodule\u003e\nValueError: value did not pass any validation\n```\n\n4. Validating a List/Iterable Scheme\n\n```python\n\u003e\u003e\u003e Scheme([str, 2, int]).validate([\"hello\", 2, 15])\n[\"hello\", 2, 15]\n\n\u003e\u003e\u003e Scheme((str, 2, int)).validate((\"hello\", 2, 15))\n(\"hello\", 2, 15)\n\n\u003e\u003e\u003e Scheme((str, 2, int)).validate([\"hello\", 2, 15])\nTraceback (most recent call last):\n  File \"\u003cstdin\u003e\", line 1, in \u003cmodule\u003e\nTypeError: expected type: 'tuple', got 'list'\n\n\u003e\u003e\u003e Scheme([str, 2, int]).validate([\"hello\", 3, 130])\nTraceback (most recent call last):\n  File \"\u003cstdin\u003e\", line 1, in \u003cmodule\u003e\nValueError: element at index 1 (expected value '2', got '3')\n\n\u003e\u003e\u003e Scheme([str, 2, int]).validate([\"hello\", 2, 4.5])\nTraceback (most recent call last):\n  File \"\u003cstdin\u003e\", line 1, in \u003cmodule\u003e\nTypeError: element at index 2 (expected type: 'int', got 'float')\n```\n\n\n5. Validating a Dictionary/Mapping Scheme\n```python\n\u003e\u003e\u003e Scheme({str: int}).validate({\"a\": 1, \"b\": 2})\n{'a': 1, 'b': 2}\n\n\u003e\u003e\u003e Scheme({str: int}).validate({\"a\": 1, \"b\": 3.5})\nTraceback (most recent call last):\n  File \"\u003cstdin\u003e\", line 1, in \u003cmodule\u003e\nTypeError: at key 'b' (expected type: 'int', got 'float')\n\n\u003e\u003e\u003e Scheme({\"hello\": 10, str: object}).validate({\"hello\": 10, \"world\": 12})\n{\"hello\": 10, \"world\": 12}\n\n\u003e\u003e\u003e Scheme({\"required-key\": int}).validate({})\nTraceback (most recent call last):\n  File \"\u003cstdin\u003e\", line 1, in \u003cmodule\u003e\nValueError: missing key 'required-key'\n\n\u003e\u003e\u003e from pyschemes.validators import Optional\n\u003e\u003e\u003e Scheme({\"k\": Optional(str, \"hello\")}).validate({})\n{'k': 'hello'}\n\n\u003e\u003e\u003e Scheme({\"k\": Optional(str, \"hello\")}).validate({\"k\": \"world\"})\n{'k': 'world'}\n\n\u003e\u003e\u003e Scheme({\"k\": Optional(int, 10)}).validate({\"k\": \"world\"})\nTraceback (most recent call last):\n  File \"\u003cstdin\u003e\", line 1, in \u003cmodule\u003e\nTypeError: at key 'k' (expected type: 'int', got 'str')\n\n\u003e\u003e\u003e Scheme({\"only-key\": str}).validate({\"only-key\": \"hello\", \"b\": \"not-allowed\"})\nTraceback (most recent call last):\n  File \"\u003cstdin\u003e\", line 1, in \u003cmodule\u003e\nValueError: at key 'b' (not allowed)\n```\n\n\n6. Lambda/Callable Scheme\n```python\n\u003e\u003e\u003e Scheme(lambda x: x \u003c 100).validate(10)\n10\n\n\u003e\u003e\u003e Scheme(lambda x: x \u003c 100).validate(101)\nTraceback (most recent call last):\n  File \"\u003cstdin\u003e\", line 1, in \u003cmodule\u003e\nValueError: validator '\u003clambda\u003e' failed for value '101'\n\n\u003e\u003e\u003e def CustomValidator(value):\n...    if value == \"foo\" or value in range(100):\n...       return value\n...    else:\n...       return False\n...\n\u003e\u003e\u003e Scheme(CustomValidator).validate(101)\nTraceback (most recent call last):\n  File \"\u003cstdin\u003e\", line 1, in \u003cmodule\u003e\nValueError: validator 'CustomValidator' failed for value '10'\n\n\u003e\u003e\u003e Scheme(CustomValidator).validate(9)\n9\n\u003e\u003e\u003e Scheme(CustomValidator).validate(\"foo\")\n'foo'\n```\n\n\n7. Compund Schemes\n```python\n\u003e\u003e\u003e Scheme({\"test\": lambda x: x \u003c 100}).validate({\"test\": 10})\n{'test': 10}\n\n\u003e\u003e\u003e Scheme({\"test\": lambda x: x \u003c 100}).validate({\"test\": 101})\nTraceback (most recent call last):\n  File \"\u003cstdin\u003e\", line 1, in \u003cmodule\u003e\nValueError: at key 'test' (validator '\u003clambda\u003e' failed for value '101')\n\n\u003e\u003e\u003e Scheme({\"test\": Scheme({\"a\": str, \"b\": int})}).validate({\"test\": {}})\nTraceback (most recent call last):\n  File \"\u003cstdin\u003e\", line 1, in \u003cmodule\u003e\nValueError: at key 'test' (missing key 'a')\n\n\u003e\u003e\u003e Scheme({\"test\": Scheme({\"b\": int})}).validate({\"test\": {\"b\": 1.5}})\nTraceback (most recent call last):\n  File \"\u003cstdin\u003e\", line 1, in \u003cmodule\u003e\nTypeError: at key 'test' (at key 'b' (expected type: 'int', got 'float'))\n\n\u003e\u003e\u003e Scheme({\"t\": {str: int}}).validate({\"t\": {\"a\": 1}})\n{'t': {'a': 1}}\n\n\u003e\u003e\u003e Scheme({\"t\": (str, int)}).validate({\"t\": (\"a\", 1)})\n{'t': ('a', 1)}\n```\n\nAnd more to come in tests!\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fspy16%2Fpyschemes","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fspy16%2Fpyschemes","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fspy16%2Fpyschemes/lists"}