-
Notifications
You must be signed in to change notification settings - Fork 495
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add validation util for custom compose app
- Loading branch information
Showing
2 changed files
with
25 additions
and
15 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
23 changes: 23 additions & 0 deletions
23
src/middlewared/middlewared/plugins/apps/custom_app_utils.py
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,23 @@ | ||
import yaml | ||
|
||
from middlewared.service import ValidationErrors | ||
|
||
|
||
def validate_payload(data: dict, schema: str) -> dict: | ||
verrors = ValidationErrors() | ||
compose_keys = ('custom_compose_config', 'custom_compose_config_string') | ||
if all(not data.get(k) for k in compose_keys): | ||
verrors.add(f'{schema}.custom_compose_config', 'This field is required') | ||
elif all(data.get(k) for k in compose_keys): | ||
verrors.add(f'{schema}.custom_compose_config_string', 'Only one of these fields should be provided') | ||
|
||
compose_config = data.get('custom_compose_config') | ||
if data.get('custom_compose_config_string'): | ||
try: | ||
compose_config = yaml.YAMLError(data['custom_compose_config_string']) | ||
except yaml.YAMLError: | ||
verrors.add('app_create.custom_compose_config_string', 'Invalid YAML provided') | ||
|
||
verrors.check() | ||
|
||
return compose_config |