Skip to content

Commit

Permalink
Fix field name validation
Browse files Browse the repository at this point in the history
  • Loading branch information
lk-geimfari committed Aug 19, 2023
1 parent e30943b commit 2a4f838
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 119 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
Version 11.1.0
--------------

**Added**:

- Added custom field name validation.


Version 11.0.0
--------------

Expand Down
16 changes: 9 additions & 7 deletions docs/schema.rst
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,13 @@ create a field handler. Let's call it ``my_field``.
Afterwards, you need to register this field handler using a name you intend to use later. In this example,
we will name the field ``hohoho``.

.. note::

To avoid receiving a ValueError, the field name must be a string that conforms to a valid Python identifier.

Also, it's important to note that **every** field handler must be registered using a unique name,
otherwise, you will override an existing field handler. The filed names are case-sensitive.

.. code:: python
>>> from mimesis.schema import Field
Expand All @@ -313,7 +320,8 @@ we will name the field ``hohoho``.
>>> # Now you can use it:
>>> field("hohoho", a="a", b="b")
'a'
>>> # Note that you can still use the key function:
>>> # Note that you can still use the key function, but the order of arguments matters, so
>>> # key goes first, and then the rest of the arguments which are passed to the field handler.
>>> field("hohoho", key=str.upper, a="a", b="b")
'A'
Expand All @@ -331,12 +339,6 @@ You can register multiple fields at once:
>>> field("mf2", key=str.upper)
.. note::

It's important to note that **every** field handler must be registered using a unique name,
otherwise it will override the existing field handler with the same name.


Unregister Field Handler
~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down
2 changes: 1 addition & 1 deletion mimesis/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@
"__license__",
]

__version__ = "11.0.0"
__version__ = "11.1.0"
__title__ = "mimesis"
__description__ = "Mimesis: Fake Data Generator."
__url__ = "https://github.com/lk-geimfari/mimesis"
Expand Down
10 changes: 7 additions & 3 deletions mimesis/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,18 +186,19 @@ def perform(

return result

def __str__(self) -> str:
return f"{self.__class__.__name__} <{self._gen.locale}>"

def register_field(self, field_name: str, field_handler: FieldHandler) -> None:
"""Register a new field handler.
:param field_name: Name of the field.
:param field_handler: Callable object.
"""

if not isinstance(field_name, str):
raise TypeError("Field name must be a string.")

if not field_name.isidentifier():
raise ValueError("Field name must be a valid Python identifier.")

if not callable(field_handler):
raise TypeError("Handler must be a callable object.")

Expand Down Expand Up @@ -243,6 +244,9 @@ def unregister_all_fields(self) -> None:
"""
self._custom_fields.clear()

def __str__(self) -> str:
return f"{self.__class__.__name__} <{self._gen.locale}>"


class Field(BaseField):
"""Greedy field.
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "mimesis"
version = "11.0.0"
version = "11.1.0"
description = "Mimesis: Fake Data Generator."
authors = ["Isaak Uchakaev <[email protected]>"]
license = "MIT"
Expand Down
Loading

0 comments on commit 2a4f838

Please sign in to comment.