Skip to content

Commit

Permalink
fix: validate job timeout value in schema
Browse files Browse the repository at this point in the history
  • Loading branch information
keithmanville committed Jul 2, 2024
1 parent b8d013d commit e47ad45
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/dioptra/restapi/v1/jobs/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
# ACCESS THE FULL CC BY 4.0 LICENSE HERE:
# https://creativecommons.org/licenses/by/4.0/legalcode
"""The schemas for serializing/deserializing Job resources."""
import re

from marshmallow import Schema, fields, validate

from dioptra.restapi.v1.schemas import (
Expand All @@ -26,6 +28,8 @@
generate_base_resource_schema,
)

VALID_TIMEOUT_REGEX = re.compile(r"^[1-9][0-9]*[hms]?$") # noqa: B950; fmt: skip

JobRefSchema = generate_base_resource_ref_schema("Job")
JobSnapshotRefSchema = generate_base_resource_ref_schema("Job", keep_snapshot_id=True)

Expand Down Expand Up @@ -126,6 +130,15 @@ class JobSchema(JobBaseSchema): # type: ignore
description="The maximum alloted time for a job before it times out and "
"is stopped. If omitted, the job timeout will default to 24 hours.",
),
validate=validate.Regexp(
VALID_TIMEOUT_REGEX,
error=(
"'{input}' is not valid timeout value. The proper timeout is an "
"integer denoting the time in seconds. It may optionally be followed "
"'h', 'm', or 's' (hours, minutes, seconds to specify different time "
"units. Examples: '3600', '3600s', '60m', '1h'"
),
),
)
status = fields.String(
attribute="status",
Expand Down

0 comments on commit e47ad45

Please sign in to comment.