-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Validate by regexp/Validate with a custom error (#132)
* Validate by regexp * Add validate * Fix tests * More tests
- Loading branch information
Showing
6 changed files
with
64 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import collections.abc | ||
import re | ||
from typing import Any | ||
|
||
import marshmallow.validate | ||
|
||
ValidationFunc = collections.abc.Callable[[Any], Any] | ||
|
||
|
||
def regexp_validate(regexp: re.Pattern | str, *, error: str | None = None) -> ValidationFunc: | ||
return marshmallow.validate.Regexp(regexp, error=error) | ||
|
||
|
||
def validate(validator: ValidationFunc, *, error: str | None = None) -> ValidationFunc: | ||
if error is None: | ||
return validator | ||
|
||
def _validator_with_custom_error(value: Any) -> Any: | ||
result = validator(value) | ||
if result is False: | ||
raise marshmallow.ValidationError(error) | ||
return result | ||
|
||
return _validator_with_custom_error |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters