forked from adobe/buildrunner
-
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.
Expand config validation to most of the config
- Loading branch information
1 parent
c9b2fca
commit 2a413e9
Showing
12 changed files
with
1,377 additions
and
435 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 add_validation_errors(exc: ValidationError) -> Errors: | ||
""" Add 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.