-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(validator): extract validator abstraction
- Loading branch information
Showing
5 changed files
with
58 additions
and
95 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
from .jsonschema import json_schema_validator # noqa | ||
from .marshmallow import Validator as marshmallow # noqa | ||
from .validator import Validator # noqa | ||
from .jsonschema_validator import JSONSchemaValidator as jsonschema # noqa | ||
from .marshmallow_validator import MarshmallowValidator as marshmallow # noqa |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from typing import Any, Dict, List, Tuple | ||
from collections import defaultdict | ||
|
||
from jsonschema import Draft7Validator | ||
from lambda_handlers.validators import Validator | ||
|
||
|
||
class JSONSchemaValidator(Validator): | ||
|
||
def validate(self, instance, schema) -> Tuple[Any, List[Any]]: | ||
validator = Draft7Validator(schema) | ||
errors = sorted(validator.iter_errors(instance), key=lambda error: error.path) | ||
return instance, errors | ||
|
||
def format_error(self, errors) -> List[Dict[str, Any]]: | ||
path_errors: Dict[str, List[str]] = defaultdict(list) | ||
for error in errors: | ||
path_errors[error.path.pop()].append(error.message) | ||
|
||
return [{path: messages} for path, messages in path_errors.items()] |
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,17 @@ | ||
from typing import Any, Dict, List, Tuple | ||
|
||
from marshmallow import ValidationError | ||
from lambda_handlers.validators import Validator | ||
|
||
NO_FIELD_NAME = 'errors' | ||
|
||
|
||
class MarshmallowValidator(Validator): | ||
|
||
def validate(self, instance, schema) -> Tuple[Any, List[Any]]: | ||
result = schema.load(instance) | ||
return result.data, result.errors | ||
|
||
def format_error(self, errors) -> List[Dict[str, Any]]: | ||
exception = ValidationError(errors) | ||
return exception.normalized_messages(no_field_name=NO_FIELD_NAME).get(NO_FIELD_NAME, None) |
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