Minos is a library to do flexible validation of Python objects
Project description
[‘Minos: A Lightweight Validation Frameworkn’, ‘=========================================n’, ‘Minos is a library that you can use in your code to create a simple validationn’, ‘framework. Its aim is to provide a lightweight interface of functions that make itn’, ‘simple to quickly build and manipulate models.n’, ‘n’, ‘.. toctree::n’, ‘ :maxdepth: 2n’, ‘n’, ‘Installationn’, ‘============n’, ‘n’, ‘To install Minos locally:n’, ‘n’, ‘::n’, ‘n’, ‘ $ python setup.py developn’, ‘n’, ‘n’, ‘Minos will soon be on PyPi.n’, ‘n’, ‘.. _Quickstart:n’, ‘n’, ‘Quickstartn’, ‘==========n’, ‘n’, ‘There are a few patterns Minos was designed to support; class validation with validators declared asn’, ‘part of the class definition, class validation where the validators are declared seperately, and asn’, ‘totally independant object validators. See below for a few examples of each pattern.n’, ‘n’, ‘Class Validators in Class Definitionn’, ‘————————————n’, ‘n’, ‘To declare validators inside of a class, the class needs to inherit from the Minos Mixin, and then’, ‘validation arguments are passed to each validator on instantiation.n’, ‘n’, ‘For example, given a class definition like so:n’, ‘n’, ‘.. sourcecode:: pythonn’, ‘n’, ‘ import minosn’, ‘ import pytzn’, ‘n’, ‘ from datetime import datetimen’, ‘ from minos.validators import DatetimeValidator, NumericalityValidator, PresenceValidatorn’, ‘n’, ‘n’, ‘ class Toaster(minos.Mixin):n’, ‘n’, ‘ #Attributesn’, ‘n’, ‘ created_at = Nonen’, ‘ num_slots = Nonen’, ‘n’, ‘ #Validatorsn’, ‘n’, ‘ validators = [n’, “ DatetimeValidator(‘created_at’),n”, “ PresenceValidator(‘num_slots’), # every toaster needs to have its number of slots specifiedn”, ‘ #Toasters can only have an integer number of slots between 2-8n’, ‘ NumericalityValidator(n’, “ ‘num_slots’,n”, ‘ integer_only=True,n’, ‘ greater_than_or_equal_to=2,n’, ‘ less_than_or_equal_to=8n’, ‘ )n’, ‘ ]n’, ‘n’, ‘n’, ‘ def __init__(self, bread):n’, ‘ self.bread = breadn’, ‘ self.created_at = datetime.now(pytz.utc)n’, ‘n’, ‘ #…class definition continues…n’, ‘n’, ‘n’, ‘When using the ToasterClass in code, you can do stuff like this:n’, ‘n’, ‘.. sourcecode:: pythonn’, ‘n’, ‘ def toast_bread(bread):n’, ‘ toaster = Toaster(num_slots=4)n’, ‘ try:n’, “ toaster.validate() # Validates that toaster’s attributes are validn”, ‘ except minos.errors.FormValidationError:n’, ‘ raise ToastError(“can't toast bread, toasters busted.”)n’, ‘n’, ‘ toasted_bread = toaster.toast(bread)n’, ‘n’, ‘ return toasted_breadn’, ‘n’, ‘Class Validators Outside Class Definitionn’, ‘—————————————–n’, ‘n’, ‘Similarly, You can also create a validator that validates a single attribute of a python object,n’, “and pass objects to be validated to it using the Validator’s .validate_wrapper() call.n”, ‘n’, ‘n’, ‘.. sourcecode:: pythonn’, ‘n’, ‘ import minosn’, ‘ from minos.validators import NumericalityValidatorn’, ‘n’, “ #Create a validator that for a toaster’s slots ahead of timen”, ‘ slot_validator = NumericalityValidator(n’, “ ‘num_slots’,n”, ‘ integer_only=True,n’, ‘ greater_than_or_equal_to=2,n’, ‘ less_than_or_equal_to=8n’, ‘ )n’, ‘n’, ‘ #Given a list of toasters, validate them alln’, ‘ for toaster in [dorm_toaster, kitchen_toaster, fancy_toaster]:n’, ‘ try:n’, ‘ #Check this toaster using the parameters specified earliern’, ‘ slot_validator.validate_wrapper(toaster)n’, ‘ except minos.errors.FormValidationError:n’, ‘ print “{} doesn't have the right number of slots.”.format(toaster.name)n’, ‘n’, ‘Single-Use Validationn’, ‘———————-n’, ‘n’, ‘You can also create validators without configuration, and specify the configuration inline using the .validate() call:n’, ‘n’, ‘n’, ‘.. sourcecode:: pythonn’, ‘n’, ‘ from minos.validators import NumericalityValidatorn’, ‘ from minos.errors import ValidationErrorn’, ‘n’, ‘ #Make a validator that checks numeric qualities of objectsn’, ‘ accountant = NumericalityValidator()n’, ‘n’, ‘n’, ‘ #Check a bunch of stuffn’, ‘n’, ‘ try:n’, ‘ accountant.validate(bank_account, greater_than=0)n’, ‘ except ValidationError:n’, ‘ print “You're broke!”n’, ‘n’, ‘ try:n’, ‘ accountant.validate(tax_payment, greater_than=0, less_than=5000.00)n’, ‘ except ValidationError:n’, ‘ print “You're tax payment doesn't seem right…”n’, ‘n’, ‘ try:n’, ‘ accountant.validate(num_deductions, integer_only=True)n’, ‘ except ValidationError:n’, ‘ print “Your number of deductions doesn't make sense.”n’, ‘n’, ‘n’]
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.