|
4 | 4 | from pydantic import model_validator, field_validator, Field, ValidationError
|
5 | 5 | from pyparsing import Word, alphas, nums, Literal, Combine, StringEnd, alphanums, ParseException
|
6 | 6 | import pyparsing
|
| 7 | +from spellchecker import SpellChecker |
7 | 8 |
|
8 | 9 |
|
9 | 10 | from .validation_helper import ValidatorHelper, ParsePattern
|
@@ -178,14 +179,54 @@ def validate_ifc_input(cls, value):
|
178 | 179 |
|
179 | 180 | @field_validator('description')
|
180 | 181 | def validate_description(cls, value=list) -> list:
|
181 |
| - """must include a description of the rule that start with "The rule verifies ...""" # allow for comma's |
182 |
| - if not any(value.startswith(f"{prefix} rule verifies{optional_comma}") for prefix in ("This", "The") for optional_comma in ("", ",")): |
| 182 | + """ |
| 183 | + Validates that the description starts with 'The rule verifies ...'. |
| 184 | + Captures cases where an incorrect prefix is used or the format is entirely incorrect. |
| 185 | + """ |
| 186 | + valid_prefix = "The" |
| 187 | + invalid_prefix = "This" |
| 188 | + |
| 189 | + valid_start = any(value.startswith(f"{prefix} rule verifies{optional_comma}") |
| 190 | + for prefix in [valid_prefix, invalid_prefix] |
| 191 | + for optional_comma in ("", ",")) |
| 192 | + |
| 193 | + incorrect_prefix_start = any(value.startswith(f"{prefix} rule verifies{optional_comma}") |
| 194 | + for prefix in (invalid_prefix,) |
| 195 | + for optional_comma in ("", ",")) |
| 196 | + |
| 197 | + if incorrect_prefix_start: |
183 | 198 | raise ProtocolError(
|
184 | 199 | value=value,
|
185 |
| - message=f"The description must start with 'The rule verifies', it now starts with {value}" |
| 200 | + message=f"The description starts with an incorrect prefix '{invalid_prefix}'. It should start with 'The rule verifies'." |
186 | 201 | )
|
187 |
| - return value |
188 | 202 |
|
| 203 | + if not valid_start: |
| 204 | + spell = SpellChecker() |
| 205 | + misspelled_words = spell.unknown(value.split()[:4]) |
| 206 | + if misspelled_words: |
| 207 | + corrections = {word: spell.correction(word) for word in misspelled_words} |
| 208 | + corrected_string = ' '.join([corrections.get(word, word) for word in value.split()]) |
| 209 | + corrected_valid_start = any(corrected_string.startswith(f"{prefix} rule verifies{optional_comma}") |
| 210 | + for prefix in [valid_prefix] |
| 211 | + for optional_comma in ("", ",")) |
| 212 | + |
| 213 | + message = f"The feature description contains spelling errors. Here are the incorrect words and their suggested corrections: {corrections}." |
| 214 | + |
| 215 | + if not corrected_valid_start: |
| 216 | + message += f" Additionally, after corrections, the description still does not start with 'The rule verifies'. It starts with '{' '.join(corrected_string.split()[:4])}'." |
| 217 | + |
| 218 | + raise ProtocolError( |
| 219 | + value=value, |
| 220 | + message=message |
| 221 | + ) |
| 222 | + else: |
| 223 | + raise ProtocolError( |
| 224 | + value=value, |
| 225 | + message=f"The description must start with 'The rule verifies', but it now starts with '{' '.join(value.split()[:4])}'." |
| 226 | + ) |
| 227 | + |
| 228 | + return value |
| 229 | + |
189 | 230 | @field_validator('steps')
|
190 | 231 | def validate_steps(cls, value):
|
191 | 232 | """Check only correct keywords are applied: 'Given', 'Then', 'And'"""
|
|
0 commit comments