|
10 | 10 | from .entities import DEFAULT_GTS_CONFIG, GtsConfig, GtsEntity |
11 | 11 | from .files_reader import GtsFileReader |
12 | 12 | from .gts import GtsID, GtsWildcard |
| 13 | +from .gts_ref_validation import GtsRefValidationMode |
13 | 14 | from .path_resolver import GtsPathResolver |
14 | 15 | from .schema_cast import GtsEntityCastResult |
15 | 16 | from .store import GtsStore, GtsStoreQueryResult |
16 | 17 |
|
17 | 18 | # Interface helpers |
18 | 19 |
|
19 | 20 |
|
| 21 | +def _normalize_gts_ref_validation(value: Any) -> GtsRefValidationMode: |
| 22 | + if isinstance(value, GtsRefValidationMode): |
| 23 | + return value |
| 24 | + try: |
| 25 | + return GtsRefValidationMode(value) |
| 26 | + except (TypeError, ValueError): |
| 27 | + return GtsRefValidationMode.ANY_VALID |
| 28 | + |
| 29 | + |
20 | 30 | @dataclass |
21 | 31 | class GtsIdValidationResult: |
22 | 32 | """Result of validating a GTS ID format.""" |
@@ -384,8 +394,12 @@ def reload_from_path(self, path: str | builtins.list[str]) -> None: |
384 | 394 | self.store = GtsStore(self._reader) |
385 | 395 |
|
386 | 396 | def add_entity( |
387 | | - self, content: dict[str, Any], validate: bool = False |
| 397 | + self, |
| 398 | + content: dict[str, Any], |
| 399 | + validate: bool = False, |
| 400 | + gts_ref_validation: GtsRefValidationMode = GtsRefValidationMode.ANY_VALID, |
388 | 401 | ) -> GtsAddEntityResult: |
| 402 | + gts_ref_validation = _normalize_gts_ref_validation(gts_ref_validation) |
389 | 403 | entity = GtsEntity(content=content, cfg=self.cfg) |
390 | 404 |
|
391 | 405 | # For instances (non-schemas), require an id field from entity_id_fields |
@@ -429,9 +443,11 @@ def add_entity( |
429 | 443 | if entity.is_schema: |
430 | 444 | self.store.validate_schema_basic(entity.gts_id.id) |
431 | 445 | if validate: |
432 | | - self.store.validate_schema(entity.gts_id.id) |
| 446 | + self.store.validate_schema(entity.gts_id.id, gts_ref_validation) |
433 | 447 | elif validate: |
434 | | - self.store.validate_instance(entity.raw_id or entity.gts_id.id) |
| 448 | + self.store.validate_instance( |
| 449 | + entity.raw_id or entity.gts_id.id, gts_ref_validation |
| 450 | + ) |
435 | 451 | except Exception as e: # noqa: BLE001 - converted to a result object at API boundary |
436 | 452 | self.store.unregister(store_key) |
437 | 453 | if previous: |
@@ -652,34 +668,52 @@ def validate_json( |
652 | 668 | is_type_schema=entity.is_schema, |
653 | 669 | ) |
654 | 670 |
|
655 | | - def validate_instance(self, gts_id: str) -> GtsValidationResult: |
| 671 | + def validate_instance( |
| 672 | + self, |
| 673 | + gts_id: str, |
| 674 | + gts_ref_validation: GtsRefValidationMode = GtsRefValidationMode.ANY_VALID, |
| 675 | + ) -> GtsValidationResult: |
| 676 | + gts_ref_validation = _normalize_gts_ref_validation(gts_ref_validation) |
656 | 677 | try: |
657 | | - self.store.validate_instance(gts_id) |
| 678 | + self.store.validate_instance(gts_id, gts_ref_validation) |
658 | 679 | return GtsValidationResult(id=gts_id, ok=True) |
659 | 680 | except Exception as e: # noqa: BLE001 - converted to a result object at API boundary |
660 | 681 | return GtsValidationResult(id=gts_id, ok=False, error=str(e)) |
661 | 682 |
|
662 | | - def validate_schema(self, gts_id: str) -> GtsValidationResult: |
| 683 | + def validate_schema( |
| 684 | + self, |
| 685 | + gts_id: str, |
| 686 | + gts_ref_validation: GtsRefValidationMode = GtsRefValidationMode.ANY_VALID, |
| 687 | + ) -> GtsValidationResult: |
| 688 | + gts_ref_validation = _normalize_gts_ref_validation(gts_ref_validation) |
663 | 689 | try: |
664 | | - self.store.validate_schema(gts_id) |
| 690 | + self.store.validate_schema(gts_id, gts_ref_validation) |
665 | 691 | return GtsValidationResult(id=gts_id, ok=True) |
666 | 692 | except Exception as e: # noqa: BLE001 - converted to a result object at API boundary |
667 | 693 | return GtsValidationResult(id=gts_id, ok=False, error=str(e)) |
668 | 694 |
|
669 | | - def validate_entity(self, gts_id: str) -> GtsEntityValidationResult: |
670 | | - try: |
671 | | - parsed = GtsID(gts_id) |
672 | | - except Exception as e: # noqa: BLE001 - converted to a result object at API boundary |
673 | | - return GtsEntityValidationResult( |
674 | | - id=gts_id, ok=False, entity_type="", error=str(e) |
675 | | - ) |
| 695 | + def validate_entity( |
| 696 | + self, |
| 697 | + gts_id: str, |
| 698 | + gts_ref_validation: GtsRefValidationMode = GtsRefValidationMode.ANY_VALID, |
| 699 | + ) -> GtsEntityValidationResult: |
| 700 | + gts_ref_validation = _normalize_gts_ref_validation(gts_ref_validation) |
| 701 | + entity = self.store.get(gts_id) |
| 702 | + if entity: |
| 703 | + entity_type = "schema" if entity.is_schema else "instance" |
| 704 | + else: |
| 705 | + try: |
| 706 | + parsed = GtsID(gts_id) |
| 707 | + entity_type = "schema" if parsed.is_type else "instance" |
| 708 | + except Exception as e: # noqa: BLE001 - converted at API boundary |
| 709 | + return GtsEntityValidationResult( |
| 710 | + id=gts_id, ok=False, entity_type="", error=str(e) |
| 711 | + ) |
676 | 712 |
|
677 | | - if parsed.is_type: |
678 | | - entity_type = "schema" |
679 | | - result = self.validate_schema(gts_id) |
| 713 | + if entity_type == "schema": |
| 714 | + result = self.validate_schema(gts_id, gts_ref_validation) |
680 | 715 | else: |
681 | | - entity_type = "instance" |
682 | | - result = self.validate_instance(gts_id) |
| 716 | + result = self.validate_instance(gts_id, gts_ref_validation) |
683 | 717 |
|
684 | 718 | return GtsEntityValidationResult( |
685 | 719 | id=result.id, ok=result.ok, entity_type=entity_type, error=result.error |
|
0 commit comments