Enhance shift constraints validation and error handling in forms and templates
This commit is contained in:
@@ -663,6 +663,26 @@ class ShiftForm(forms.Form):
|
||||
parsed = json.loads(val)
|
||||
if not isinstance(parsed, list):
|
||||
raise forms.ValidationError("Constraints must be a JSON list of constraint objects")
|
||||
# Validate each constraint object for basic correctness
|
||||
errors = []
|
||||
for i, c in enumerate(parsed):
|
||||
if not isinstance(c, dict):
|
||||
errors.append(f"Constraint #{i+1} must be an object")
|
||||
continue
|
||||
name = c.get('name') or c.get('type')
|
||||
if not name:
|
||||
errors.append(f"Constraint #{i+1} missing 'name'")
|
||||
continue
|
||||
if name in ('pre', 'post'):
|
||||
opts = c.get('options', {}) or {}
|
||||
# days is now required: check top-level or under options
|
||||
days_val = c.get('days') if c.get('days') is not None else opts.get('days')
|
||||
if days_val is None:
|
||||
errors.append(
|
||||
f"Constraint #{i+1} ('{name}') requires a 'days' duration (number of days the constraint applies for)."
|
||||
)
|
||||
if errors:
|
||||
raise forms.ValidationError(errors)
|
||||
return parsed
|
||||
except forms.ValidationError:
|
||||
raise
|
||||
|
||||
Reference in New Issue
Block a user