Skip to content

Commit

Permalink
Merge pull request #18 from xeBuz/jesus_custom_messages
Browse files Browse the repository at this point in the history
Variables on message
  • Loading branch information
xeBuz committed Jan 25, 2016
2 parents e4ae4ed + 438c45d commit 0a91d90
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 8 deletions.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,18 @@ Every Constraint has a parameter to throw an exception everytime the validation
ValidateNumeric(Table.field, False, True, "Message")
```

The last parameter enables this featur and throw a `ValidateError` exception, otherwise it will fails silently.
The third parameter enables this feature and throw a `ValidateError` exception, otherwise it will fails silently.


## Message Exception

The fourth parameter allow a custom message exception, with a few variables available

- `old_value`: value previous to the modification
- `new_value`: value provided (with the error)
- `key`: the column name
- `field`: object.column


## Available Constraints

Expand Down
4 changes: 2 additions & 2 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@
# built documents.
#
# The short X.Y version.
version = '1.1'
version = '1.1.1'
# The full version, including alpha/beta/rc tags.
release = '1.1'
release = '1.1.1'

# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
Expand Down
2 changes: 1 addition & 1 deletion docs/custom.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ You must define your own method ``check_value()`` and if you are receiving any a
retunr if value in ['A', 'B']
validator = ValidateAorB('yadayada')
validator = ValidateAorB(field, True, True, 'yadayada')
1 change: 1 addition & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Contents:
usage
validators
custom
messages



Expand Down
19 changes: 19 additions & 0 deletions docs/messages.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Exception Message
=================


You will be able to create customs exception messages, with a few variables availables:

* ``field``: Object and property
* ``key``: property
* ``new_value``: New value changed
* ``old_value``: Previous value



.. code-block:: python
from flask_validator import ValidateEmail
validator = ValidateEmail(field, False, True, 'Message: Field {field}, Key {key}, New value {new_value}, Old value {old_value}')
2 changes: 1 addition & 1 deletion docs/usage.rst
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Basic Usage
====================
===========


The most performant way to set up your validations is uring the SQLAlchemy special directive_ ``__declare_last__``, it occurs after mappings are assumed to be completed and the ‘configure’ step has finished.
Expand Down
3 changes: 2 additions & 1 deletion examples/example.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ def __init__(self, string, integer):
@classmethod
def __declare_last__(cls):
ValidateInteger(User.integer)
ValidateString(User.string, False, True, "Custom Message")
ValidateString(User.string, False, True, "Custom Message, "
"Field {field} - Value {value} - Oldvalue {oldvalue} - Key {key}")

db.create_all()
u = User("user", 1)
Expand Down
4 changes: 3 additions & 1 deletion flask_validator/validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from sqlalchemy import event
from .exceptions import ValidateError

__version__ = '1.1'
__version__ = '1.1.1'


class FlaskValidator:
Expand Down Expand Up @@ -45,6 +45,8 @@ def __validate(self, target, value, oldvalue, initiator):

if self.throw_exception:
if self.message:
self.message = self.message.format(
field=self.field, new_value=value, old_value=oldvalue, key=initiator.key)
raise ValidateError(self.message)
else:
raise ValidateError('Value %s from column %s is not valid' % (value, initiator.key))
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

setup(
name='Flask-Validator',
version='1.1',
version='1.1.1',
license='Mozilla Public License',
author='Jesus Roldan',
author_email='jesus.roldan@gmail.com',
Expand Down

0 comments on commit 0a91d90

Please sign in to comment.