-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue 62 - implement regex pattern validation (#65)
* Implement regex checking for UserProperty * Implement regex checking for SchemaDict keys * Document regex functionality * Fix error message testing * Test for TypeError if pattern is used with a property_type that is not str
- Loading branch information
Showing
12 changed files
with
212 additions
and
32 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
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,6 @@ | ||
from wysdom import UserObject, UserProperty | ||
|
||
|
||
class Address(UserObject): | ||
zip_code: int = UserProperty(int, pattern=r"^[0-9]{5}$") | ||
|
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,33 @@ | ||
from typing import Any, Dict, Tuple | ||
|
||
import re | ||
|
||
from .SchemaPrimitive import SchemaPrimitive | ||
|
||
|
||
class SchemaPattern(SchemaPrimitive): | ||
""" | ||
A schema requiring a match for a regex pattern. | ||
""" | ||
|
||
pattern: str = None | ||
|
||
def __init__(self, pattern: str) -> None: | ||
super().__init__(python_type=str) | ||
self.pattern = pattern | ||
|
||
def __call__( | ||
self, | ||
value: str, | ||
dom_info: Tuple = None | ||
) -> Any: | ||
if not re.match(self.pattern, value): | ||
raise ValueError(f"Parameter value {value} does not match regex pattern {self.pattern}.") | ||
return super().__call__(value) | ||
|
||
@property | ||
def jsonschema_definition(self) -> Dict[str, Any]: | ||
return { | ||
"type": self.type_name, | ||
"pattern": self.pattern | ||
} |
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
Oops, something went wrong.