Skip to content

Commit

Permalink
add datapoint text to taskdata
Browse files Browse the repository at this point in the history
  • Loading branch information
DicksonChi committed Oct 17, 2023
1 parent 916c73e commit 8e8e026
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 6 deletions.
21 changes: 18 additions & 3 deletions basemodels/manifest/data/taskdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from uuid import UUID

import requests
from pydantic import BaseModel, HttpUrl, validate_model, ValidationError, validator
from pydantic import BaseModel, HttpUrl, validate_model, ValidationError, validator, root_validator
from requests import RequestException

from basemodels.constants import SUPPORTED_CONTENT_TYPES
Expand All @@ -21,23 +21,27 @@ class TaskDataEntry(BaseModel):
{
"task_key": "407fdd93-687a-46bb-b578-89eb96b4109d",
"datapoint_uri": "https://domain.com/file1.jpg",
"datapoint_text": {},
"datapoint_hash": "f4acbe8562907183a484498ba901bfe5c5503aaa"
},
{
"task_key": "20bd4f3e-4518-4602-b67a-1d8dfabcce0c",
"datapoint_uri": "https://domain.com/file2.jpg",
"datapoint_text": {},
"datapoint_hash": "f4acbe8562907183a484498ba901bfe5c5503aaa"
}
]
"""

task_key: Optional[UUID]
datapoint_uri: HttpUrl
datapoint_uri: Optional[HttpUrl]
datapoint_text: Optional[Dict[str, str]]

@validator("datapoint_uri", always=True)
def validate_datapoint_uri(cls, value):
if len(value) < 10:
if value and len(value) < 10:
raise ValidationError("datapoint_uri need to be at least 10 char length.")
return value

@validator("metadata")
def validate_metadata(cls, value):
Expand All @@ -55,6 +59,17 @@ def validate_metadata(cls, value):
datapoint_hash: Optional[str]
metadata: Optional[Dict[str, Optional[Union[str, int, float, Dict[str, Any]]]]]

@root_validator
def validate_datapoint_text(cls, values):
"""
Validate datapoint_uri.
Raise error if no datapoint_text and no value for URI.
"""
if not values.get("datapoint_uri") and not values.get("datapoint_text"):
raise ValueError("datapoint_uri is missing.")
return values


def validate_content_type(uri: str) -> None:
"""Validate uri content type"""
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "hmt-basemodels"
version = "0.2.1"
version = "0.2.2"
description = ""
authors = ["Intuition Machines, Inc <[email protected]>"]
packages = [
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setuptools.setup(
name="hmt-basemodels",
version="0.2.1",
version="0.2.2",
author="HUMAN Protocol",
description="Common data models shared by various components of the Human Protocol stack",
url="https://github.com/hCaptcha/hmt-basemodels",
Expand Down
4 changes: 3 additions & 1 deletion tests/test_manifest_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -912,7 +912,9 @@ def test_valid_entry_is_true(self):
TaskDataEntry(**taskdata)

taskdata["datapoint_text"] = {"en": "Question to test with"}
TaskDataEntry(**taskdata)
taskdata.pop("datapoint_uri")
taskdata_cons = TaskDataEntry(**taskdata)
self.assertEqual(taskdata_cons.datapoint_text, {"en": "Question to test with"})

with self.assertRaises(ValidationError):
taskdata["datapoint_text"] = {}
Expand Down

0 comments on commit 8e8e026

Please sign in to comment.