@@ -535,74 +535,32 @@ def pydantic_validator(self, stac_type: str) -> Dict:
535535 dict: A dictionary containing validation results.
536536 """
537537 message = self .create_message (stac_type , "pydantic" )
538-
539- # Initialize schema as empty list to avoid None issues
540538 message ["schema" ] = ["" ]
541539
542540 try :
543- # Import here to make stac-pydantic an optional dependency
544- try :
545- print ("Attempting to import pydantic and stac_pydantic..." )
546- from pydantic import ValidationError # type: ignore
547- from stac_pydantic .extensions import validate_extensions # type: ignore
548-
549- print ("Successfully imported pydantic and stac_pydantic" )
550- except ImportError as ie :
551- print ("Import error: " + str (ie ))
552- raise ImportError (
553- "stac-pydantic is not installed. Install with 'pip install stac-validator[pydantic]'"
554- )
541+ # Import dependencies
542+ from pydantic import ValidationError # type: ignore
543+ from stac_pydantic import Catalog , Collection , Item # type: ignore
544+ from stac_pydantic .extensions import validate_extensions # type: ignore
555545
556546 # Validate based on STAC type
557547 if stac_type == "ITEM" :
558- print ("Validating ITEM with stac-pydantic" )
559- from stac_pydantic import Item # type: ignore
560-
561548 item_model = Item .model_validate (self .stac_content )
562549 message ["schema" ] = ["stac-pydantic Item model" ]
563- print ("Set schema to: " + str (message ["schema" ]))
564-
565- # Validate extensions if present
566- if (
567- "stac_extensions" in self .stac_content
568- and self .stac_content ["stac_extensions" ]
569- ):
570- extension_schemas = []
571- validate_extensions (item_model , reraise_exception = True )
572- for ext in self .stac_content ["stac_extensions" ]:
573- extension_schemas .append (ext )
574- message ["extension_schemas" ] = extension_schemas
550+ self ._validate_extensions (item_model , message , validate_extensions )
575551
576552 elif stac_type == "COLLECTION" :
577- print ("Validating COLLECTION with stac-pydantic" )
578- from stac_pydantic import Collection # type: ignore
579-
580553 collection_model = Collection .model_validate (self .stac_content )
581- message ["schema" ] = ["stac-pydantic Collection model" ]
582- print ("Set schema to: " + str (message ["schema" ]))
583-
584- # Validate extensions if present
585- if (
586- "stac_extensions" in self .stac_content
587- and self .stac_content ["stac_extensions" ]
588- ):
589- extension_schemas = []
590- validate_extensions (collection_model , reraise_exception = True )
591- for ext in self .stac_content ["stac_extensions" ]:
592- extension_schemas .append (ext )
593- message ["extension_schemas" ] = extension_schemas
554+ message ["schema" ] = [
555+ "stac-pydantic Collection model"
556+ ] # Fix applied here
557+ self ._validate_extensions (
558+ collection_model , message , validate_extensions
559+ )
594560
595561 elif stac_type == "CATALOG" :
596- print ("Validating CATALOG with stac-pydantic" )
597- from stac_pydantic import Catalog # type: ignore
598-
599- catalog_model = Catalog .model_validate (self .stac_content )
562+ Catalog .model_validate (self .stac_content )
600563 message ["schema" ] = ["stac-pydantic Catalog model" ]
601- print ("Set schema to: " + str (message ["schema" ]))
602-
603- # For catalogs, we don't need to validate extensions, but we still need to use the model
604- # to avoid flake8 warnings
605- _ = catalog_model # Acknowledge that we're using the model for validation
606564
607565 else :
608566 raise ValueError (
@@ -611,36 +569,43 @@ def pydantic_validator(self, stac_type: str) -> Dict:
611569
612570 self .valid = True
613571 message ["model_validation" ] = "passed"
614- print ("Validation passed, final message: " + str (message ))
615572
616- except ImportError as e :
617- self .valid = False
618- print ("Import error in pydantic_validator: " + str (e ))
619- message .update (self .create_err_msg ("ImportError" , str (e )))
620573 except ValidationError as e :
621574 self .valid = False
622- # Provide more context for validation errors
623- errors = e .errors ()
624- error_details = []
625- for error in errors :
626- loc = " -> " .join ([str (loc ) for loc in error .get ("loc" , [])])
627- msg = error .get ("msg" , "" )
628- error_details .append (f"{ loc } : { msg } " )
629-
630- error_message = f"Pydantic validation failed: { '; ' .join (error_details )} "
575+ error_details = [
576+ f"{ ' -> ' .join (map (str , error .get ('loc' , [])))} : { error .get ('msg' , '' )} "
577+ for error in e .errors ()
578+ ]
579+ error_message = f"Pydantic validation failed for { stac_type } : { '; ' .join (error_details )} "
631580 message .update (
632581 self .create_err_msg ("PydanticValidationError" , error_message )
633582 )
634583
635584 except Exception as e :
636585 self .valid = False
637- error_message = str (e )
638- message .update (
639- self .create_err_msg ("PydanticValidationError" , error_message )
640- )
586+ message .update (self .create_err_msg ("PydanticValidationError" , str (e )))
641587
642588 return message
643589
590+ def _validate_extensions (self , model , message : Dict , validate_extensions ) -> None :
591+ """
592+ Validate extensions for a given Pydantic model.
593+
594+ Args:
595+ model: The Pydantic model instance.
596+ message (dict): The validation message dictionary to update.
597+ validate_extensions: The function to validate extensions.
598+ """
599+ if (
600+ "stac_extensions" in self .stac_content
601+ and self .stac_content ["stac_extensions" ]
602+ ):
603+ extension_schemas = []
604+ validate_extensions (model , reraise_exception = True )
605+ for ext in self .stac_content ["stac_extensions" ]:
606+ extension_schemas .append (ext )
607+ message ["extension_schemas" ] = extension_schemas
608+
644609 def run (self ) -> bool :
645610 """
646611 Run the STAC validation process based on the input parameters.
0 commit comments