{ "info": { "author": "Ben-hur Santos Ott", "author_email": "ben-hur@outlook.com", "bugtrack_url": null, "classifiers": [ "License :: OSI Approved :: MIT License", "Operating System :: OS Independent", "Programming Language :: Python :: 3" ], "description": "# py_schema\n\nA simple and extensible schema validator.\n\n![travis](https://travis-ci.org/benhurott/py_schema.svg?branch=master)\n[![codecov](https://codecov.io/gh/benhurott/py_schema/branch/master/graph/badge.svg)](https://codecov.io/gh/benhurott/py_schema)\n\n## Install\n\n```\npip install py-schema\n```\n\n## Usage\n\n```python\nfrom py_schema import SchemaValidator, SchemaValidationError\nfrom py_schema import DictField, ListField, StrField, IntField\n\n\nschema = DictField(\n schema={\n 'name': StrField(min_length=2, max_length=50),\n 'age': IntField(min=0, max=130),\n 'pets': ListField(\n min_items=1,\n item_schema=StrField()\n )\n }\n)\n\nvalue = {\n 'name': 'Bruce',\n 'age': 40,\n 'pets': ['Billy', False]\n}\n\ntry:\n validator = SchemaValidator(\n schema=schema,\n value=value\n )\n\n validator.validate()\n\nexcept SchemaValidationError as err:\n print(err.code) # STR_TYPE\n print(err.path) # $root.pets.$1\n\n```\n\nYou can check the complete list of errors in each field spec.\n\n\n## Fields\n\n\n### BaseField\n\nThis is the abstract field that all the fields inherit from.\n\nIt has some shared props that you can use in all fields.\n\n#### required\n\nIf marked as `True` (default) and the value is `None`, it will raise a `REQUIRED_VALUE` error.\n\n```python\nfrom py_schema import SchemaValidator, SchemaValidationError, StrField\n\ntry:\n schema = StrField(required=True)\n value = None\n\n validator = SchemaValidator(schema, value)\n validator.validate()\nexcept SchemaValidationError as err:\n print(err.code) # \"REQUIRED_VALUE\"\n\n```\n\n\n### IntField\n\nValidate if the value is an integer.\n\n```python\nfrom py_schema import SchemaValidator, IntField\n\n\nschema = IntField(\n min=1,\n max=100\n)\n\nvalue = 80\n\nvalidator = SchemaValidator(schema, value)\n\nvalidator.validate()\n```\n\n#### min (int, optional, default None)\n\nIf provided, it will validate if the value is >= of the `min`.\n\nIf not, it will raise a `INT_MIN` error.\n\n#### max (int, optional, default None)\n\nIf provided, it will validate if the value is <= of the `max`.\n\nIf not, it will raise a `INT_MAX` error.\n\n\n### StrField\n\nValidate if the value is a string.\n\n```python\nfrom py_schema import SchemaValidator, StrField\n\n\nschema = StrField(\n min_length=2,\n max_length=50\n)\n\nvalue = 'Luca Turilli'\n\nvalidator = SchemaValidator(schema, value)\n\nvalidator.validate()\n\n```\n\n#### min_length (int, optional, default None)\n\nIf provided, it will validate if the string len is >= of the `min_length`.\n\nIf not, it will raise a `STR_MIN_LENGTH` error.\n\n\n#### max_length (int, optional, default None)\n\nIf provided, it will validate if the string len is <= of the `max_length`.\n\nIf not, it will raise a `STR_MAX_LENGTH` error.\n\n\n### FloatField\n\nValidate if the value is a float.\n\n```python\nfrom py_schema import SchemaValidator, FloatField\n\nschema = FloatField(\n min=0.0,\n max=99.0\n)\n\nvalue = 50.0\n\nvalidator = SchemaValidator(schema, value)\n\nvalidator.validate()\n\n```\n\n#### min (int, optional, default None)\n\nIf provided, it will validate if the value is >= of the `min`.\n\nIf not, it will raise a `FLOAT_MIN` error.\n\n#### max (int, optional, default None)\n\nIf provided, it will validate if the value is <= of the `max`.\n\nIf not, it will raise a `FLOAT_MAX` error.\n\n\n### BoolField\n\nValidate if the value is a bool.\n\n```python\nfrom py_schema import SchemaValidator, BoolField\n\nschema = BoolField()\n\nvalue = False\n\nvalidator = SchemaValidator(schema, value)\n\nvalidator.validate()\n\n```\n\n\n### DictField\n\nValidate if the value is a dict and each field inside it.\n\n```python\nfrom py_schema import SchemaValidator, DictField, StrField, BoolField\n\n\nschema = DictField(\n schema={\n 'name': StrField(),\n 'admin': BoolField(),\n 'contacts': DictField(\n schema={\n 'phone': StrField(),\n 'email': StrField()\n },\n optional_props=['phone']\n )\n },\n strict=True,\n optional_props=['contacts']\n)\n\nvalue = {\n 'name': 'Dargor The Lord',\n 'admin': True,\n 'contacts': {\n 'email': 'dargor@blackmountain.com'\n }\n}\n\nvalidator = SchemaValidator(schema, value)\n\nvalidator.validate()\n```\n\n#### schema (dict, required)\n\nThe definition of the dictionary.\n\n\n#### strict (bool, optional, default False)\n\nIf the schema should reject dictionary keys that is not present in the schema.\n\nFor example:\n\n```python\nfrom py_schema import SchemaValidator, SchemaValidationError, DictField, StrField, BoolField\n\ntry:\n schema = DictField(\n schema={\n 'name': StrField(),\n 'admin': BoolField()\n },\n strict=True\n )\n\n value = {\n 'name': 'Dargor The Lord',\n 'admin': True,\n 'contacts': {\n 'email': 'dargor@blackmountain.com'\n }\n }\n\n validator = SchemaValidator(schema, value)\n validator.validate()\n\nexcept SchemaValidationError as err:\n print(err.code) # DICT_PROP_NOT_ALLOWED\n print(err.extra) # {'prop': 'contacts'}\n\n```\n\nIn this case, the `contacts` property in the value is not present in the schema.\n\nIt will raise a `DICT_PROP_NOT_ALLOWED`.\n\n\n#### optional_props ([str], optional, default [])\n\nThis prop indicates which properties are optional in schema.\n\nIf a prop is in this list and it's not in the value, it will be ignored.\n\nOtherwise, it will raise a `DICT_PROP_MISSING` error.\n\nExample:\n\n```python\nfrom py_schema import SchemaValidator, DictField, StrField, BoolField\n\nschema = DictField(\n schema={\n 'name': StrField(),\n 'admin': BoolField(),\n 'gender': StrField() \n },\n optional_props=['gender']\n)\n\nvalue = {\n 'name': 'Dargor The Lord',\n 'admin': True\n}\n\nvalidator = SchemaValidator(schema, value)\nvalidator.validate() # valid\n```\n\n```python\nfrom py_schema import SchemaValidator, SchemaValidationError, DictField, StrField, BoolField\n\ntry:\n schema = DictField(\n schema={\n 'name': StrField(),\n 'admin': BoolField()\n }\n )\n\n value = {\n 'name': 'Dargor The Lord'\n }\n\n validator = SchemaValidator(schema, value)\n validator.validate()\n\nexcept SchemaValidationError as err:\n print(err.code) # DICT_PROP_MISSING\n print(err.extra) # {'prop': 'admin'}\n\n```\n\n\n### ListField\n\nValidate if the value is a list and the items inside it.\n\n```python\nfrom py_schema import SchemaValidator, ListField, StrField\n\n\nschema = ListField(\n min_items=1,\n max_items=3,\n item_schema=StrField()\n)\n\nvalue = ['Emerald', 'Sword']\n\nvalidator = SchemaValidator(schema, value)\nvalidator.validate()\n\n```\n\n#### item_schema (BaseField, required)\n\nThe schema for the item inside the list.\n\n\n#### min_items (int, optional, default None)\n\nValidate if the list contain at minimun `min_items` length.\n\nIf not, it will raise `LIST_MIN_ITEMS` error.\n\n\n#### max_items (int, optional, default None)\n\nValidate if the list contain at maximum `max_items` length.\n\nIf not, it will raise `LIST_MAX_ITEMS` error.\n\n\n\n### EnumField\n\nValidate if the value is one of the allowed values.\n\n\n```python\nfrom py_schema import SchemaValidator, EnumField\n\n\nschema = EnumField(\n accept=[1, True, 'Immortal']\n)\n\nvalue = 1\n\nvalidator = SchemaValidator(schema, value)\nvalidator.validate()\n\n```\n\n#### accept (list, required)\n\nThe list of the values that can be accepted.\n\nIf the value is not in the `accepted`, it will raise a `ENUM_VALUE_NOT_ACCEPT` error.\n\n\n\n### Regex Field\n\nValidate if the value matches the regex.\n\n```python\nfrom py_schema import SchemaValidator, RegexField\n\nschema = RegexField(regex='\\\\d{5}\\\\Z')\n\nvalue = '12345'\n\nvalidator = SchemaValidator(schema, value)\n\nvalidator.validate()\n\n```\n\n#### regex (str, required)\n\nThe regex pattern.\n\n\n### OR Field\n\nValidate if the value matches with at least one of given schemas.\n\n```python\nfrom py_schema import OrField, StrField, BoolField, IntField, SchemaValidator, SchemaValidationError\n\nschema = OrField(\n schemas=[\n StrField(),\n IntField()\n ]\n)\n\nvalue = True\n\nvalidator = SchemaValidator(schema, value)\n\ntry:\n validator.validate()\nexcept SchemaValidationError as e:\n print(e.extra['errors'])\n```\n\n#### schemas ([BaseField], required)\n\nThe list of the schemas to validate. \nIf all the schemas failed, it will raise a `OR_NO_MATCHING_SCHEMA` error.\n\nIf the validation fail, you can check the error prop `extra['errors']` to see all the validation results.\n\n\n\n## Misc\n\n### SchemaValidationError\n\nIf a validation fails, it will raise a `SchemaValidationError`.\n\nInside the error will will have:\n\n```python\nfrom py_schema import SchemaValidationError\n\ntry:\n # ....\n pass\nexcept SchemaValidationError as err:\n print(err.code) # The code of the error\n print(err.path) # The path in the schema that the error occurred.\n print(err.node) # The BaseField node where the validation was raised.\n print(err.extra) # Any extra argument of the error.\n```\n\n\n## Creating custom validators\n\nFor better context, let's use this sample:\n\n```python\nfrom py_schema import DictField\nfrom .my_field import MyField\n\n\nschema = DictField(\n schema={\n 'my_field': MyField()\n }\n)\n\nvalue = {\n 'my_field': 'Avalon'\n}\n\n```\n\n```python\nfrom py_schema import BaseField\n\n\nclass MyField(BaseField):\n def validator(self):\n ctx = self.ctx # the current SchemaValidator instance\n value = self.value # here is the current value of the schema (in this sample: \"Avalon\")\n\n if value != 'Avalon': # create you custom validation\n self.raise_error( # if your validation fails, raise an error\n code='MY_CUSTOM_CODE',\n extra=\"Any other extra info for your error (optional)\"\n )\n```\n\n\nAnd that's it =).\n\n", "description_content_type": "text/markdown", "docs_url": null, "download_url": "", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/benhurott/py_schema", "keywords": "", "license": "", "maintainer": "", "maintainer_email": "", "name": "py-schema", "package_url": "https://pypi.org/project/py-schema/", "platform": "", "project_url": "https://pypi.org/project/py-schema/", "project_urls": { "Homepage": "https://github.com/benhurott/py_schema" }, "release_url": "https://pypi.org/project/py-schema/0.12.0/", "requires_dist": null, "requires_python": "", "summary": "A simple and extensible schema validator.", "version": "0.12.0" }, "last_serial": 5631765, "releases": { "0.0.1": [ { "comment_text": "", "digests": { "md5": "6834774186eccdb446ac12be431733ec", "sha256": "35416086d83b5b6e4a44c89a93c14b09cf29528bc4adb0ad94ffec95814808dd" }, "downloads": -1, "filename": "py_schema-0.0.1-py3-none-any.whl", "has_sig": false, "md5_digest": "6834774186eccdb446ac12be431733ec", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 2008, "upload_time": "2019-07-28T15:16:36", "url": "https://files.pythonhosted.org/packages/9d/25/a930a1685c9a381cfc5616c185a73ac2c592e5b8e3d7f5df44cdc8690390/py_schema-0.0.1-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "ca9a5cdb1f804ac5ca2ec0260e172a98", "sha256": "4c403cb66227331710ec295e72e93f0fd042b3873c86478bc3f9c7bce035cd21" }, "downloads": -1, "filename": "py_schema-0.0.1.tar.gz", "has_sig": false, "md5_digest": "ca9a5cdb1f804ac5ca2ec0260e172a98", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 918, "upload_time": "2019-07-28T15:16:42", "url": "https://files.pythonhosted.org/packages/24/db/9e2a6cc208691d50ae82f88e1e0d2ef45b77107933b8abf5dc3aa62d1e9b/py_schema-0.0.1.tar.gz" } ], "0.10.0": [ { "comment_text": "", "digests": { "md5": "3549516f29750c479747f9cae12ef671", "sha256": "e4234e48b2750f6432dcdcf4acd93f707bc8e96c0b8172cb2694dd667f8fbc45" }, "downloads": -1, "filename": "py_schema-0.10.0-py3-none-any.whl", "has_sig": false, "md5_digest": "3549516f29750c479747f9cae12ef671", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 4046, "upload_time": "2019-07-28T15:16:39", "url": "https://files.pythonhosted.org/packages/6d/b9/69fa9902cfd2ba63ca75d5b189008948832f2775611fe1cf71b9d83075ae/py_schema-0.10.0-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "76b8880ec454156933c1212d1ba9d8fd", "sha256": "5da11665d35aed4f0572d6b1ed35397d6fa7554fc5d1b826870181466f152f96" }, "downloads": -1, "filename": "py_schema-0.10.0.tar.gz", "has_sig": false, "md5_digest": "76b8880ec454156933c1212d1ba9d8fd", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 4019, "upload_time": "2019-07-28T15:16:43", "url": "https://files.pythonhosted.org/packages/c2/1e/da4a44eb58a3b4ae48912dff1d73e992e7fcd73027209699a002cd699070/py_schema-0.10.0.tar.gz" } ], "0.10.1": [ { "comment_text": "", "digests": { "md5": "b3c859d7eb879c50a2da6a7b1b25242c", "sha256": "08ca42f62967f56e5d33994dbb7ee99d9e8997c3c477894d12acbaa584ccf15f" }, "downloads": -1, "filename": "py_schema-0.10.1-py3-none-any.whl", "has_sig": false, "md5_digest": "b3c859d7eb879c50a2da6a7b1b25242c", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 7327, "upload_time": "2019-07-28T15:30:55", "url": "https://files.pythonhosted.org/packages/5b/ac/4a9fa7f96b807061f19432bfcdf6313a5345409090ef9717f5588e142c28/py_schema-0.10.1-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "b91939493991b3db525c9c5250e1adef", "sha256": "c031d92bf66a4b990c39e7b4f10642231614a4a309a59960363358a2a740e4fc" }, "downloads": -1, "filename": "py_schema-0.10.1.tar.gz", "has_sig": false, "md5_digest": "b91939493991b3db525c9c5250e1adef", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 8192, "upload_time": "2019-07-28T15:30:57", "url": "https://files.pythonhosted.org/packages/3b/a2/bd2c923a1708f735b57d480515593d567ba4fc9e573bfbb185e9da906879/py_schema-0.10.1.tar.gz" } ], "0.11.0": [ { "comment_text": "", "digests": { "md5": "e0bf7ac84cf44ebe16a6e022824442ad", "sha256": "22dbdf99bed76258e8ddf595e861f2be2e564dac5e78ae1e6e570f62f21fdeb7" }, "downloads": -1, "filename": "py_schema-0.11.0-py3-none-any.whl", "has_sig": false, "md5_digest": "e0bf7ac84cf44ebe16a6e022824442ad", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 7617, "upload_time": "2019-07-28T19:39:40", "url": "https://files.pythonhosted.org/packages/e7/63/541cc5cc2049780d5994cc1cee518fd5999afcb54c8cfe3d51bf586584f0/py_schema-0.11.0-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "03ba9cd45e1983f7a97b2097f0859df2", "sha256": "a870b07082c54bb4d536c96119af05f59d03da3266f1ae11354f73f2b6e1418e" }, "downloads": -1, "filename": "py_schema-0.11.0.tar.gz", "has_sig": false, "md5_digest": "03ba9cd45e1983f7a97b2097f0859df2", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 8752, "upload_time": "2019-07-28T19:39:42", "url": "https://files.pythonhosted.org/packages/e3/1d/e98b1444700c4571afc198b06c33b0d6a98d67c155af19dc5ad5aea19606/py_schema-0.11.0.tar.gz" } ], "0.12.0": [ { "comment_text": "", "digests": { "md5": "08b1dff5946b7514899d44c7a79091c6", "sha256": "ced23cafc8860a8537ea51504d371a1de53c1575abe289f0101a7077b09652fc" }, "downloads": -1, "filename": "py_schema-0.12.0-py3-none-any.whl", "has_sig": false, "md5_digest": "08b1dff5946b7514899d44c7a79091c6", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 8201, "upload_time": "2019-08-04T22:31:26", "url": "https://files.pythonhosted.org/packages/0b/c0/4511001a828a1658ac76235ac5887c60f5ee997477ee429ecd8531b6aa57/py_schema-0.12.0-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "7a5fde7276b6cb39aeb0e381b12a8515", "sha256": "9e00e07cde91fdeb8c6e73faeb35bc91f379caabd92f0b3162560d93442e914e" }, "downloads": -1, "filename": "py_schema-0.12.0.tar.gz", "has_sig": false, "md5_digest": "7a5fde7276b6cb39aeb0e381b12a8515", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 9479, "upload_time": "2019-08-04T22:31:28", "url": "https://files.pythonhosted.org/packages/eb/21/e651d6c15daebae038263729c33c0864c94fca4a4366ef5f81a090a80602/py_schema-0.12.0.tar.gz" } ], "0.9.0": [ { "comment_text": "", "digests": { "md5": "6bc8fc1caded138e2d7f3eb1c3014924", "sha256": "150e23eee21c67b9da6037c8a0e21c5943cb14db0646b6a43f2d73dd5e0a1180" }, "downloads": -1, "filename": "py_schema-0.9.0-py3-none-any.whl", "has_sig": false, "md5_digest": "6bc8fc1caded138e2d7f3eb1c3014924", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 3765, "upload_time": "2019-07-28T15:16:41", "url": "https://files.pythonhosted.org/packages/9a/41/f8ec75f391ebc5ff84cd22836a2486950c889a41f804161e86cdbfea8ea1/py_schema-0.9.0-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "b89a9b7a24a265315c9d93b8c30e52fc", "sha256": "35bff1f900780bd854592ef75a67eff9b0edf4351195f18652ca7f19ecd68fa3" }, "downloads": -1, "filename": "py_schema-0.9.0.tar.gz", "has_sig": false, "md5_digest": "b89a9b7a24a265315c9d93b8c30e52fc", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 3614, "upload_time": "2019-07-28T15:16:45", "url": "https://files.pythonhosted.org/packages/3f/03/bfc4bc01d1893c40dcfb89f10d88f1a5b722ee90c588e8da6513ac6ca038/py_schema-0.9.0.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "08b1dff5946b7514899d44c7a79091c6", "sha256": "ced23cafc8860a8537ea51504d371a1de53c1575abe289f0101a7077b09652fc" }, "downloads": -1, "filename": "py_schema-0.12.0-py3-none-any.whl", "has_sig": false, "md5_digest": "08b1dff5946b7514899d44c7a79091c6", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 8201, "upload_time": "2019-08-04T22:31:26", "url": "https://files.pythonhosted.org/packages/0b/c0/4511001a828a1658ac76235ac5887c60f5ee997477ee429ecd8531b6aa57/py_schema-0.12.0-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "7a5fde7276b6cb39aeb0e381b12a8515", "sha256": "9e00e07cde91fdeb8c6e73faeb35bc91f379caabd92f0b3162560d93442e914e" }, "downloads": -1, "filename": "py_schema-0.12.0.tar.gz", "has_sig": false, "md5_digest": "7a5fde7276b6cb39aeb0e381b12a8515", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 9479, "upload_time": "2019-08-04T22:31:28", "url": "https://files.pythonhosted.org/packages/eb/21/e651d6c15daebae038263729c33c0864c94fca4a4366ef5f81a090a80602/py_schema-0.12.0.tar.gz" } ] }