PySimpleValidate


NamePySimpleValidate JSON
Version 0.2.12 PyPI version JSON
download
home_pagehttps://github.com/asweigart/pysimplevalidate
SummaryA collection of string-based validation functions, suitable for use in other Python 2 and 3 applications.
upload_time2020-07-09 19:58:55
maintainer
docs_urlNone
authorAl Sweigart
requires_python
licenseBSD
keywords input validation text string
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage
            pysimplevalidate
================

A collection of string-based validation functions, suitable for use in other Python 2 and 3 applications.

Pass a string to these validation functions, which raise ValidationException if validation fails. Otherwise they return a platonic value of the validated string (i.e. the `validateInt('42')` returns the int `42`).


Installation
------------

    pip install pysimplevalidate

About
-----

PySimpleValidate provides several functions to perform common input validation.
The validate* functions in this module accept a *value* argument and raise a
*ValidationException* if it doesn't pass validation.

If *value* was valid, the function returns. The return value is the form of
the value that the validation function has accepted as value.
This could include any transformations such as stripping whitespace from the ends.

The following (hopefully self-descriptive) validation functions are implemented
in this module:

* validateNum()
* validateInt()
* validateFloat()
* validateChoice()
* validateDate()
* validateTime()
* validateDatetime()
* validateRegex()
* validateRegexStr()
* validateEmail()
* validateURL()
* validateYesNo()
* validateState()
* validateMonth()
* validateDayOfWeek()
* validateDayOfMonth()

These validation functions have the following common parameters:

* *value*: (str) The value being validated.
* *blank*: (bool) If False, a blank string is considered valid. Defaults to False.
* *strip*: (bool, str, None) If omitted or None, whitespace is stripped from value. If a string, the string's characters are stripped from value. If False, nothing is stripped.
* *allowlistRegexes*: (Sequence, None) A sequence of regex str that will explicitly pass validation, even if they aren't numbers. Defaults to None.
* *blocklistRegexes*: (Sequence, None) A sequence of regex str or (regex_str, response_str) tuples that, if matched, will explicitly fail validation. Defaults to None.

Further, the text-based validators have the following common parameters:

* caseSensitive (bool): If True, value must match the exact casing of an acceptable response. If False, any casing can be used. Defaults to False.

Quickstart
----------

PySimpleValidate's validation functions will raise *ValidationException* if
the value passed to them fails validation. Otherwise, a cleaned up version of
the value is returned.

It's recommended to import PySimpleValidation with the shorter name ``pysv``.

    >>> import pysimplevalidate as pysv
    >>> pysv.validateStr('I have a cat', allowlistRegexes=['caterpillar', 'cat(.*?)dog'], blocklistRegexes=['cat', 'm(o){2:}se'])
    Traceback (most recent call last):
        ...
    pysimplevalidate.ValidationException: This response is invalid.
    >>> pysv.validateStr('I have a caterpillar', allowlistRegexes=['caterpillar', 'cat(.*?)dog'], blocklistRegexes=['cat', 'm(o){2:}se'])
    'I have a caterpillar'
    >>> pysv.validateStr('I have a cat and a dog', allowlistRegexes=['caterpillar', 'cat(.*?)dog'], blocklistRegexes=['cat', 'm(o){2:}se'])
    'I have a cat and a dog'
    >>> pysv.validateStr('I have a mooooose', allowlistRegexes=['caterpillar', 'cat(.*?)dog'], blocklistRegexes=['cat', 'm(o){2:}se'])
    'I have a mooooose'
    >>> pysv.validateNum('42')
    42
    >>> pysv.validateNum('twelve')
    Traceback (most recent call last):
        ...
    pysimplevalidate.ValidationException: 'twelve' is not a number.
    >>>
    >>> pysv.validateNum('5', lessThan=10)
    5
    >>> pysv.validateFloat('4')
    4.0
    >>> pysv.validateFloat('4.12')
    4.12
    >>> pysv.validateInt('4.12')
    Traceback (most recent call last):
        ...
    pysimplevalidate.ValidationException: '4.12' is not an integer.
    >>> pysv.validateChoice('cat', ['dog', 'cat', 'moose'])
    'cat'
    >>> pysv.validateChoice('CAT', ['dog', 'cat', 'moose'])
    'cat'
    >>> pysv.validateTime('12:00:01')
    datetime.time(12, 0, 1)
    >>> pysv.validateTime('hour 12 minute 00', formats=['hour %H minute %M'])
    datetime.time(12, 0)
    >>> pysv.validateEmail('al@inventwithpython.com')
    'al@inventwithpython.com'
    >>> pysv.validateURL('https://inventwithpython.com')
    'https://inventwithpython.com'
    >>> pysv.validateYesNo('y')
    'yes'
    >>> pysv.validateYesNo('NO')
    'no'
    >>> pysv.validateState('California')
    'CA'
    >>> pysv.validateState('TEXAS')
    'TX'
    >>> pysv.validateState('NY')
    'NY'
    >>> pysv.validateDayOfWeek('mon')
    'Monday'
    >>> pysv.validateDayOfWeek('FRIday')
    'Friday'
    >>> pysv.validateDayOfMonth(29, 2004, 2)
    29
    >>> pysv.validateDayOfMonth(31, 2019, 10)
    31
    >>> pysv.validateDayOfMonth(31, 2019, 9)
    Traceback (most recent call last):
        ...
    pysimplevalidate.ValidationException: '31' is not a day in the month of September 2019
            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/asweigart/pysimplevalidate",
    "name": "PySimpleValidate",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "input validation text string",
    "author": "Al Sweigart",
    "author_email": "al@inventwithpython.com",
    "download_url": "https://files.pythonhosted.org/packages/0b/54/8fd99436073dbdbba719e08e1b7e5f3f36a8cc9e518612e1b97e4806d2ce/PySimpleValidate-0.2.12.tar.gz",
    "platform": "",
    "description": "pysimplevalidate\n================\n\nA collection of string-based validation functions, suitable for use in other Python 2 and 3 applications.\n\nPass a string to these validation functions, which raise ValidationException if validation fails. Otherwise they return a platonic value of the validated string (i.e. the `validateInt('42')` returns the int `42`).\n\n\nInstallation\n------------\n\n    pip install pysimplevalidate\n\nAbout\n-----\n\nPySimpleValidate provides several functions to perform common input validation.\nThe validate* functions in this module accept a *value* argument and raise a\n*ValidationException* if it doesn't pass validation.\n\nIf *value* was valid, the function returns. The return value is the form of\nthe value that the validation function has accepted as value.\nThis could include any transformations such as stripping whitespace from the ends.\n\nThe following (hopefully self-descriptive) validation functions are implemented\nin this module:\n\n* validateNum()\n* validateInt()\n* validateFloat()\n* validateChoice()\n* validateDate()\n* validateTime()\n* validateDatetime()\n* validateRegex()\n* validateRegexStr()\n* validateEmail()\n* validateURL()\n* validateYesNo()\n* validateState()\n* validateMonth()\n* validateDayOfWeek()\n* validateDayOfMonth()\n\nThese validation functions have the following common parameters:\n\n* *value*: (str) The value being validated.\n* *blank*: (bool) If False, a blank string is considered valid. Defaults to False.\n* *strip*: (bool, str, None) If omitted or None, whitespace is stripped from value. If a string, the string's characters are stripped from value. If False, nothing is stripped.\n* *allowlistRegexes*: (Sequence, None) A sequence of regex str that will explicitly pass validation, even if they aren't numbers. Defaults to None.\n* *blocklistRegexes*: (Sequence, None) A sequence of regex str or (regex_str, response_str) tuples that, if matched, will explicitly fail validation. Defaults to None.\n\nFurther, the text-based validators have the following common parameters:\n\n* caseSensitive (bool): If True, value must match the exact casing of an acceptable response. If False, any casing can be used. Defaults to False.\n\nQuickstart\n----------\n\nPySimpleValidate's validation functions will raise *ValidationException* if\nthe value passed to them fails validation. Otherwise, a cleaned up version of\nthe value is returned.\n\nIt's recommended to import PySimpleValidation with the shorter name ``pysv``.\n\n    >>> import pysimplevalidate as pysv\n    >>> pysv.validateStr('I have a cat', allowlistRegexes=['caterpillar', 'cat(.*?)dog'], blocklistRegexes=['cat', 'm(o){2:}se'])\n    Traceback (most recent call last):\n        ...\n    pysimplevalidate.ValidationException: This response is invalid.\n    >>> pysv.validateStr('I have a caterpillar', allowlistRegexes=['caterpillar', 'cat(.*?)dog'], blocklistRegexes=['cat', 'm(o){2:}se'])\n    'I have a caterpillar'\n    >>> pysv.validateStr('I have a cat and a dog', allowlistRegexes=['caterpillar', 'cat(.*?)dog'], blocklistRegexes=['cat', 'm(o){2:}se'])\n    'I have a cat and a dog'\n    >>> pysv.validateStr('I have a mooooose', allowlistRegexes=['caterpillar', 'cat(.*?)dog'], blocklistRegexes=['cat', 'm(o){2:}se'])\n    'I have a mooooose'\n    >>> pysv.validateNum('42')\n    42\n    >>> pysv.validateNum('twelve')\n    Traceback (most recent call last):\n        ...\n    pysimplevalidate.ValidationException: 'twelve' is not a number.\n    >>>\n    >>> pysv.validateNum('5', lessThan=10)\n    5\n    >>> pysv.validateFloat('4')\n    4.0\n    >>> pysv.validateFloat('4.12')\n    4.12\n    >>> pysv.validateInt('4.12')\n    Traceback (most recent call last):\n        ...\n    pysimplevalidate.ValidationException: '4.12' is not an integer.\n    >>> pysv.validateChoice('cat', ['dog', 'cat', 'moose'])\n    'cat'\n    >>> pysv.validateChoice('CAT', ['dog', 'cat', 'moose'])\n    'cat'\n    >>> pysv.validateTime('12:00:01')\n    datetime.time(12, 0, 1)\n    >>> pysv.validateTime('hour 12 minute 00', formats=['hour %H minute %M'])\n    datetime.time(12, 0)\n    >>> pysv.validateEmail('al@inventwithpython.com')\n    'al@inventwithpython.com'\n    >>> pysv.validateURL('https://inventwithpython.com')\n    'https://inventwithpython.com'\n    >>> pysv.validateYesNo('y')\n    'yes'\n    >>> pysv.validateYesNo('NO')\n    'no'\n    >>> pysv.validateState('California')\n    'CA'\n    >>> pysv.validateState('TEXAS')\n    'TX'\n    >>> pysv.validateState('NY')\n    'NY'\n    >>> pysv.validateDayOfWeek('mon')\n    'Monday'\n    >>> pysv.validateDayOfWeek('FRIday')\n    'Friday'\n    >>> pysv.validateDayOfMonth(29, 2004, 2)\n    29\n    >>> pysv.validateDayOfMonth(31, 2019, 10)\n    31\n    >>> pysv.validateDayOfMonth(31, 2019, 9)\n    Traceback (most recent call last):\n        ...\n    pysimplevalidate.ValidationException: '31' is not a day in the month of September 2019",
    "bugtrack_url": null,
    "license": "BSD",
    "summary": "A collection of string-based validation functions, suitable for use in other Python 2 and 3 applications.",
    "version": "0.2.12",
    "project_urls": {
        "Homepage": "https://github.com/asweigart/pysimplevalidate"
    },
    "split_keywords": [
        "input",
        "validation",
        "text",
        "string"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "0b548fd99436073dbdbba719e08e1b7e5f3f36a8cc9e518612e1b97e4806d2ce",
                "md5": "4de8f6a289cf88cecf1a072dcd886d1f",
                "sha256": "645d24bdca17ad4c40658f3aa0bd5c1aa1688ba0c02ba75c5ed2cb3b8abaaa19"
            },
            "downloads": -1,
            "filename": "PySimpleValidate-0.2.12.tar.gz",
            "has_sig": false,
            "md5_digest": "4de8f6a289cf88cecf1a072dcd886d1f",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 22958,
            "upload_time": "2020-07-09T19:58:55",
            "upload_time_iso_8601": "2020-07-09T19:58:55.197811Z",
            "url": "https://files.pythonhosted.org/packages/0b/54/8fd99436073dbdbba719e08e1b7e5f3f36a8cc9e518612e1b97e4806d2ce/PySimpleValidate-0.2.12.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2020-07-09 19:58:55",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "asweigart",
    "github_project": "pysimplevalidate",
    "travis_ci": false,
    "coveralls": true,
    "github_actions": false,
    "tox": true,
    "lcname": "pysimplevalidate"
}
        
Elapsed time: 0.24414s