|
| 1 | +"""Pydantic models for validating YAML card and mapping structures. |
| 2 | +
|
| 3 | +This module provides validation models for OWASP Cornucopia YAML files, |
| 4 | +ensuring structural integrity while maintaining backward compatibility. |
| 5 | +""" |
| 6 | + |
| 7 | +from pydantic import BaseModel, Field, ValidationError, ConfigDict |
| 8 | +from typing import Dict, List, Optional, Any |
| 9 | + |
| 10 | + |
| 11 | +class Card(BaseModel): |
| 12 | + """Minimal validation for card YAML structure. |
| 13 | + |
| 14 | + Validates core required fields while allowing additional fields |
| 15 | + like capec, stride, owasp_asvs, etc. through extra="allow". |
| 16 | + """ |
| 17 | + model_config = ConfigDict(extra="allow") |
| 18 | + |
| 19 | + id: str = Field(..., min_length=1, description="Required card ID (e.g. VE2, AT3)") |
| 20 | + value: str = Field(..., min_length=1, description="Required card value (e.g. 2, K, A)") |
| 21 | + desc: str = Field(..., min_length=10, description="Required card description") |
| 22 | + url: Optional[str] = None |
| 23 | + misc: Optional[str] = None |
| 24 | + |
| 25 | + |
| 26 | +class MappingCard(BaseModel): |
| 27 | + """Minimal validation for mapping card structure. |
| 28 | + |
| 29 | + Mapping files don't have descriptions, only id, value, and mapping data. |
| 30 | + """ |
| 31 | + model_config = ConfigDict(extra="allow") |
| 32 | + |
| 33 | + id: str = Field(..., min_length=1, description="Required card ID (e.g. VE2, AT3)") |
| 34 | + value: str = Field(..., min_length=1, description="Required card value (e.g. 2, K, A)") |
| 35 | + url: Optional[str] = None |
| 36 | + |
| 37 | + |
| 38 | +class Suit(BaseModel): |
| 39 | + """Validation for suit structure. |
| 40 | + |
| 41 | + A suit contains an ID, name, and a collection of cards. |
| 42 | + """ |
| 43 | + model_config = ConfigDict(extra="allow") |
| 44 | + |
| 45 | + id: str = Field(..., min_length=1) |
| 46 | + name: str = Field(..., min_length=1) |
| 47 | + cards: List[Card] = Field(..., min_length=1) |
| 48 | + |
| 49 | + |
| 50 | +class MappingSuit(BaseModel): |
| 51 | + """Validation for mapping suit structure. |
| 52 | + |
| 53 | + A mapping suit contains an ID, name, and a collection of mapping cards. |
| 54 | + """ |
| 55 | + model_config = ConfigDict(extra="allow") |
| 56 | + |
| 57 | + id: str = Field(..., min_length=1) |
| 58 | + name: str = Field(..., min_length=1) |
| 59 | + cards: List[MappingCard] = Field(..., min_length=1) |
| 60 | + |
| 61 | + |
| 62 | +class Meta(BaseModel): |
| 63 | + """Validation for metadata section. |
| 64 | + |
| 65 | + Contains edition, component, language, and version information. |
| 66 | + Additional fields like layouts, templates, and languages are allowed. |
| 67 | + """ |
| 68 | + model_config = ConfigDict(extra="allow") |
| 69 | + |
| 70 | + edition: str |
| 71 | + component: str |
| 72 | + language: str |
| 73 | + version: str |
| 74 | + |
| 75 | + |
| 76 | +class CardYAML(BaseModel): |
| 77 | + """Top-level card YAML structure. |
| 78 | + |
| 79 | + Represents the complete structure of a card YAML file, |
| 80 | + including metadata, suits, and optional paragraphs. |
| 81 | + """ |
| 82 | + model_config = ConfigDict(extra="allow") |
| 83 | + |
| 84 | + meta: Meta |
| 85 | + suits: List[Suit] = Field(..., min_length=1) |
| 86 | + |
| 87 | + |
| 88 | +class MappingYAML(BaseModel): |
| 89 | + """Top-level mapping YAML structure. |
| 90 | + |
| 91 | + Represents the complete structure of a mapping YAML file, |
| 92 | + which includes additional mapping information like CAPEC, ASVS, etc. |
| 93 | + """ |
| 94 | + model_config = ConfigDict(extra="allow") |
| 95 | + |
| 96 | + meta: Meta |
| 97 | + suits: List[MappingSuit] = Field(..., min_length=1) |
| 98 | + |
| 99 | + |
| 100 | +# Export ValidationError for convenience |
| 101 | +__all__ = ['Card', 'MappingCard', 'Suit', 'MappingSuit', 'Meta', 'CardYAML', 'MappingYAML', 'ValidationError'] |
0 commit comments