Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: form generation/validation from json schema #63

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 27 additions & 3 deletions src/aind_data_transfer_models/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class ModalityConfigs(BaseSettings):
),
title="Extra Configs",
)
job_settings: Optional[dict] = Field(
job_settings: Optional[Union[dict, str]] = Field(
default=None,
description=(
"Configs to pass into modality compression job. Must be json "
Expand Down Expand Up @@ -132,6 +132,21 @@ def parse_modality_string(
else:
return input_modality

@field_validator("job_settings", mode="before")
def parse_job_settings_str(
cls, job_settings: Optional[Union[dict, str]]
) -> Optional[dict]:
"""Attempt to convert job_settings to a dict if it is a string"""
if isinstance(job_settings, str):
try:
job_settings_dict = json.loads(job_settings)
return job_settings_dict
except Exception as e:
raise ValueError(
f"job_settings must be json serializable! {e}"
)
return job_settings

@field_validator("compress_raw_data", mode="after")
def get_compress_source_default(
cls, compress_source: Optional[bool], info: ValidationInfo
Expand Down Expand Up @@ -361,8 +376,8 @@ class BasicUploadJobConfigs(BaseSettings):
title="Trigger Capsule Configs (deprecated. Use codeocean_configs)",
validate_default=True,
)
codeocean_configs: CodeOceanPipelineMonitorConfigs = Field(
default=CodeOceanPipelineMonitorConfigs(),
codeocean_configs: Optional[CodeOceanPipelineMonitorConfigs] = Field(
default=None,
description=(
"User can pass custom fields. Otherwise, transfer service will "
"handle setting default values at runtime."
Expand Down Expand Up @@ -656,6 +671,15 @@ def fill_in_metadata_configs(self, handler):
)
return validated_self

@field_validator("codeocean_configs", mode="before")
def set_default_codeocean_configs(
cls, codeocean_configs: Optional[CodeOceanPipelineMonitorConfigs]
) -> CodeOceanPipelineMonitorConfigs:
"""Sets default values for codeocean_configs"""
if codeocean_configs is None:
codeocean_configs = CodeOceanPipelineMonitorConfigs()
return codeocean_configs

@model_validator(mode="after")
def set_codeocean_configs(self):
"""Merge user defined fields with some defaults."""
Expand Down
22 changes: 22 additions & 0 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,28 @@ def test_extra_configs_json_error(self):
self.assertEqual(1, len(errors))
self.assertTrue(expected_error_message_snippet in errors[0]["msg"])

def test_job_settings_string(self):
"""Tests that job_settings can be passed in as a string."""

configs = ModalityConfigs(
modality=Modality.ECEPHYS,
source="some_dir",
job_settings='{"param1": 3, "param2": "abc"}',
)
self.assertEqual({"param1": 3, "param2": "abc"}, configs.job_settings)
with self.assertRaises(ValidationError) as e:
ModalityConfigs(
modality=Modality.ECEPHYS,
source="some_dir",
job_settings="some string",
)
expected_error_message_snippet = (
"Value error, job_settings must be json serializable!"
)
errors = e.exception.errors()
self.assertEqual(1, len(errors))
self.assertTrue(expected_error_message_snippet in errors[0]["msg"])


class TestCodeOceanPipelineMonitorConfigs(unittest.TestCase):
"""Tests CodeOceanPipelineMonitorConfigs class"""
Expand Down