{"id":13705627,"url":"https://github.com/miss85246/sanic-dantic","last_synced_at":"2025-05-05T16:33:25.729Z","repository":{"id":56073121,"uuid":"312763338","full_name":"miss85246/sanic-dantic","owner":"miss85246","description":"A request parameter checking and parsing library based on pydantic under the sanic framework","archived":false,"fork":false,"pushed_at":"2023-02-12T11:10:01.000Z","size":681,"stargazers_count":51,"open_issues_count":0,"forks_count":10,"subscribers_count":3,"default_branch":"main","last_synced_at":"2023-11-20T16:02:42.976Z","etag":null,"topics":["parsing-library","sanic-framework"],"latest_commit_sha":null,"homepage":"https://miss85246.github.io/sanic-dantic/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/miss85246.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}},"created_at":"2020-11-14T06:53:01.000Z","updated_at":"2024-01-14T21:10:16.680Z","dependencies_parsed_at":"2023-02-14T14:16:09.344Z","dependency_job_id":null,"html_url":"https://github.com/miss85246/sanic-dantic","commit_stats":null,"previous_names":["misss85246/sanic-dantic"],"tags_count":0,"template":null,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/miss85246%2Fsanic-dantic","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/miss85246%2Fsanic-dantic/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/miss85246%2Fsanic-dantic/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/miss85246%2Fsanic-dantic/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/miss85246","download_url":"https://codeload.github.com/miss85246/sanic-dantic/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224455897,"owners_count":17314202,"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":["parsing-library","sanic-framework"],"created_at":"2024-08-02T22:00:45.118Z","updated_at":"2024-11-13T13:30:44.547Z","avatar_url":"https://github.com/miss85246.png","language":"Python","funding_links":[],"categories":["Extensions"],"sub_categories":["Utils"],"readme":"# sanic-dantic\r\n\r\n[![Build Status](https://img.shields.io/endpoint.svg?url=https%3A%2F%2Factions-badge.atrox.dev%2Fmiss85246%2Fsanic-dantic%2Fbadge%3Fref%3Dmain\u0026style=flat)](https://actions-badge.atrox.dev/miss85246/sanic-dantic/goto?ref=main)\r\n[![Downloads](https://static.pepy.tech/personalized-badge/sanic-dantic?period=total\u0026units=international_system\u0026left_color=grey\u0026right_color=brightgreen\u0026left_text=Downloads)](https://pepy.tech/project/sanic-dantic)\r\n[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/sanic-dantic)](https://pypi.org/project/sanic-dantic/)\r\n[![PyPI](https://img.shields.io/pypi/v/sanic-dantic)](https://pypi.org/project/sanic-dantic/)\r\n[![PyPI - License](https://img.shields.io/pypi/l/sanic-dantic)](https://pypi.org/project/sanic-dantic/)\r\n[![Docs](https://img.shields.io/badge/docs-passing-brightgreen)](https://miss85246.github.io/sanic-dantic/)\r\n\r\n当前为 sanic-dantic 的英文简介，如果您想阅读中文版本，请点击[这里](README_zh.md)。\r\n\r\n## Introduction\r\n\r\nsanic-dantic is a request parameter check and parsing library based on the sanic\r\nframework. It is based on pydantic and can help developers quickly check and\r\nget request parameters.\r\n\r\n## Why sanic-dantic\r\n\r\nIn many cases, we need to check and parse request parameters, but the sanic\r\nframework itself does not provide it, so we need to implement it ourselves.\r\nIn most cases, you may need to do this:\r\n\r\n```python\r\nfrom sanic import Sanic\r\nfrom sanic.response import json\r\n\r\napp = Sanic(\"SanicDanticExample\")\r\n\r\n\r\n@app.route('/example')\r\nasync def path_param_examples(request):\r\n    name = request.args.get(\"name\")\r\n    age = request.args.get(\"age\")\r\n    if not isinstance(name, str) or not isinstance(age, int):\r\n        return json({\"error\": \"parameter type error\"})\r\n    return json({\"message\": f\"hello {name} are you {age} years old ?\"})\r\n```\r\n\r\nThis code looks simple, but when your parameters become more and more, you will\r\nfind that you need to write a lot. This is a bad implementation because it is\r\nnot maintainable. So we need a better way to implement it.\r\n\r\nsanic-dantic can help you quickly implement parameter checking and parsing.\r\n\r\n## Installation\r\n\r\nYou can install sanic-dantic through pip:\r\n\r\n```bash\r\npip install sanic-dantic\r\n```\r\n\r\nYou can also install it through the source code:\r\n\r\n```bash\r\npip install git+https://github.com/miss85246/sanic-dantic.git\r\n```\r\n\r\n### Requirements\r\n\r\nsanic-dantic depends on sanic and pydantic, their specific versions are:\r\n\r\n```bash\r\nsanic \u003e= 20.3.0\r\npydantic \u003e= 1.7.3\r\n```\r\n\r\n## Simple Usage\r\n\r\nFirst, define the parameter check model through pydantic,\r\nthen define the view function, and check and parse the parameters through the\r\n`parse_params` decorator.\r\n\r\n```python\r\nfrom sanic import Sanic\r\nfrom sanic.response import json\r\n\r\nfrom sanic_dantic import parse_params, BaseModel, Field\r\n\r\n\r\n# define params check model by pydantic.\r\nclass Person(BaseModel):\r\n    name: str = Field(description=\"name of person\")\r\n    age: int = Field(default=18, description=\"age of person\")\r\n\r\n\r\napp = Sanic(\"SanicDanticExample\")\r\n\r\n\r\n# view function and check and parse params by `parse_params` decorator.\r\n@app.route('/example')\r\n@parse_params(path=Person)\r\nasync def path_param_examples(request, params):\r\n    # request.ctx and params are parsed parameters\r\n    print(\"ctx:\", request.ctx.params)\r\n    print(\"params:\", params)\r\n    print(\"name:\", params.name)\r\n    print(\"age:\", params.age)\r\n\r\n    return json({\"ctx\": request.ctx.params, \"params\": params})\r\n\r\n\r\nif __name__ == \"__main__\":\r\n    app.run(host=\"localhost\", port=8000)\r\n```\r\n\r\nnow you can test with curl:\r\n\r\n```bash\r\ncurl -X GET \"http://localhost:8000/example?name=Connor\u0026age=18\"\r\n```\r\n\r\nthe response is:\r\n\r\n```json\r\n{\r\n  \"ctx\": {\r\n    \"name\": \"Connor\",\r\n    \"age\": 18\r\n  },\r\n  \"params\": {\r\n    \"name\": \"Connor\",\r\n    \"age\": 18\r\n  }\r\n}\r\n```\r\n\r\nand the console output is:\r\n\r\n```bash\r\nctx: {'name': 'Connor', 'age': 18}\r\nparams: {'name': 'Connor', 'age': 18}\r\nname: Connor\r\nage: 18\r\n```\r\n\r\nmore pydantic usage can see\r\n[pydantic documentation](https://pydantic-docs.helpmanual.io/usage/models/)。\r\n\r\nmore sanic-dantic advanced usage can see\r\n[sanic-dantic documentation](https://miss85246.github.io/sanic-dantic/)。\r\n\r\n## License\r\n\r\nsanic-dantic is licensed under the GPL-3.0 License. If you want to use\r\nsanic-dantic for secondary development or commercial use, please follow the\r\nGPL-3.0 License.\r\n\r\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmiss85246%2Fsanic-dantic","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmiss85246%2Fsanic-dantic","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmiss85246%2Fsanic-dantic/lists"}