
Security News
CISA Kills Off RSS Feeds for KEVs and Cyber Alerts
CISA is discontinuing official RSS support for KEV and cybersecurity alerts, shifting updates to email and social media, disrupting automation workflows.
validation-decorators
Advanced tools
a simple, zero-dependency, easy-to-use and customizable validation and cleansing decorators for function arguments.
validation_decorators is a simple package that provides validation and cleansing for function arguments. It is very customizable and easy to set up.
start by downloading the package:
pip install validation_decorators
import your desired validation method:
from validation_decorators import ValidateArgType, ValidateArgAttribute, CastArg
create or import your error handler. This package contains a few simple error handlers, but you can easily add your own.
from validation_decorators.errors import log_error, raise_error, ignore_error
type_validator = ValidateArgType(raise_error, logger=None)
use your decorator
@type_validator(id=str, value=(int, float))
def output_item(id, title, value):
"""prints item"""
print(f'{title}({id}): {value}')
from validation_decorators import ValidateArgType,
from validation_decorators.errors import raise_error
type_validator = ValidateArgType(raise_error, logger=None)
@type_validator(id=str, value=int)
def output_item(id, value):
"""prints item"""
print(f'{id}: {value}')
why was this package created? Even with dynamic languages, there are times when one needs to validate function arguments, whether it be ensuring that everything is in order during development, or that you want a little extra peace of mind when collecting data from users, files, or old database tables.
Different environments and industries require different approaches to validation, and the Python community is also split on how to validate; Do you assume everything is correct, check by type, or is duck-typing your preferred method?
Even once validation preferences have been dealt with, there is another issue to tackle; If you find a validation error, how do you want to handle the error? Do you log it, throw an exception, ignore it?
Due to the large amount of variables in programming, I decided to make this package.
decorators are defined by creating an object from one of the decorator classes. You may also make your own class in order to help suit your needs. constructors are as follows:
decorator = ValidatorClass(error_function, logger=None)
error class: a function to run in the event that an argument does not match its validator. error function specifications will be described later.
logger: This is a logger object that will be used in the event that you choose to log your errors. It defaults to None, and will not be referenced unless it is called in the error function.
it should be noted that not all arguments need to be validated. only argument referenced in the decorator will be validated. all others will be passed normally.
There are two main types of decorator classes:
Validation
validate_args = ValidateArgType(raise_error)
@validate_args(dt=(date, datetime))
def date_to_yyyymmdd(dt)
...
in the above example, an argument that is of the datetime or date class(as well as subclasses) will considered valid. any other type will trigger the error function.
validate_args = ValidateArgType(log_error, logger=current_app.logger)
@validate_args(dt='month')
def date_to_yyyymmdd(dt)
...
in the above example, the dt argument will be considered valid if it has the attribute month
Replacement
note: this class has a special argument:
cast_args = CastArg(ignore_error)
class StockData():
__slots__ = ['stock_name', 'prev_close', 'close']
@cast_args(value_one_error='-', stock_name=str, prev_close=float, close=float)
def __init(self, stock_name, prev_close, close):
self.prev_close = prev_close
if self.prev_close != '-':
...
Any uncastable data will be set to value_on_error. value_on_error defaults to None. If the argument is the same type as specified, it will simply be passed without any unnecessary casting. It should be noted that some types, especially dicts, datetimes, etc. cannot be cast with this decorator due to their nature.
this decorator is good for controlling any unexpected values. In the above example, if prev_close was None or an empty string, it will be turned into '-'. This is helpful because all values can be tested against value_on_error. If you are displaying a the data on a website, the prev_close will be listed as a value or - without any further validation.
the error functions in this package are very simple in order for developers to easily change the logic to suite their use case. For example, it may be sufficient for websites to throw an exception due to global error handling, while programs may benefit from using logging.
It should be noted that CastArg was created to attempt to cast values without causing errors. While you can have the decorator trigger an error, using an error function like ignore_errors to suppress errors is recommended.
the error function should have the following specifications:
def error_func(msg, func_name, logger):
...
FAQs
a simple, zero-dependency, easy-to-use and customizable validation and cleansing decorators for function arguments.
We found that validation-decorators demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
CISA is discontinuing official RSS support for KEV and cybersecurity alerts, shifting updates to email and social media, disrupting automation workflows.
Security News
The MCP community is launching an official registry to standardize AI tool discovery and let agents dynamically find and install MCP servers.
Research
Security News
Socket uncovers an npm Trojan stealing crypto wallets and BullX credentials via obfuscated code and Telegram exfiltration.