{"id":43589768,"url":"https://github.com/ausaki/python-validator","last_synced_at":"2026-02-04T01:54:52.986Z","repository":{"id":54730818,"uuid":"142562539","full_name":"ausaki/python-validator","owner":"ausaki","description":"a data validator like Django ORM","archived":false,"fork":false,"pushed_at":"2023-07-17T11:52:18.000Z","size":1056,"stargazers_count":43,"open_issues_count":3,"forks_count":4,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-10-28T05:12:08.791Z","etag":null,"topics":["data","python","python-validator","schema","validate","validation","validation-library","validator"],"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/ausaki.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":"2018-07-27T10:17:31.000Z","updated_at":"2025-01-06T10:24:47.000Z","dependencies_parsed_at":"2022-08-14T01:10:09.601Z","dependency_job_id":null,"html_url":"https://github.com/ausaki/python-validator","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"purl":"pkg:github/ausaki/python-validator","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ausaki%2Fpython-validator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ausaki%2Fpython-validator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ausaki%2Fpython-validator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ausaki%2Fpython-validator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ausaki","download_url":"https://codeload.github.com/ausaki/python-validator/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ausaki%2Fpython-validator/sbom","scorecard":{"id":216643,"data":{"date":"2025-08-11","repo":{"name":"github.com/ausaki/python-validator","commit":"c795b038b53cb54adf4acceb223b156eb903002c"},"scorecard":{"version":"v5.2.1-40-gf6ed084d","commit":"f6ed084d17c9236477efd66e5b258b9d4cc7b389"},"score":1.7,"checks":[{"name":"Packaging","score":-1,"reason":"packaging workflow not detected","details":["Warn: no GitHub/GitLab publishing workflow detected."],"documentation":{"short":"Determines if the project is published as a package that others can easily download, install, easily update, and uninstall.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#packaging"}},{"name":"Maintained","score":0,"reason":"0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0","details":null,"documentation":{"short":"Determines if the project is \"actively maintained\".","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#maintained"}},{"name":"Dangerous-Workflow","score":-1,"reason":"no workflows found","details":null,"documentation":{"short":"Determines if the project's GitHub Action workflows avoid dangerous patterns.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#dangerous-workflow"}},{"name":"Token-Permissions","score":-1,"reason":"No tokens found","details":null,"documentation":{"short":"Determines if the project's workflows follow the principle of least privilege.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#token-permissions"}},{"name":"Pinned-Dependencies","score":-1,"reason":"no dependencies found","details":null,"documentation":{"short":"Determines if the project has declared and pinned the dependencies of its build process.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#pinned-dependencies"}},{"name":"Code-Review","score":0,"reason":"Found 1/29 approved changesets -- score normalized to 0","details":null,"documentation":{"short":"Determines if the project requires human code review before pull requests (aka merge requests) are merged.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#code-review"}},{"name":"Binary-Artifacts","score":10,"reason":"no binaries found in the repo","details":null,"documentation":{"short":"Determines if the project has generated executable (binary) artifacts in the source repository.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#binary-artifacts"}},{"name":"CII-Best-Practices","score":0,"reason":"no effort to earn an OpenSSF best practices badge detected","details":null,"documentation":{"short":"Determines if the project has an OpenSSF (formerly CII) Best Practices Badge.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#cii-best-practices"}},{"name":"Security-Policy","score":0,"reason":"security policy file not detected","details":["Warn: no security policy file detected","Warn: no security file to analyze","Warn: no security file to analyze","Warn: no security file to analyze"],"documentation":{"short":"Determines if the project has published a security policy.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#security-policy"}},{"name":"Fuzzing","score":0,"reason":"project is not fuzzed","details":["Warn: no fuzzer integrations found"],"documentation":{"short":"Determines if the project uses fuzzing.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#fuzzing"}},{"name":"License","score":10,"reason":"license file detected","details":["Info: project has a license file: LICENSE:0","Info: FSF or OSI recognized license: MIT License: LICENSE:0"],"documentation":{"short":"Determines if the project has defined a license.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#license"}},{"name":"Signed-Releases","score":-1,"reason":"no releases found","details":null,"documentation":{"short":"Determines if the project cryptographically signs release artifacts.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#signed-releases"}},{"name":"Branch-Protection","score":0,"reason":"branch protection not enabled on development/release branches","details":["Warn: branch protection not enabled for branch 'master'"],"documentation":{"short":"Determines if the default and release branches are protected with GitHub's branch protection settings.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#branch-protection"}},{"name":"SAST","score":0,"reason":"SAST tool is not run on all commits -- score normalized to 0","details":["Warn: 0 commits out of 2 are checked with a SAST tool"],"documentation":{"short":"Determines if the project uses static code analysis.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#sast"}},{"name":"Vulnerabilities","score":0,"reason":"22 existing vulnerabilities detected","details":["Warn: Project is vulnerable to: PYSEC-2022-42991 / GHSA-v3c5-jqr6-7qm8","Warn: Project is vulnerable to: GHSA-cpwx-vrp4-4pq7","Warn: Project is vulnerable to: GHSA-h5c8-rqwp-cp95","Warn: Project is vulnerable to: GHSA-h75v-3vvj-5mfj","Warn: Project is vulnerable to: GHSA-q2x7-8rv6-6q7h","Warn: Project is vulnerable to: PYSEC-2022-288 / GHSA-6hrg-qmvc-2xh8","Warn: Project is vulnerable to: PYSEC-2021-356 / GHSA-2ww3-fxvq-293j","Warn: Project is vulnerable to: PYSEC-2024-167 / GHSA-cgvx-9447-vcch","Warn: Project is vulnerable to: PYSEC-2021-859 / GHSA-f8m6-h2c7-8h9x","Warn: Project is vulnerable to: PYSEC-2022-5 / GHSA-rqjh-jp2r-59cj","Warn: Project is vulnerable to: PYSEC-2022-42969","Warn: Project is vulnerable to: PYSEC-2023-117 / GHSA-mrwq-x4v8-fh7p","Warn: Project is vulnerable to: GHSA-jh85-wwv9-24hv","Warn: Project is vulnerable to: GHSA-753j-mpmx-qq6g","Warn: Project is vulnerable to: GHSA-7cx3-6m66-7c5m","Warn: Project is vulnerable to: GHSA-8w49-h785-mj3c","Warn: Project is vulnerable to: PYSEC-2023-75 / GHSA-hj3f-6gcp-jg8j","Warn: Project is vulnerable to: GHSA-qppv-j76h-2rpx","Warn: Project is vulnerable to: GHSA-w235-7p84-xx57","Warn: Project is vulnerable to: GHSA-g7vv-2v7x-gj9p","Warn: Project is vulnerable to: PYSEC-2024-187 / GHSA-rqc4-2hc7-8c8v","Warn: Project is vulnerable to: GHSA-jfmj-5v4g-7637"],"documentation":{"short":"Determines if the project has open, known unfixed vulnerabilities.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#vulnerabilities"}}]},"last_synced_at":"2025-08-17T01:46:54.483Z","repository_id":54730818,"created_at":"2025-08-17T01:46:54.483Z","updated_at":"2025-08-17T01:46:54.483Z"},"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29064097,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-04T00:26:14.114Z","status":"ssl_error","status_checked_at":"2026-02-04T00:23:06.435Z","response_time":96,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["data","python","python-validator","schema","validate","validation","validation-library","validator"],"created_at":"2026-02-04T01:54:52.506Z","updated_at":"2026-02-04T01:54:52.977Z","avatar_url":"https://github.com/ausaki.png","language":"Python","readme":"# python-validator\n\npython-validator 是一个类似于 Django ORM 的数据校验库，适用与任何需要进行数据校验的应用，比较常见的是 Web 后端校验前端的输入数据。\n\n[文档](https://ausaki.github.io/python-validator)\n\n---\n\n[![PyPi](https://img.shields.io/pypi/v/python-validator.svg)\n](https://pypi.org/project/python-validator/)\n[![PyPI - Status](https://img.shields.io/pypi/status/python-validator.svg)](https://pypi.org/project/python-validator/)\n[![PyPi - Python Version](https://img.shields.io/pypi/pyversions/python-validator.svg)](https://pypi.org/project/python-validator/)\n[![PyPI - License](https://img.shields.io/pypi/l/python-validator.svg)](https://pypi.org/project/python-validator/)\n\n![GitHub last commit](https://img.shields.io/github/last-commit/ausaki/python-validator.svg)\n[![Build Status - master](https://travis-ci.org/ausaki/python-validator.svg?branch=master)](https://travis-ci.org/ausaki/python-validator)\n\n---\n\n## 特性\n\n- 支持 python2 和 python3。\n\n- 使用类描述数据结构，数据字段一目了然。另外也支持使用字典定义数据结构。\n\n- 可以自动生成用于测试的 mocking data。\n\n- 可以打印出清晰的数据结构。\n\n- 易于扩展。\n\n\n## 依赖\n\n- six\n\n- IPy\n\n- pytz[可选，`DatetimeField` 的 `tzinfo` 参数需要一个 `tzinfo` 对象]\n\n\n## 安装\n\n`pip install python-validator`\n\n\n## 快速入门\n\n假设现在正在开发一个上传用户信息的接口 `POST /api/user/`，用户信息如下：\n\n| 字段 | 类型 | 描述 |\n|--|--|--|\n|name|String| 必选 |\n|age|integer| 可选，默认 20|\n|sex|String, 'f'表示女, 'm'表示男 | 可选, 默认 None|\n\n原始的、枯燥无味的、重复性劳动的数据校验代码可能是下面这样：\n\n```python\ndef user(request):\n    # data = json.loads(request.body)\n    data = {\n        'age': '24f',\n        'sex': 'f'\n    }\n    name = data.get('name')\n    age = data.get('age', 20)\n    sex = dage.get('sex')\n\n    if name is None or len(name) == 0:\n        return Response('必须提供 name', status=400)\n\n    try:\n        age = int(age)\n    except ValueError as e:\n        return Response('age 格式错误', status=400)\n\n    if sex is not None and sex not in ('f', 'm'):\n        return Response('sex 格式错误', status=400)\n\n    user_info = {\n        'name': name,\n        'age': age,\n        'sex': sex,\n    }\n    ...\n```\n\n上面这段代码总的来说有几个问题：\n\n- 枯燥无味和重复性代码，不断的取出数据，检查字段是否缺失，类型是否合法等等。\n\n- 从数据校验的代码无法轻易看出用户信息的数据结构，即字段是什么类型的，是否可选，默认值是什么。\n\n**使用 python-validator 校验数据**\n\n首先定义一个 UserInfoValidator 类\n\n```python\n# validators.py\nfrom validator import Validator, StringField, IntegerField, EnumField\n\nclass UserInfoValidator(Validator):\n    name = StringField(max_length=50, required=True)\n    age = IntegerField(min_value=1, max_value=120, default=20)\n    sex = EnumField(choices=['f', 'm'])\n```\n\n接下来使用 `UserInfoValidator` 进行数据校验，\n\n```python\nfrom .validators import UserInfoValidator\n\ndef user(request):\n    # data = json.loads(request.body)\n    data = {\n        'age': '24',\n        'sex': 'f'\n    }\n    v = UserInfoValidator(data)\n    if not v.is_valid():\n        return Response({'msg': v.str_errors, 'code': 400}, status=400)\n\n    user_info = v.validated_data\n    ...\n```\n\n`v.str_errors` 是一个字段名 - 错误信息的 dict，例如：\n\n```python\n{'age': 'got a wrong type: str, expect integer', 'name': 'Field is required'}\n```\n\n错误信息解释：\n\n- `age` 等于 \"24\"，不是合法的 `int` 类型。\n\n- `name` 是必须提供的，且没有指定默认值。\n\n\nv.validated_data 是校验后合法的数据，例如：\n\n```json\n{'age': 24, 'name': u'Michael', 'sex': 'f'}\n```\n\n下面是一些错误数据的例子：\n\n```python\ndata:  {'age': 24, 'name': 'abcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabc', 'sex': 'f'}\nis_valid: False\nerrors: {'name': 'string is too long, max-lenght is 50'}\nvalidated_data: None\n```\n\n```python\ndata:  {'age': 24, 'name': 'Michael', 'sex': 'c'}\nis_valid: False\nerrors: {'sex': \"'c' not in the choices\"}\nvalidated_data: None\n```\n\n细心的同学可能发现了 `IntegerField` 不接受 “数字字符串”，上面的例子中 `age` 是一个 `IntegerField`，形如 `'24'` 这样的值是非法的。在某些情况下，你可能希望 `IntegerField` 不要这么严格，`'24'` 这样的值也是可以接受的，那么可以把 `strict` 选项设为 `False`，如：`age = IntegerField(min_value=1, max_value=120, default=20, strict=False)`。当 `strict` 选项为 `False` 时，python-validator 会尝试进行类型转换，假如转换失败则会报错。\n\n接下来你可以 [查看进阶](https://ausaki.github.io/python-validator/advanced/) 了解 python-validator 更多的用法，[查看字段 API](https://ausaki.github.io/python-validator/fields/) 了解字段的详细信息。\n\n\n## 测试\n\n使用 tox 和 pytest 进行代码测试。\n\n推荐使用 [pipenv](https://github.com/pypa/pipenv) 来管理项目依赖。\n\n**假如使用 pipenv：**\n\n- `pipenv install`(安装依赖库)\n\n- `pipenv run test`\n\n**假如使用 pip：**\n\n- `pip install -r requirements.txt`(安装依赖库)\n\n- `tox`\n\n\n## 其它\n\n如果遇到 BUG 或者有任何建议，欢迎提交 issue 或者 PR。\n\n如果希望贡献代码，请尽量编写测试用例并测试你的代码，然后再提交 PR。","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fausaki%2Fpython-validator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fausaki%2Fpython-validator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fausaki%2Fpython-validator/lists"}