Spaces:
Sleeping
Sleeping
Create validators.py
Browse files- utils/validators.py +47 -0
utils/validators.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Validation utilities for model card inputs
|
| 3 |
+
"""
|
| 4 |
+
|
| 5 |
+
import re
|
| 6 |
+
|
| 7 |
+
def validate_mmcid(mmcid):
|
| 8 |
+
"""
|
| 9 |
+
Validate MMCID format: DF-MC-YYYY-NNN
|
| 10 |
+
|
| 11 |
+
Args:
|
| 12 |
+
mmcid: String to validate
|
| 13 |
+
|
| 14 |
+
Returns:
|
| 15 |
+
Boolean indicating if format is valid
|
| 16 |
+
|
| 17 |
+
Examples:
|
| 18 |
+
DF-MC-2025-001 ✓
|
| 19 |
+
DF-MC-2024-123 ✓
|
| 20 |
+
df-mc-2025-001 ✗ (wrong case)
|
| 21 |
+
DF-MC-25-001 ✗ (year must be 4 digits)
|
| 22 |
+
"""
|
| 23 |
+
if not mmcid:
|
| 24 |
+
return True # Empty is valid (optional field)
|
| 25 |
+
|
| 26 |
+
pattern = r'^DF-MC-\d{4}-\d{3}$'
|
| 27 |
+
return bool(re.match(pattern, mmcid))
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
def validate_controlled_vocab_selection(selections, max_items=3):
|
| 31 |
+
"""
|
| 32 |
+
Validate that multi-select doesn't exceed max items
|
| 33 |
+
|
| 34 |
+
Args:
|
| 35 |
+
selections: List of selected items
|
| 36 |
+
max_items: Maximum allowed selections (default 3)
|
| 37 |
+
|
| 38 |
+
Returns:
|
| 39 |
+
Tuple of (is_valid, error_message)
|
| 40 |
+
"""
|
| 41 |
+
if not selections:
|
| 42 |
+
return True, None
|
| 43 |
+
|
| 44 |
+
if len(selections) > max_items:
|
| 45 |
+
return False, f"Please select no more than {max_items} items"
|
| 46 |
+
|
| 47 |
+
return True, None
|