|
6 | 6 |
|
7 | 7 | use \Exception; |
8 | 8 | use Stolt\LlmsTxt\Section\Link; |
| 9 | +use Stolt\LlmsTxt\Validation\ValidationError; |
| 10 | +use Stolt\LlmsTxt\Validation\ValidationResult; |
9 | 11 |
|
10 | 12 | final class LlmsTxt |
11 | 13 | { |
@@ -112,15 +114,55 @@ public function toString(): string |
112 | 114 | } |
113 | 115 |
|
114 | 116 | /** |
| 117 | + * Validates a given llms.txt file content. |
| 118 | + * |
| 119 | + * If `$detailed` is false (default), it returns a simple boolean for backward compatibility. |
| 120 | + * If `$detailed` is true, returns a `ValidationResult` object with rich diagnostics. |
| 121 | + * |
| 122 | + * @param bool $detailed Whether to return a ValidationResult instead of a boolean. |
| 123 | + * |
115 | 124 | * @throws Exception |
| 125 | + * @return bool|ValidationResult |
116 | 126 | */ |
117 | | - public function validate(): bool |
| 127 | + public function validate(bool $detailed = false): bool|ValidationResult |
118 | 128 | { |
119 | 129 | if ($this->hasBeenParsed) { |
| 130 | + |
| 131 | + |
120 | 132 | if ($this->title !== '' && $this->description !== '' && $this->details !== '' && \count($this->sections) > 0 && \count($this->sections[0]->getLinks()) > 0) { |
| 133 | + if ($detailed) { |
| 134 | + return new ValidationResult(); |
| 135 | + } |
121 | 136 | return true; |
122 | 137 | } |
123 | 138 |
|
| 139 | + if ($detailed) { |
| 140 | + $result = new ValidationResult(); |
| 141 | + |
| 142 | + if (!isset($this->title) || empty($this->title)) { |
| 143 | + $result->addError(new ValidationError('Missing title')); |
| 144 | + } |
| 145 | + |
| 146 | + if (!isset($this->description) || empty($this->description)) { |
| 147 | + $result->addError(new ValidationError('Missing description')); |
| 148 | + } |
| 149 | + |
| 150 | + if (!isset($this->details) || empty($this->details)) { |
| 151 | + $result->addError(new ValidationError('Missing details')); |
| 152 | + } |
| 153 | + |
| 154 | + if (\count($this->sections) === 0) { |
| 155 | + $result->addError(new ValidationError('Missing at least one section')); |
| 156 | + $result->addError(new ValidationError('Missing at least one section link')); |
| 157 | + } |
| 158 | + |
| 159 | + if (\count($this->sections) > 0 && \count($this->sections[0]->getLinks()) === 0) { |
| 160 | + $result->addError(new ValidationError('Missing at least one section link')); |
| 161 | + } |
| 162 | + |
| 163 | + return $result; |
| 164 | + } |
| 165 | + |
124 | 166 | return false; |
125 | 167 | } |
126 | 168 |
|
|
0 commit comments