askrobots / dbbasic-content
Public
WordPress-style content management with block-based pages, stored as JSON files on disk instead of MySQL.
blocks.py · 3.6 KB · 144 lines
| 1 | """ |
| 2 | Block definitions and validation. |
| 3 | |
| 4 | Defines the block types supported by the content system. |
| 5 | """ |
| 6 | |
| 7 | from typing import Dict, Any, List, Tuple |
| 8 | from enum import Enum |
| 9 | |
| 10 | |
| 11 | class BlockType(str, Enum): |
| 12 | """Supported block types (extensible).""" |
| 13 | PARAGRAPH = "paragraph" |
| 14 | HEADING = "heading" |
| 15 | LIST = "list" |
| 16 | CARD = "card" |
| 17 | CARD_LIST = "card_list" |
| 18 | CODE = "code" |
| 19 | IMAGE = "image" |
| 20 | QUOTE = "quote" |
| 21 | |
| 22 | |
| 23 | # Block schemas for validation |
| 24 | BLOCK_SCHEMAS = { |
| 25 | BlockType.PARAGRAPH: { |
| 26 | "required": ["content"], |
| 27 | "properties": { |
| 28 | "content": {"type": "string"}, |
| 29 | } |
| 30 | }, |
| 31 | BlockType.HEADING: { |
| 32 | "required": ["content"], |
| 33 | "properties": { |
| 34 | "level": {"type": "integer", "min": 1, "max": 6}, |
| 35 | "content": {"type": "string"}, |
| 36 | } |
| 37 | }, |
| 38 | BlockType.LIST: { |
| 39 | "required": ["items"], |
| 40 | "properties": { |
| 41 | "listType": {"type": "string", "enum": ["ordered", "unordered"]}, |
| 42 | "items": {"type": "array"}, |
| 43 | } |
| 44 | }, |
| 45 | BlockType.CARD: { |
| 46 | "required": ["content"], |
| 47 | "properties": { |
| 48 | "header": {"type": "string"}, |
| 49 | "content": {"type": "string"}, |
| 50 | } |
| 51 | }, |
| 52 | BlockType.CARD_LIST: { |
| 53 | "required": ["cards"], |
| 54 | "properties": { |
| 55 | "cards": {"type": "array"}, |
| 56 | } |
| 57 | }, |
| 58 | BlockType.CODE: { |
| 59 | "required": ["code"], |
| 60 | "properties": { |
| 61 | "language": {"type": "string"}, |
| 62 | "code": {"type": "string"}, |
| 63 | "title": {"type": "string"}, |
| 64 | } |
| 65 | }, |
| 66 | BlockType.IMAGE: { |
| 67 | "required": ["src"], |
| 68 | "properties": { |
| 69 | "src": {"type": "string"}, |
| 70 | "alt": {"type": "string"}, |
| 71 | "caption": {"type": "string"}, |
| 72 | } |
| 73 | }, |
| 74 | BlockType.QUOTE: { |
| 75 | "required": ["content"], |
| 76 | "properties": { |
| 77 | "content": {"type": "string"}, |
| 78 | "author": {"type": "string"}, |
| 79 | "source": {"type": "string"}, |
| 80 | } |
| 81 | }, |
| 82 | } |
| 83 | |
| 84 | |
| 85 | def validate_block(block: Dict[str, Any]) -> Tuple[bool, str]: |
| 86 | """ |
| 87 | Validate a block structure. |
| 88 | |
| 89 | Args: |
| 90 | block: Block dict with 'type' and 'data' keys |
| 91 | |
| 92 | Returns: |
| 93 | Tuple of (is_valid, error_message) |
| 94 | """ |
| 95 | if not isinstance(block, dict): |
| 96 | return False, "Block must be a dictionary" |
| 97 | |
| 98 | if 'type' not in block: |
| 99 | return False, "Block missing 'type' field" |
| 100 | |
| 101 | if 'data' not in block: |
| 102 | return False, "Block missing 'data' field" |
| 103 | |
| 104 | block_type = block['type'] |
| 105 | block_data = block['data'] |
| 106 | |
| 107 | # Check if block type is recognized |
| 108 | if block_type not in [bt.value for bt in BlockType]: |
| 109 | # Allow unknown block types (extensibility) |
| 110 | return True, "" |
| 111 | |
| 112 | # Validate against schema |
| 113 | schema = BLOCK_SCHEMAS.get(block_type) |
| 114 | if not schema: |
| 115 | return True, "" # No schema, assume valid |
| 116 | |
| 117 | # Check required fields |
| 118 | for required_field in schema.get('required', []): |
| 119 | if required_field not in block_data: |
| 120 | return False, f"Block type '{block_type}' requires field '{required_field}'" |
| 121 | |
| 122 | return True, "" |
| 123 | |
| 124 | |
| 125 | def validate_blocks(blocks: List[Dict[str, Any]]) -> Tuple[bool, List[str]]: |
| 126 | """ |
| 127 | Validate a list of blocks. |
| 128 | |
| 129 | Args: |
| 130 | blocks: List of block dicts |
| 131 | |
| 132 | Returns: |
| 133 | Tuple of (is_valid, list_of_errors) |
| 134 | """ |
| 135 | if not isinstance(blocks, list): |
| 136 | return False, ["Blocks must be a list"] |
| 137 | |
| 138 | errors = [] |
| 139 | for i, block in enumerate(blocks): |
| 140 | is_valid, error = validate_block(block) |
| 141 | if not is_valid: |
| 142 | errors.append(f"Block {i}: {error}") |
| 143 | |
| 144 | return len(errors) == 0, errors |