Files
docker-images/python-tools/app/inputs.py
T
ajp_anton c9826e9ccc
Build custom container images / build (map[base_image:php:8-fpm-alpine build_args:PHP_VERSION=8 context:php8-pgsql fingerprint_command:{ apk info -v | LC_ALL=C sort; find /usr/local/lib/php/extensions /usr/local/etc/php/conf.d -type f -exec sha256sum {} + | LC_ALL=C sort; } name:ph… (push) Successful in 49s
Build custom container images / build (map[base_image:postgres:18 build_args:PG_VERSION=18 POSTGIS_VERSION=3 VCHORD_VERSION=0.5.3 context:postgres fingerprint_command:{ dpkg-query -W -f='${binary:Package}=${Version}\n' | LC_ALL=C sort; find /usr/lib/postgresql -type f -exec sha256su… (push) Successful in 49s
Build custom container images / build (map[base_image:python:3 build_args:PYTHON_VERSION=3 context:python-tools fingerprint_command:{ dpkg-query -W -f='${binary:Package}=${Version}\n' | LC_ALL=C sort; pip freeze | LC_ALL=C sort; } name:python-tools oci_labels:org.opencontainers.ima… (push) Failing after 1m46s
Build custom container images / build (map[base_image:python:3.12-slim build_args:PYTHON_VERSION=3.12-slim context:linkki-tiedotus fingerprint_command:{ dpkg-query -W -f='${binary:Package}=${Version}\n' | LC_ALL=C sort; pip freeze | LC_ALL=C sort; } name:linkki-tiedotus oci_labels:… (push) Successful in 24s
Build custom container images / build (map[base_image:python:3.12-slim build_args:PYTHON_VERSION=3.12-slim context:timepoll fingerprint_command:{ dpkg-query -W -f='${binary:Package}=${Version}\n' | LC_ALL=C sort; pip freeze | LC_ALL=C sort; } name:timepoll oci_labels:org.opencontai… (push) Successful in 32s
Respect explicitly cleared checkbox selections
2026-10-26 22:13:56 +00:00

144 lines
5.0 KiB
Python

"""Validation for task-input manifest values."""
from __future__ import annotations
import re
from collections.abc import Mapping, Sequence
from dataclasses import dataclass
from datetime import date, datetime
from pathlib import Path
from typing import BinaryIO, Protocol
from .discovery import InputField
class Upload(Protocol):
filename: str | None
content_length: int | None
stream: BinaryIO
@dataclass(frozen=True)
class PendingUpload:
field_name: str
extension: str
maximum_bytes: int | None
stream: BinaryIO
def validate_inputs(
fields: Sequence[InputField],
values: Mapping[str, list[str]],
) -> tuple[dict[str, str | int | list[str]], dict[str, str]]:
result: dict[str, str | int | list[str]] = {}
errors: dict[str, str] = {}
for field in fields:
if field.type == "file":
continue
raw_values = values.get(field.name, [])
value, error = validate_field(field, raw_values)
if error:
errors[field.name] = error
elif value is not None:
result[field.name] = value
return result, errors
def validate_uploads(
fields: Sequence[InputField],
uploads: Mapping[str, Upload],
) -> tuple[dict[str, PendingUpload], dict[str, str]]:
result: dict[str, PendingUpload] = {}
errors: dict[str, str] = {}
for field in fields:
if field.type != "file":
continue
upload = uploads.get(field.name)
filename = upload.filename if upload is not None else None
if upload is None or not filename:
if field.required:
errors[field.name] = "Choose a file."
continue
extension = Path(filename).suffix.lower()
if field.accept and extension not in field.accept:
errors[field.name] = f"Choose a file with one of: {', '.join(field.accept)}."
continue
if upload.content_length is not None and field.maximum_bytes is not None and upload.content_length > field.maximum_bytes:
errors[field.name] = f"Choose a file no larger than {field.maximum_bytes} bytes."
continue
result[field.name] = PendingUpload(
field_name=field.name,
extension=extension,
maximum_bytes=field.maximum_bytes,
stream=upload.stream,
)
return result, errors
def validate_field(
field: InputField,
raw_values: list[str],
) -> tuple[str | int | list[str] | None, str | None]:
if field.type == "multi_choice":
return validate_multi_choice(field, raw_values)
if len(raw_values) > 1:
return None, "Only one value is allowed."
raw = raw_values[0] if raw_values else ""
if not raw:
if field.default is not None:
return field.default, None
if field.required:
return None, "This field is required."
return None, None
if field.type == "integer":
try:
value = int(raw)
except ValueError:
return None, "Enter a whole number."
if field.minimum is not None and value < field.minimum:
return None, f"Enter a value of at least {field.minimum}."
if field.maximum is not None and value > field.maximum:
return None, f"Enter a value no greater than {field.maximum}."
if field.step is not None and (value - (field.minimum or 0)) % field.step:
return None, f"Enter a value in increments of {field.step}."
return value, None
if field.type == "date":
try:
date.fromisoformat(raw)
except ValueError:
return None, "Enter a valid date."
elif field.type == "datetime":
try:
datetime.fromisoformat(raw)
except ValueError:
return None, "Enter a valid date and time."
elif field.type == "choice" and raw not in {option.value for option in field.options}:
return None, "Choose one of the available options."
elif field.type == "text" and field.pattern and not re.fullmatch(field.pattern, raw):
return None, "Enter a value in the required format."
return raw, None
def validate_multi_choice(
field: InputField,
raw_values: list[str],
) -> tuple[list[str] | None, str | None]:
if not raw_values:
if field.default is not None:
return list(field.default), None
if field.required:
return None, "Choose at least one option."
return None, None
# The form's hidden empty value distinguishes deselection from omission.
raw_values = [value for value in raw_values if value != ""]
if not raw_values:
if field.required:
return None, "Choose at least one option."
return [], None
allowed = {option.value for option in field.options}
if any(value not in allowed for value in raw_values):
return None, "Choose only from the available options."
if len(raw_values) != len(set(raw_values)):
return None, "Each option can be selected only once."
return raw_values, None