File size: 53,851 Bytes
bf06a0d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 | """
Validation Manager
This module provides centralized validation logic for Google Docs operations,
extracting validation patterns from individual tool functions.
"""
import logging
from typing import Dict, Any, List, Tuple, Optional
from urllib.parse import urlparse
from gdocs.docs_helpers import (
validate_operation,
VALID_NAMED_STYLE_TYPES,
VALID_TEXT_BASELINE_OFFSETS,
VALID_PARAGRAPH_DIRECTIONS,
VALID_PARAGRAPH_SPACING_MODES,
VALID_SECTION_TYPES,
VALID_CONTENT_DIRECTIONS,
VALID_COLUMN_SEPARATOR_STYLES,
VALID_DOCUMENT_MODES,
VALID_BULLET_PRESETS,
)
logger = logging.getLogger(__name__)
class ValidationManager:
"""
Centralized validation manager for Google Docs operations.
Provides consistent validation patterns and error messages across
all document operations, reducing code duplication and improving
error message quality.
"""
def __init__(self):
"""Initialize the validation manager."""
self.validation_rules = self._setup_validation_rules()
def _setup_validation_rules(self) -> Dict[str, Any]:
"""Setup validation rules and constraints."""
return {
"table_max_rows": 1000,
"table_max_columns": 20,
"document_id_pattern": r"^[a-zA-Z0-9-_]+$",
"max_text_length": 1000000, # 1MB text limit
"font_size_range": (1, 400), # Google Docs font size limits
"valid_header_footer_types": ["DEFAULT", "FIRST_PAGE_ONLY", "EVEN_PAGE"],
"valid_section_types": ["header", "footer"],
"valid_list_types": ["UNORDERED", "ORDERED", "CHECKBOX"],
"valid_element_types": ["table", "list", "page_break"],
"valid_alignments": ["START", "CENTER", "END", "JUSTIFIED"],
"heading_level_range": (0, 6),
"font_weight_range": (100, 900),
}
def validate_document_id(self, document_id: str) -> Tuple[bool, str]:
"""
Validate Google Docs document ID format.
Args:
document_id: Document ID to validate
Returns:
Tuple of (is_valid, error_message)
"""
if not document_id:
return False, "Document ID cannot be empty"
if not isinstance(document_id, str):
return (
False,
f"Document ID must be a string, got {type(document_id).__name__}",
)
# Basic length check (Google Docs IDs are typically 40+ characters)
if len(document_id) < 20:
return False, "Document ID appears too short to be valid"
return True, ""
def validate_table_data(self, table_data: List[List[str]]) -> Tuple[bool, str]:
"""
Comprehensive validation for table data format.
This extracts and centralizes table validation logic from multiple functions.
Args:
table_data: 2D array of data to validate
Returns:
Tuple of (is_valid, detailed_error_message)
"""
if not table_data:
return (
False,
"Table data cannot be empty. Required format: [['col1', 'col2'], ['row1col1', 'row1col2']]",
)
if not isinstance(table_data, list):
return (
False,
f"Table data must be a list, got {type(table_data).__name__}. Required format: [['col1', 'col2'], ['row1col1', 'row1col2']]",
)
# Check if it's a 2D list
if not all(isinstance(row, list) for row in table_data):
non_list_rows = [
i for i, row in enumerate(table_data) if not isinstance(row, list)
]
return (
False,
f"All rows must be lists. Rows {non_list_rows} are not lists. Required format: [['col1', 'col2'], ['row1col1', 'row1col2']]",
)
# Check for empty rows
if any(len(row) == 0 for row in table_data):
empty_rows = [i for i, row in enumerate(table_data) if len(row) == 0]
return (
False,
f"Rows cannot be empty. Empty rows found at indices: {empty_rows}",
)
# Check column consistency
col_counts = [len(row) for row in table_data]
if len(set(col_counts)) > 1:
return (
False,
f"All rows must have the same number of columns. Found column counts: {col_counts}. Fix your data structure.",
)
rows = len(table_data)
cols = col_counts[0]
# Check dimension limits
if rows > self.validation_rules["table_max_rows"]:
return (
False,
f"Too many rows ({rows}). Maximum allowed: {self.validation_rules['table_max_rows']}",
)
if cols > self.validation_rules["table_max_columns"]:
return (
False,
f"Too many columns ({cols}). Maximum allowed: {self.validation_rules['table_max_columns']}",
)
# Check cell content types
for row_idx, row in enumerate(table_data):
for col_idx, cell in enumerate(row):
if cell is None:
return (
False,
f"Cell ({row_idx},{col_idx}) is None. All cells must be strings, use empty string '' for empty cells.",
)
if not isinstance(cell, str):
return (
False,
f"Cell ({row_idx},{col_idx}) is {type(cell).__name__}, not string. All cells must be strings. Value: {repr(cell)}",
)
return True, f"Valid table data: {rows}×{cols} table format"
def validate_text_formatting_params(
self,
bold: Optional[bool] = None,
italic: Optional[bool] = None,
underline: Optional[bool] = None,
strikethrough: Optional[bool] = None,
font_size: Optional[int] = None,
font_family: Optional[str] = None,
font_weight: Optional[int] = None,
text_color: Optional[str] = None,
background_color: Optional[str] = None,
link_url: Optional[str] = None,
clear_link: Optional[bool] = None,
baseline_offset: Optional[str] = None,
small_caps: Optional[bool] = None,
) -> Tuple[bool, str]:
"""
Validate text formatting parameters.
Args:
bold: Bold setting
italic: Italic setting
underline: Underline setting
strikethrough: Strikethrough setting
font_size: Font size in points
font_family: Font family name
text_color: Text color in "#RRGGBB" format
background_color: Background color in "#RRGGBB" format
link_url: Hyperlink URL (http/https)
Returns:
Tuple of (is_valid, error_message)
"""
# Check if at least one formatting option is provided
formatting_params = [
bold,
italic,
underline,
strikethrough,
font_size,
font_family,
font_weight,
text_color,
background_color,
link_url,
clear_link,
baseline_offset,
small_caps,
]
if all(param is None for param in formatting_params):
return (
False,
"At least one formatting parameter must be provided (bold, italic, underline, strikethrough, font_size, font_family, font_weight, text_color, background_color, link_url, clear_link, baseline_offset, or small_caps)",
)
# Validate boolean parameters
for param, name in [
(bold, "bold"),
(italic, "italic"),
(underline, "underline"),
(strikethrough, "strikethrough"),
]:
if param is not None and not isinstance(param, bool):
return (
False,
f"{name} parameter must be boolean (True/False), got {type(param).__name__}",
)
for param, name in [
(clear_link, "clear_link"),
(small_caps, "small_caps"),
]:
if param is not None and not isinstance(param, bool):
return (
False,
f"{name} parameter must be boolean (True/False), got {type(param).__name__}",
)
# Validate font size
if font_size is not None:
if not isinstance(font_size, int):
return (
False,
f"font_size must be an integer, got {type(font_size).__name__}",
)
min_size, max_size = self.validation_rules["font_size_range"]
if not (min_size <= font_size <= max_size):
return (
False,
f"font_size must be between {min_size} and {max_size} points, got {font_size}",
)
# Validate font family
if font_family is not None:
if not isinstance(font_family, str):
return (
False,
f"font_family must be a string, got {type(font_family).__name__}",
)
if not font_family.strip():
return False, "font_family cannot be empty"
if font_weight is not None:
if not isinstance(font_weight, int):
return (
False,
f"font_weight must be an integer, got {type(font_weight).__name__}",
)
min_weight, max_weight = self.validation_rules["font_weight_range"]
if not (min_weight <= font_weight <= max_weight) or font_weight % 100 != 0:
return (
False,
"font_weight must be a multiple of 100 between 100 and 900",
)
if font_family is None:
return False, "font_weight requires font_family to also be provided"
if clear_link and link_url is not None:
return False, "clear_link cannot be combined with link_url"
if baseline_offset is not None:
if not isinstance(baseline_offset, str):
return (
False,
"baseline_offset must be a string "
f"({', '.join(VALID_TEXT_BASELINE_OFFSETS)})",
)
if baseline_offset.upper() not in VALID_TEXT_BASELINE_OFFSETS:
return (
False,
"baseline_offset must be one of: "
f"{', '.join(VALID_TEXT_BASELINE_OFFSETS)}",
)
# Validate colors
is_valid, error_msg = self.validate_color_param(text_color, "text_color")
if not is_valid:
return False, error_msg
is_valid, error_msg = self.validate_color_param(
background_color, "background_color"
)
if not is_valid:
return False, error_msg
is_valid, error_msg = self.validate_link_url(link_url)
if not is_valid:
return False, error_msg
return True, ""
def validate_link_url(self, link_url: Optional[str]) -> Tuple[bool, str]:
"""Validate hyperlink URL parameters."""
if link_url is None:
return True, ""
if not isinstance(link_url, str):
return False, f"link_url must be a string, got {type(link_url).__name__}"
if not link_url.strip():
return False, "link_url cannot be empty"
parsed = urlparse(link_url)
if parsed.scheme not in ("http", "https"):
return False, "link_url must start with http:// or https://"
if not parsed.netloc:
return False, "link_url must include a valid host"
return True, ""
def validate_paragraph_style_params(
self,
heading_level: Optional[int] = None,
alignment: Optional[str] = None,
line_spacing: Optional[float] = None,
indent_first_line: Optional[float] = None,
indent_start: Optional[float] = None,
indent_end: Optional[float] = None,
space_above: Optional[float] = None,
space_below: Optional[float] = None,
named_style_type: Optional[str] = None,
direction: Optional[str] = None,
keep_lines_together: Optional[bool] = None,
keep_with_next: Optional[bool] = None,
avoid_widow_and_orphan: Optional[bool] = None,
page_break_before: Optional[bool] = None,
spacing_mode: Optional[str] = None,
shading_color: Optional[str] = None,
) -> Tuple[bool, str]:
"""
Validate paragraph style parameters.
Args:
heading_level: Heading level 0-6 (0 = NORMAL_TEXT, 1-6 = HEADING_N)
alignment: Text alignment - 'START', 'CENTER', 'END', or 'JUSTIFIED'
line_spacing: Line spacing multiplier (must be positive)
indent_first_line: First line indent in points
indent_start: Left/start indent in points
indent_end: Right/end indent in points
space_above: Space above paragraph in points
space_below: Space below paragraph in points
named_style_type: Direct named style (TITLE, SUBTITLE, HEADING_1..6, NORMAL_TEXT)
Returns:
Tuple of (is_valid, error_message)
"""
style_params = [
heading_level,
alignment,
line_spacing,
indent_first_line,
indent_start,
indent_end,
space_above,
space_below,
named_style_type,
direction,
keep_lines_together,
keep_with_next,
avoid_widow_and_orphan,
page_break_before,
spacing_mode,
shading_color,
]
if all(param is None for param in style_params):
return (
False,
"At least one paragraph style parameter must be provided (heading_level, alignment, line_spacing, indent_first_line, indent_start, indent_end, space_above, space_below, named_style_type, direction, keep_lines_together, keep_with_next, avoid_widow_and_orphan, page_break_before, spacing_mode, or shading_color)",
)
if heading_level is not None and named_style_type is not None:
return (
False,
"heading_level and named_style_type are mutually exclusive; provide only one",
)
if named_style_type is not None:
if named_style_type not in VALID_NAMED_STYLE_TYPES:
return (
False,
f"Invalid named_style_type '{named_style_type}'. Must be one of: {', '.join(VALID_NAMED_STYLE_TYPES)}",
)
if heading_level is not None and named_style_type is None:
if not isinstance(heading_level, int):
return (
False,
f"heading_level must be an integer, got {type(heading_level).__name__}",
)
min_level, max_level = self.validation_rules["heading_level_range"]
if not (min_level <= heading_level <= max_level):
return (
False,
f"heading_level must be between {min_level} and {max_level}, got {heading_level}",
)
if alignment is not None:
if not isinstance(alignment, str):
return (
False,
f"alignment must be a string, got {type(alignment).__name__}",
)
valid = self.validation_rules["valid_alignments"]
if alignment.upper() not in valid:
return (
False,
f"alignment must be one of: {', '.join(valid)}, got '{alignment}'",
)
if line_spacing is not None:
if not isinstance(line_spacing, (int, float)):
return (
False,
f"line_spacing must be a number, got {type(line_spacing).__name__}",
)
if line_spacing <= 0:
return False, "line_spacing must be positive"
for param, name in [
(indent_first_line, "indent_first_line"),
(indent_start, "indent_start"),
(indent_end, "indent_end"),
(space_above, "space_above"),
(space_below, "space_below"),
]:
if param is not None:
if not isinstance(param, (int, float)):
return (
False,
f"{name} must be a number, got {type(param).__name__}",
)
# indent_first_line may be negative (hanging indent)
if name != "indent_first_line" and param < 0:
return False, f"{name} must be non-negative, got {param}"
if direction is not None:
if not isinstance(direction, str):
return (
False,
f"direction must be a string, got {type(direction).__name__}",
)
if direction.upper() not in VALID_PARAGRAPH_DIRECTIONS:
return (
False,
"direction must be one of: "
f"{', '.join(VALID_PARAGRAPH_DIRECTIONS)}, got '{direction}'",
)
for param, name in [
(keep_lines_together, "keep_lines_together"),
(keep_with_next, "keep_with_next"),
(avoid_widow_and_orphan, "avoid_widow_and_orphan"),
(page_break_before, "page_break_before"),
]:
if param is not None and not isinstance(param, bool):
return (
False,
f"{name} must be boolean (True/False), got {type(param).__name__}",
)
if spacing_mode is not None:
if not isinstance(spacing_mode, str):
return (
False,
f"spacing_mode must be a string, got {type(spacing_mode).__name__}",
)
if spacing_mode.upper() not in VALID_PARAGRAPH_SPACING_MODES:
return (
False,
"spacing_mode must be one of: "
f"{', '.join(VALID_PARAGRAPH_SPACING_MODES)}, got '{spacing_mode}'",
)
is_valid, error_msg = self.validate_color_param(shading_color, "shading_color")
if not is_valid:
return False, error_msg
return True, ""
def validate_named_range_operation(
self,
name: Optional[str] = None,
start_index: Optional[int] = None,
end_index: Optional[int] = None,
named_range_id: Optional[str] = None,
named_range_name: Optional[str] = None,
) -> Tuple[bool, str]:
"""Validate named range create/delete/replace inputs."""
if name is not None:
if not isinstance(name, str):
return False, f"name must be a string, got {type(name).__name__}"
if not name.strip():
return False, "name cannot be empty"
if len(name) > 256:
return False, "name cannot exceed 256 UTF-16 code units"
if start_index is not None or end_index is not None:
is_valid, error_msg = self.validate_index_range(start_index, end_index)
if not is_valid:
return False, error_msg
if named_range_id is not None and not isinstance(named_range_id, str):
return (
False,
f"named_range_id must be a string, got {type(named_range_id).__name__}",
)
if named_range_name is not None and not isinstance(named_range_name, str):
return (
False,
f"named_range_name must be a string, got {type(named_range_name).__name__}",
)
return True, ""
def validate_document_style_params(
self,
background_color: Optional[str] = None,
margin_top: Optional[float] = None,
margin_bottom: Optional[float] = None,
margin_left: Optional[float] = None,
margin_right: Optional[float] = None,
margin_header: Optional[float] = None,
margin_footer: Optional[float] = None,
page_width: Optional[float] = None,
page_height: Optional[float] = None,
page_number_start: Optional[int] = None,
use_even_page_header_footer: Optional[bool] = None,
use_first_page_header_footer: Optional[bool] = None,
flip_page_orientation: Optional[bool] = None,
document_mode: Optional[str] = None,
) -> Tuple[bool, str]:
"""Validate updateDocumentStyle parameters."""
params = [
background_color,
margin_top,
margin_bottom,
margin_left,
margin_right,
margin_header,
margin_footer,
page_width,
page_height,
page_number_start,
use_even_page_header_footer,
use_first_page_header_footer,
flip_page_orientation,
document_mode,
]
if all(param is None for param in params):
return False, "At least one document style parameter must be provided"
for value, name in [
(margin_top, "margin_top"),
(margin_bottom, "margin_bottom"),
(margin_left, "margin_left"),
(margin_right, "margin_right"),
(margin_header, "margin_header"),
(margin_footer, "margin_footer"),
(page_width, "page_width"),
(page_height, "page_height"),
]:
if value is not None:
if not isinstance(value, (int, float)):
return False, f"{name} must be a number, got {type(value).__name__}"
if value <= 0:
return False, f"{name} must be positive, got {value}"
is_valid, error_msg = self.validate_color_param(
background_color, "background_color"
)
if not is_valid:
return False, error_msg
if page_number_start is not None:
if not isinstance(page_number_start, int):
return (
False,
f"page_number_start must be an integer, got {type(page_number_start).__name__}",
)
if page_number_start < 1:
return False, "page_number_start must be >= 1"
for value, name in [
(use_even_page_header_footer, "use_even_page_header_footer"),
(use_first_page_header_footer, "use_first_page_header_footer"),
(flip_page_orientation, "flip_page_orientation"),
]:
if value is not None and not isinstance(value, bool):
return (
False,
f"{name} must be boolean (True/False), got {type(value).__name__}",
)
if document_mode is not None:
if not isinstance(document_mode, str):
return (
False,
f"document_mode must be a string, got {type(document_mode).__name__}",
)
if document_mode.upper() not in VALID_DOCUMENT_MODES:
return (
False,
f"document_mode must be one of: {', '.join(VALID_DOCUMENT_MODES)}",
)
return True, ""
def validate_section_style_params(
self,
margin_top: Optional[float] = None,
margin_bottom: Optional[float] = None,
margin_left: Optional[float] = None,
margin_right: Optional[float] = None,
margin_header: Optional[float] = None,
margin_footer: Optional[float] = None,
page_number_start: Optional[int] = None,
use_first_page_header_footer: Optional[bool] = None,
flip_page_orientation: Optional[bool] = None,
content_direction: Optional[str] = None,
column_count: Optional[int] = None,
column_spacing: Optional[float] = None,
column_separator_style: Optional[str] = None,
) -> Tuple[bool, str]:
"""Validate updateSectionStyle parameters."""
params = [
margin_top,
margin_bottom,
margin_left,
margin_right,
margin_header,
margin_footer,
page_number_start,
use_first_page_header_footer,
flip_page_orientation,
content_direction,
column_count,
column_spacing,
column_separator_style,
]
if all(param is None for param in params):
return False, "At least one section style parameter must be provided"
is_valid, error_msg = self.validate_document_style_params(
margin_top=margin_top,
margin_bottom=margin_bottom,
margin_left=margin_left,
margin_right=margin_right,
margin_header=margin_header,
margin_footer=margin_footer,
page_number_start=page_number_start,
flip_page_orientation=flip_page_orientation,
)
if not is_valid and "At least one document style parameter" not in error_msg:
return False, error_msg
if use_first_page_header_footer is not None and not isinstance(
use_first_page_header_footer, bool
):
return (
False,
"use_first_page_header_footer must be boolean (True/False)",
)
if content_direction is not None:
if not isinstance(content_direction, str):
return (
False,
f"content_direction must be a string, got {type(content_direction).__name__}",
)
if content_direction.upper() not in VALID_CONTENT_DIRECTIONS:
return (
False,
"content_direction must be one of: "
f"{', '.join(VALID_CONTENT_DIRECTIONS)}",
)
if column_count is not None:
if not isinstance(column_count, int):
return (
False,
f"column_count must be an integer, got {type(column_count).__name__}",
)
if column_count < 1 or column_count > 3:
return False, "column_count must be between 1 and 3"
if column_spacing is not None:
if not isinstance(column_spacing, (int, float)):
return (
False,
f"column_spacing must be a number, got {type(column_spacing).__name__}",
)
if column_spacing < 0:
return False, "column_spacing must be non-negative"
if column_count is None:
return False, "column_spacing requires column_count to be provided"
if column_separator_style is not None:
if not isinstance(column_separator_style, str):
return (
False,
"column_separator_style must be a string, "
f"got {type(column_separator_style).__name__}",
)
if column_separator_style.upper() not in VALID_COLUMN_SEPARATOR_STYLES:
return (
False,
"column_separator_style must be one of: "
f"{', '.join(VALID_COLUMN_SEPARATOR_STYLES)}",
)
return True, ""
VALID_CONTENT_ALIGNMENTS = ("TOP", "MIDDLE", "BOTTOM")
def validate_table_cell_style_params(
self,
background_color: Optional[str] = None,
border_color: Optional[str] = None,
border_width: Optional[float] = None,
padding_top: Optional[float] = None,
padding_bottom: Optional[float] = None,
padding_left: Optional[float] = None,
padding_right: Optional[float] = None,
content_alignment: Optional[str] = None,
row_index: Optional[int] = None,
column_index: Optional[int] = None,
row_span: Optional[int] = None,
column_span: Optional[int] = None,
) -> Tuple[bool, str]:
"""
Validate table cell style parameters for updateTableCellStyle requests.
Args:
background_color: Cell background color in "#RRGGBB" format
border_color: Border color in "#RRGGBB" format
border_width: Border width in points
padding_top: Top padding in points (non-negative)
padding_bottom: Bottom padding in points (non-negative)
padding_left: Left padding in points (non-negative)
padding_right: Right padding in points (non-negative)
content_alignment: Vertical alignment ("TOP", "MIDDLE", "BOTTOM")
row_index: Optional starting row index for a targeted cell range
column_index: Optional starting column index for a targeted cell range
row_span: Optional row span for a targeted cell range
column_span: Optional column span for a targeted cell range
Returns:
Tuple of (is_valid, error_message)
"""
if all(
param is None
for param in (
background_color,
border_color,
border_width,
padding_top,
padding_bottom,
padding_left,
padding_right,
content_alignment,
)
):
return (
False,
"At least one table cell style parameter must be provided "
"(background_color, border_color, border_width, padding_top, "
"padding_bottom, padding_left, padding_right, or content_alignment)",
)
is_valid, error_msg = self.validate_color_param(
background_color, "background_color"
)
if not is_valid:
return False, error_msg
is_valid, error_msg = self.validate_color_param(border_color, "border_color")
if not is_valid:
return False, error_msg
if border_width is not None:
if not isinstance(border_width, (int, float)):
return (
False,
f"border_width must be a number, got {type(border_width).__name__}",
)
if border_width <= 0:
return False, f"border_width must be positive, got {border_width}"
for padding_value, padding_name in (
(padding_top, "padding_top"),
(padding_bottom, "padding_bottom"),
(padding_left, "padding_left"),
(padding_right, "padding_right"),
):
if padding_value is not None:
if isinstance(padding_value, bool) or not isinstance(
padding_value, (int, float)
):
return (
False,
f"{padding_name} must be a number, got {type(padding_value).__name__}",
)
if padding_value < 0:
return (
False,
f"{padding_name} must be non-negative, got {padding_value}",
)
if content_alignment is not None:
if not isinstance(content_alignment, str):
return (
False,
f"content_alignment must be a string, got {type(content_alignment).__name__}",
)
if content_alignment.upper() not in self.VALID_CONTENT_ALIGNMENTS:
return (
False,
f"content_alignment must be one of: {', '.join(self.VALID_CONTENT_ALIGNMENTS)}",
)
has_range_start = row_index is not None or column_index is not None
has_range_span = row_span is not None or column_span is not None
if has_range_start or has_range_span:
if row_index is None or column_index is None:
return (
False,
"row_index and column_index must both be provided when targeting a table range",
)
for value, name in (
(row_index, "row_index"),
(column_index, "column_index"),
):
if not isinstance(value, int):
return (
False,
f"{name} must be an integer, got {type(value).__name__}",
)
if value < 0:
return False, f"{name} must be non-negative, got {value}"
for value, name in (
(row_span, "row_span"),
(column_span, "column_span"),
):
if value is not None:
if not isinstance(value, int):
return (
False,
f"{name} must be an integer, got {type(value).__name__}",
)
if value <= 0:
return False, f"{name} must be positive, got {value}"
return True, ""
def validate_color_param(
self, color: Optional[str], param_name: str
) -> Tuple[bool, str]:
"""Validate color parameters (hex string "#RRGGBB")."""
if color is None:
return True, ""
if not isinstance(color, str):
return False, f"{param_name} must be a hex string like '#RRGGBB'"
if len(color) != 7 or not color.startswith("#"):
return False, f"{param_name} must be a hex string like '#RRGGBB'"
hex_color = color[1:]
if any(c not in "0123456789abcdefABCDEF" for c in hex_color):
return False, f"{param_name} must be a hex string like '#RRGGBB'"
return True, ""
def validate_index(self, index: int, context: str = "Index") -> Tuple[bool, str]:
"""
Validate a single document index.
Args:
index: Index to validate
context: Context description for error messages
Returns:
Tuple of (is_valid, error_message)
"""
if not isinstance(index, int):
return False, f"{context} must be an integer, got {type(index).__name__}"
if index < 0:
return (
False,
f"{context} {index} is negative. You MUST call inspect_doc_structure first to get the proper insertion index.",
)
return True, ""
def validate_index_range(
self,
start_index: int,
end_index: Optional[int] = None,
document_length: Optional[int] = None,
) -> Tuple[bool, str]:
"""
Validate document index ranges.
Args:
start_index: Starting index
end_index: Ending index (optional)
document_length: Total document length for bounds checking
Returns:
Tuple of (is_valid, error_message)
"""
# Validate start_index
if not isinstance(start_index, int):
return (
False,
f"start_index must be an integer, got {type(start_index).__name__}",
)
if start_index < 0:
return False, f"start_index cannot be negative, got {start_index}"
# Validate end_index if provided
if end_index is not None:
if not isinstance(end_index, int):
return (
False,
f"end_index must be an integer, got {type(end_index).__name__}",
)
if end_index <= start_index:
return (
False,
f"end_index ({end_index}) must be greater than start_index ({start_index})",
)
# Validate against document length if provided
if document_length is not None:
if start_index >= document_length:
return (
False,
f"start_index ({start_index}) exceeds document length ({document_length})",
)
if end_index is not None and end_index > document_length:
return (
False,
f"end_index ({end_index}) exceeds document length ({document_length})",
)
return True, ""
def validate_element_insertion_params(
self, element_type: str, index: int, **kwargs
) -> Tuple[bool, str]:
"""
Validate parameters for element insertion.
Args:
element_type: Type of element to insert
index: Insertion index
**kwargs: Additional parameters specific to element type
Returns:
Tuple of (is_valid, error_message)
"""
# Validate element type
if element_type not in self.validation_rules["valid_element_types"]:
valid_types = ", ".join(self.validation_rules["valid_element_types"])
return (
False,
f"Invalid element_type '{element_type}'. Must be one of: {valid_types}",
)
# Validate index
if not isinstance(index, int) or index < 0:
return False, f"index must be a non-negative integer, got {index}"
# Validate element-specific parameters
if element_type == "table":
rows = kwargs.get("rows")
columns = kwargs.get("columns")
if not rows or not columns:
return False, "Table insertion requires 'rows' and 'columns' parameters"
if not isinstance(rows, int) or not isinstance(columns, int):
return False, "Table rows and columns must be integers"
if rows <= 0 or columns <= 0:
return False, "Table rows and columns must be positive integers"
if rows > self.validation_rules["table_max_rows"]:
return (
False,
f"Too many rows ({rows}). Maximum: {self.validation_rules['table_max_rows']}",
)
if columns > self.validation_rules["table_max_columns"]:
return (
False,
f"Too many columns ({columns}). Maximum: {self.validation_rules['table_max_columns']}",
)
elif element_type == "list":
list_type = kwargs.get("list_type")
if not list_type:
return False, "List insertion requires 'list_type' parameter"
if list_type not in self.validation_rules["valid_list_types"]:
valid_types = ", ".join(self.validation_rules["valid_list_types"])
return (
False,
f"Invalid list_type '{list_type}'. Must be one of: {valid_types}",
)
return True, ""
def validate_header_footer_params(
self, section_type: str, header_footer_type: str = "DEFAULT"
) -> Tuple[bool, str]:
"""
Validate header/footer operation parameters.
Args:
section_type: Type of section ("header" or "footer")
header_footer_type: Specific header/footer type
Returns:
Tuple of (is_valid, error_message)
"""
if section_type not in self.validation_rules["valid_section_types"]:
valid_types = ", ".join(self.validation_rules["valid_section_types"])
return (
False,
f"section_type must be one of: {valid_types}, got '{section_type}'",
)
if header_footer_type not in self.validation_rules["valid_header_footer_types"]:
valid_types = ", ".join(self.validation_rules["valid_header_footer_types"])
return (
False,
f"header_footer_type must be one of: {valid_types}, got '{header_footer_type}'",
)
return True, ""
def validate_batch_operations(
self, operations: List[Dict[str, Any]]
) -> Tuple[bool, str]:
"""
Validate a list of batch operations.
Args:
operations: List of operation dictionaries
Returns:
Tuple of (is_valid, error_message)
"""
if not operations:
return False, "Operations list cannot be empty"
if not isinstance(operations, list):
return False, f"Operations must be a list, got {type(operations).__name__}"
# Validate each operation
for i, op in enumerate(operations):
if not isinstance(op, dict):
return (
False,
f"Operation {i + 1} must be a dictionary, got {type(op).__name__}",
)
if "type" not in op:
return False, f"Operation {i + 1} missing required 'type' field"
# Validate required fields for the operation type
is_valid, error_msg = validate_operation(op)
if not is_valid:
return False, f"Operation {i + 1}: {error_msg}"
op_type = op["type"]
if op_type == "format_text":
is_valid, error_msg = self.validate_text_formatting_params(
op.get("bold"),
op.get("italic"),
op.get("underline"),
op.get("strikethrough"),
op.get("font_size"),
op.get("font_family"),
op.get("font_weight"),
op.get("text_color"),
op.get("background_color"),
op.get("link_url"),
op.get("clear_link"),
op.get("baseline_offset"),
op.get("small_caps"),
)
if not is_valid:
return False, f"Operation {i + 1} (format_text): {error_msg}"
is_valid, error_msg = self.validate_index_range(
op["start_index"], op["end_index"]
)
if not is_valid:
return False, f"Operation {i + 1} (format_text): {error_msg}"
elif op_type == "update_paragraph_style":
is_valid, error_msg = self.validate_paragraph_style_params(
op.get("heading_level"),
op.get("alignment"),
op.get("line_spacing"),
op.get("indent_first_line"),
op.get("indent_start"),
op.get("indent_end"),
op.get("space_above"),
op.get("space_below"),
op.get("named_style_type"),
op.get("direction"),
op.get("keep_lines_together"),
op.get("keep_with_next"),
op.get("avoid_widow_and_orphan"),
op.get("page_break_before"),
op.get("spacing_mode"),
op.get("shading_color"),
)
if not is_valid:
return (
False,
f"Operation {i + 1} (update_paragraph_style): {error_msg}",
)
is_valid, error_msg = self.validate_index_range(
op["start_index"], op["end_index"]
)
if not is_valid:
return (
False,
f"Operation {i + 1} (update_paragraph_style): {error_msg}",
)
elif op_type == "update_table_cell_style":
is_valid, error_msg = self.validate_index(
op["table_start_index"], "table_start_index"
)
if not is_valid:
return (
False,
f"Operation {i + 1} (update_table_cell_style): {error_msg}",
)
is_valid, error_msg = self.validate_table_cell_style_params(
op.get("background_color"),
op.get("border_color"),
op.get("border_width"),
op.get("padding_top"),
op.get("padding_bottom"),
op.get("padding_left"),
op.get("padding_right"),
op.get("content_alignment"),
op.get("row_index"),
op.get("column_index"),
op.get("row_span"),
op.get("column_span"),
)
if not is_valid:
return (
False,
f"Operation {i + 1} (update_table_cell_style): {error_msg}",
)
elif op_type == "create_named_range":
is_valid, error_msg = self.validate_named_range_operation(
name=op.get("name"),
start_index=op.get("start_index"),
end_index=op.get("end_index"),
)
if not is_valid:
return False, f"Operation {i + 1} (create_named_range): {error_msg}"
elif op_type == "replace_named_range_content":
is_valid, error_msg = self.validate_named_range_operation(
named_range_id=op.get("named_range_id"),
named_range_name=op.get("named_range_name"),
)
if not is_valid:
return (
False,
f"Operation {i + 1} (replace_named_range_content): {error_msg}",
)
if not op.get("named_range_id") and not op.get("named_range_name"):
return (
False,
f"Operation {i + 1} (replace_named_range_content): named_range_id or named_range_name is required",
)
elif op_type == "delete_named_range":
is_valid, error_msg = self.validate_named_range_operation(
named_range_id=op.get("named_range_id"),
named_range_name=op.get("named_range_name"),
)
if not is_valid:
return False, f"Operation {i + 1} (delete_named_range): {error_msg}"
if not op.get("named_range_id") and not op.get("named_range_name"):
return (
False,
f"Operation {i + 1} (delete_named_range): named_range_id or named_range_name is required",
)
elif op_type == "insert_section_break":
if op.get("index") is not None:
is_valid, error_msg = self.validate_index(op["index"], "index")
if not is_valid:
return (
False,
f"Operation {i + 1} (insert_section_break): {error_msg}",
)
if op.get("section_type") is not None:
section_type = op["section_type"]
if (
not isinstance(section_type, str)
or section_type.upper() not in VALID_SECTION_TYPES
):
return (
False,
f"Operation {i + 1} (insert_section_break): section_type must be one of {', '.join(VALID_SECTION_TYPES)}",
)
elif op_type == "update_document_style":
is_valid, error_msg = self.validate_document_style_params(
background_color=op.get("background_color"),
margin_top=op.get("margin_top"),
margin_bottom=op.get("margin_bottom"),
margin_left=op.get("margin_left"),
margin_right=op.get("margin_right"),
margin_header=op.get("margin_header"),
margin_footer=op.get("margin_footer"),
page_width=op.get("page_width"),
page_height=op.get("page_height"),
page_number_start=op.get("page_number_start"),
use_even_page_header_footer=op.get("use_even_page_header_footer"),
use_first_page_header_footer=op.get("use_first_page_header_footer"),
flip_page_orientation=op.get("flip_page_orientation"),
document_mode=op.get("document_mode"),
)
if not is_valid:
return (
False,
f"Operation {i + 1} (update_document_style): {error_msg}",
)
elif op_type == "update_section_style":
is_valid, error_msg = self.validate_index_range(
op["start_index"], op["end_index"]
)
if not is_valid:
return (
False,
f"Operation {i + 1} (update_section_style): {error_msg}",
)
is_valid, error_msg = self.validate_section_style_params(
margin_top=op.get("margin_top"),
margin_bottom=op.get("margin_bottom"),
margin_left=op.get("margin_left"),
margin_right=op.get("margin_right"),
margin_header=op.get("margin_header"),
margin_footer=op.get("margin_footer"),
page_number_start=op.get("page_number_start"),
use_first_page_header_footer=op.get("use_first_page_header_footer"),
flip_page_orientation=op.get("flip_page_orientation"),
content_direction=op.get("content_direction"),
column_count=op.get("column_count"),
column_spacing=op.get("column_spacing"),
column_separator_style=op.get("column_separator_style"),
)
if not is_valid:
return (
False,
f"Operation {i + 1} (update_section_style): {error_msg}",
)
elif op_type == "create_bullet_list":
list_type = op.get("list_type", "UNORDERED")
if list_type not in ("UNORDERED", "ORDERED", "CHECKBOX", "NONE"):
return (
False,
f"Operation {i + 1} (create_bullet_list): list_type must be UNORDERED, ORDERED, CHECKBOX, or NONE",
)
bullet_preset = op.get("bullet_preset")
if (
bullet_preset is not None
and bullet_preset not in VALID_BULLET_PRESETS
):
return (
False,
f"Operation {i + 1} (create_bullet_list): bullet_preset must be one of {', '.join(VALID_BULLET_PRESETS)}",
)
return True, ""
def validate_text_content(
self, text: str, max_length: Optional[int] = None
) -> Tuple[bool, str]:
"""
Validate text content for insertion.
Args:
text: Text to validate
max_length: Maximum allowed length
Returns:
Tuple of (is_valid, error_message)
"""
if not isinstance(text, str):
return False, f"Text must be a string, got {type(text).__name__}"
max_len = max_length or self.validation_rules["max_text_length"]
if len(text) > max_len:
return False, f"Text too long ({len(text)} characters). Maximum: {max_len}"
return True, ""
def get_validation_summary(self) -> Dict[str, Any]:
"""
Get a summary of all validation rules and constraints.
Returns:
Dictionary containing validation rules
"""
return {
"constraints": self.validation_rules.copy(),
"supported_operations": {
"table_operations": ["create_table", "populate_table"],
"text_operations": [
"insert_text",
"format_text",
"find_replace",
"update_paragraph_style",
"update_table_cell_style",
],
"element_operations": [
"insert_table",
"insert_list",
"insert_page_break",
],
"header_footer_operations": ["update_header", "update_footer"],
},
"data_formats": {
"table_data": "2D list of strings: [['col1', 'col2'], ['row1col1', 'row1col2']]",
"text_formatting": "Optional boolean/integer parameters for styling",
"document_indices": "Non-negative integers for position specification",
},
}
|