Skip to main content

User input validation with defaults for the Google App Engine datastore.

Project description

Copyright © 2014-2019, Brendan Doms
Licensed under the MIT license

GAE Validators

GAE Validators provides user input validation methods with smart defaults for the Google App Engine datastore.

Build Status

Python 2 Tests Python 3 Tests Flake8 Linter Build Package

How It Works

Each validator is simply a method that receives string input and returns a tuple of (valid, value) back. valid is simply a boolean of whether the input passed validation or not. value is a coerced, potentially optimized version of the input. For example, strings have outer whitespace stripped, while integers, booleans, and dates are all returned as their respective type.

Some validators have additional parameters to help configure how validation should be done. By default, these are all defined to match the restrictions of the GAE properties. In theory you could support any datastore backend simply by changing the configuration parameters.

Example Use

The normal flow of a program would be to get some user input from a form, pull it out of the request, and validate it. If validation passes for all the fields, then update the datastore, if not, then return helpful errors to the user. For example:

from gae_validators import validateEmail

class ExampleHandler(webapp2.RequestHandler):

    def post(self):

        form_email = self.request.get("email")

        is_valid, validated_email = validateEmail(form_email)

        if is_valid:
            user.email = validated_email
            user.put()
        else:
            self.redirect("/user/update?errors=email")

Available Validators

Here are all the function signatures with their default configuration values:

validateString(source, max_length=500, newlines=False, encoding='utf-8')

validateRequiredString(source, min_length=1, max_length=500, newlines=False, encoding='utf-8')
# same as above execpt that a string below the min_length will fail

validateText(source, max_length=ONE_MB, newlines=True, encoding='utf-8')
# ONE_MB is defined as 2 ** 20

validateRequiredText(source, min_length=1, max_length=ONE_MB, newlines=True, encoding='utf-8')

validateEmail(source)

validateRequiredEmail(source)

validatePhone(source)
# returns the number in a good approximation of E.164 format
# this should work exactly for numbers with country code 1 (US and Canada)
# however it will not be correct in all cases for all countries
# you'll need a different solution if you want full international support

validateRequiredPhone(source)

validateUrl(source)
# only http and https schemes are supported

validateRequiredUrl(source)

validateChoices(source, choices)
# choices should be an iterable

validateRequiredChoices(source, choices)

validateBool(source)
# any value can be truthy or falsy
# so there is no required version of validateBool, and it will never return an invalid result

validateInt(source, min_amount=-INT_SIZE, max_amount=INT_SIZE - 1)
# INT_SIZE is defined as a 64 bit signed integer, which means 2 ** 63

validateRequiredInt(source, min_amount=-INT_SIZE, max_amount=INT_SIZE - 1)

validateFloat(source, min_amount=-INT_SIZE, max_amount=INT_SIZE - 1)

validateRequiredFloat(source, min_amount=-INT_SIZE, max_amount=INT_SIZE - 1)

validateDateTime(source, date_format="%Y-%m-%dT%H:%M", future_only=False, past_only=False)

validateRequiredDateTime(source, date_format="%Y-%m-%dT%H:%M", future_only=False, past_only=False)

validateDate(source, date_format="%Y-%m-%d", future_only=False, past_only=False)

validateRequiredDate(source, date_format="%Y-%m-%d", future_only=False, past_only=False)

validateTime(source, time_format="%H:%M")

validateRequiredTime(source, time_format="%H:%M")

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for gae-validators, version 1.0.2
Filename, size File type Python version Upload date Hashes
Filename, size gae_validators-1.0.2-py2.py3-none-any.whl (5.4 kB) File type Wheel Python version py2.py3 Upload date Hashes View hashes
Filename, size gae_validators-1.0.2.tar.gz (5.2 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page