-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #73 from shanejbrown/full-validation
Expand config validation to most of the config
- Loading branch information
Showing
13 changed files
with
1,465 additions
and
440 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,48 @@ | ||
""" | ||
Copyright 2023 Adobe | ||
All Rights Reserved. | ||
NOTICE: Adobe permits you to use, modify, and distribute this file in accordance | ||
with the terms of the Adobe license agreement accompanying it. | ||
""" | ||
from typing import Union | ||
|
||
from pydantic import ValidationError | ||
|
||
|
||
class Errors: | ||
""" Error class for storing validation errors """ | ||
class Error: | ||
""" Error class for storing validation error """ | ||
def __init__(self, field: str, message: str): | ||
self.field: str = field | ||
self.message: Union[str, None] = message | ||
|
||
def __init__(self): | ||
self.errors = [] | ||
|
||
def add(self, field: str, message: str): | ||
""" Add an error """ | ||
self.errors.append(self.Error(field, message)) | ||
|
||
def count(self): | ||
""" Return the number of errors """ | ||
return len(self.errors) | ||
|
||
def __str__(self): | ||
return '\n'.join([f' {error.field}: {error.message}' for error in self.errors]) | ||
|
||
def __repr__(self): | ||
return self.__str__() | ||
|
||
|
||
def get_validation_errors(exc: ValidationError) -> Errors: | ||
""" Get validation errors to an Errors object """ | ||
errors = Errors() | ||
for error in exc.errors(): | ||
loc = [str(item) for item in error["loc"]] | ||
if error["type"] == "value_error.extra": | ||
errors.add(field='.'.join(loc), message='not a valid field, please check the spelling and documentation') | ||
else: | ||
errors.add(field='.'.join(loc), message=f'{error["msg"]} ({error["type"]})') | ||
return errors |
Oops, something went wrong.