Skip to content

Commit

Permalink
feat(docworker): Add value question validations
Browse files Browse the repository at this point in the history
  • Loading branch information
MarekSuchanek committed Nov 26, 2024
1 parent 0b40851 commit 7ab1941
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 4 deletions.
2 changes: 1 addition & 1 deletion packages/dsw-document-worker/dsw/document_worker/consts.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
CMD_CHANNEL = 'doc_worker'
CMD_COMPONENT = 'doc_worker'
COMPONENT_NAME = 'Document Worker'
CURRENT_METAMODEL = 15
CURRENT_METAMODEL = 16
DEFAULT_ENCODING = 'utf-8'
EXIT_SUCCESS = 0
NULL_UUID = '00000000-0000-0000-0000-000000000000'
Expand Down
55 changes: 53 additions & 2 deletions packages/dsw-document-worker/dsw/document_worker/model/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -792,6 +792,53 @@ def cross_references(self) -> list[CrossReference]:
return [r for r in self.references if isinstance(r, CrossReference)]


class ValueQuestionValidation:
SHORT_TYPE: dict[str, str] = {
'MinLengthQuestionValidation': 'min-length',
'MaxLengthQuestionValidation': 'max-length',
'RegexQuestionValidation': 'regex',
'OrcidQuestionValidation': 'orcid',
'DoiQuestionValidation': 'doi',
'MinNumberQuestionValidation': 'min',
'MaxNumberQuestionValidation': 'max',
'FromDateQuestionValidation': 'from-date',
'ToDateQuestionValidation': 'to-date',
'FromDateTimeQuestionValidation': 'from-datetime',
'ToDateTimeQuestionValidation': 'to-datetime',
'FromTimeQuestionValidation': 'from-time',
'ToTimeQuestionValidation': 'to-time',
'DomainQuestionValidation': 'domain',
}
VALUE_TYPE: dict[str, type | None] = {
'MinLengthQuestionValidation': int,
'MaxLengthQuestionValidation': int,
'RegexQuestionValidation': str,
'OrcidQuestionValidation': None,
'DoiQuestionValidation': None,
'MinNumberQuestionValidation': float,
'MaxNumberQuestionValidation': float,
'FromDateQuestionValidation': str,
'ToDateQuestionValidation': str,
'FromDateTimeQuestionValidation': str,
'ToDateTimeQuestionValidation': str,
'FromTimeQuestionValidation': str,
'ToTimeQuestionValidation': str,
'DomainQuestionValidation': str,
}

def __init__(self, validation_type: str, value: str | int | float | None = None):
self.type = self.SHORT_TYPE.get(validation_type, 'unknown')
self.full_type = validation_type
self.value = value

@staticmethod
def load(data: dict, **options):
return ValueQuestionValidation(
validation_type=data['type'],
value=data.get('value', None),
)


class ValueQuestion(Question):

def __init__(self, uuid, title, text, tag_uuids, reference_uuids,
Expand All @@ -800,6 +847,7 @@ def __init__(self, uuid, title, text, tag_uuids, reference_uuids,
reference_uuids, expert_uuids, required_phase_uuid,
annotations)
self.value_type = value_type # type: str
self.validations = list() # type: list[ValueQuestionValidation]

@property
def a(self):
Expand Down Expand Up @@ -846,7 +894,7 @@ def _resolve_links(self, ctx):

@staticmethod
def load(data: dict, **options):
return ValueQuestion(
question = ValueQuestion(
uuid=data['uuid'],
title=data['title'],
text=data['text'],
Expand All @@ -857,6 +905,9 @@ def load(data: dict, **options):
value_type=data['valueType'],
annotations=_load_annotations(data['annotations']),
)
question.validations = [ValueQuestionValidation.load(d, **options)
for d in data['validations']]
return question


class OptionsQuestion(Question):
Expand Down Expand Up @@ -1765,7 +1816,7 @@ def load(data: dict, **options):

class DocumentContext:
"""Document Context smart representation"""
METAMODEL_VERSION = 15
METAMODEL_VERSION = 16

def __init__(self, ctx, **options):
self.metamodel_version = int(ctx.get('metamodelVersion', '0'))
Expand Down
2 changes: 1 addition & 1 deletion packages/dsw-tdk/dsw/tdk/consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

APP = 'dsw-tdk'
VERSION = '4.12.0'
METAMODEL_VERSION = 15
METAMODEL_VERSION = 16

REGEX_SEMVER = re.compile(r'^[0-9]+\.[0-9]+\.[0-9]+$')
REGEX_ORGANIZATION_ID = re.compile(r'^(?![.])(?!.*[.]$)[a-zA-Z0-9.]+$')
Expand Down
1 change: 1 addition & 0 deletions packages/dsw-tdk/dsw/tdk/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ def __init__(self, message: str, hint: str):
13: (4, 3, 0),
14: (4, 10, 0),
15: (4, 12, 0),
16: (4, 13, 0),
}


Expand Down

0 comments on commit 7ab1941

Please sign in to comment.