@@ -713,3 +713,174 @@ fn test_errors_only_structured_output() {
713713 assert_eq ! ( records[ 0 ] [ "instance" ] , invalid) ;
714714 assert_eq ! ( records[ 0 ] [ "payload" ] [ "valid" ] , false ) ;
715715}
716+
717+ #[ test]
718+ fn test_validate_valid_schema ( ) {
719+ let dir = tempdir ( ) . unwrap ( ) ;
720+ let schema = create_temp_file ( & dir, "schema.json" , r#"{"type": "string"}"# ) ;
721+
722+ let mut cmd = cli ( ) ;
723+ cmd. arg ( & schema) ;
724+ let output = cmd. output ( ) . unwrap ( ) ;
725+ assert ! ( output. status. success( ) ) ;
726+
727+ let stdout = String :: from_utf8_lossy ( & output. stdout ) ;
728+ assert ! ( stdout. contains( "Schema is valid" ) ) ;
729+ }
730+
731+ #[ test]
732+ fn test_validate_invalid_schema ( ) {
733+ let dir = tempdir ( ) . unwrap ( ) ;
734+ let schema = create_temp_file (
735+ & dir,
736+ "schema.json" ,
737+ r#"{"type": "invalid_type", "minimum": "not a number"}"# ,
738+ ) ;
739+
740+ let mut cmd = cli ( ) ;
741+ cmd. arg ( & schema) ;
742+ let output = cmd. output ( ) . unwrap ( ) ;
743+ assert ! ( !output. status. success( ) ) ;
744+
745+ let stdout = String :: from_utf8_lossy ( & output. stdout ) ;
746+ assert ! ( stdout. contains( "Schema is invalid" ) ) ;
747+ }
748+
749+ #[ test]
750+ fn test_instance_validation_with_invalid_schema_structured_output ( ) {
751+ let dir = tempdir ( ) . unwrap ( ) ;
752+ let schema = create_temp_file (
753+ & dir,
754+ "schema.json" ,
755+ r#"{"type": "invalid_type", "minimum": "not a number"}"# ,
756+ ) ;
757+ let instance = create_temp_file ( & dir, "instance.json" , "42" ) ;
758+
759+ let mut cmd = cli ( ) ;
760+ cmd. arg ( & schema)
761+ . arg ( "--instance" )
762+ . arg ( & instance)
763+ . arg ( "--output" )
764+ . arg ( "flag" ) ;
765+ let output = cmd. output ( ) . unwrap ( ) ;
766+ assert ! ( !output. status. success( ) ) ;
767+
768+ let stdout = String :: from_utf8_lossy ( & output. stdout ) ;
769+ let json: serde_json:: Value = serde_json:: from_str ( & stdout) . expect ( "Should be valid JSON" ) ;
770+
771+ assert_eq ! ( json[ "output" ] , "flag" ) ;
772+ assert_eq ! ( json[ "payload" ] [ "valid" ] , false ) ;
773+ assert ! ( json[ "schema" ] . as_str( ) . unwrap( ) . ends_with( "schema.json" ) ) ;
774+ }
775+
776+ #[ test]
777+ fn test_instance_validation_with_invalid_schema_list_output ( ) {
778+ let dir = tempdir ( ) . unwrap ( ) ;
779+ let schema = create_temp_file (
780+ & dir,
781+ "schema.json" ,
782+ r#"{"type": "invalid_type", "minimum": "not a number"}"# ,
783+ ) ;
784+ let instance = create_temp_file ( & dir, "instance.json" , "42" ) ;
785+
786+ let mut cmd = cli ( ) ;
787+ cmd. arg ( & schema)
788+ . arg ( "--instance" )
789+ . arg ( & instance)
790+ . arg ( "--output" )
791+ . arg ( "list" ) ;
792+ let output = cmd. output ( ) . unwrap ( ) ;
793+ assert ! ( !output. status. success( ) ) ;
794+
795+ let stdout = String :: from_utf8_lossy ( & output. stdout ) ;
796+ let json: serde_json:: Value = serde_json:: from_str ( & stdout) . expect ( "Should be valid JSON" ) ;
797+
798+ assert_eq ! ( json[ "output" ] , "list" ) ;
799+ assert_eq ! ( json[ "payload" ] [ "valid" ] , false ) ;
800+ assert ! ( json[ "schema" ] . as_str( ) . unwrap( ) . ends_with( "schema.json" ) ) ;
801+ }
802+
803+ #[ test]
804+ fn test_instance_validation_with_invalid_schema_hierarchical_output ( ) {
805+ let dir = tempdir ( ) . unwrap ( ) ;
806+ let schema = create_temp_file (
807+ & dir,
808+ "schema.json" ,
809+ r#"{"type": "invalid_type", "minimum": "not a number"}"# ,
810+ ) ;
811+ let instance = create_temp_file ( & dir, "instance.json" , "42" ) ;
812+
813+ let mut cmd = cli ( ) ;
814+ cmd. arg ( & schema)
815+ . arg ( "--instance" )
816+ . arg ( & instance)
817+ . arg ( "--output" )
818+ . arg ( "hierarchical" ) ;
819+ let output = cmd. output ( ) . unwrap ( ) ;
820+ assert ! ( !output. status. success( ) ) ;
821+
822+ let stdout = String :: from_utf8_lossy ( & output. stdout ) ;
823+ let json: serde_json:: Value = serde_json:: from_str ( & stdout) . expect ( "Should be valid JSON" ) ;
824+
825+ assert_eq ! ( json[ "output" ] , "hierarchical" ) ;
826+ assert_eq ! ( json[ "payload" ] [ "valid" ] , false ) ;
827+ assert ! ( json[ "schema" ] . as_str( ) . unwrap( ) . ends_with( "schema.json" ) ) ;
828+ }
829+
830+ #[ test]
831+ fn test_validate_invalid_schema_list_output ( ) {
832+ let dir = tempdir ( ) . unwrap ( ) ;
833+ let schema = create_temp_file (
834+ & dir,
835+ "schema.json" ,
836+ r#"{"type": "invalid_type", "minimum": "not a number"}"# ,
837+ ) ;
838+
839+ let mut cmd = cli ( ) ;
840+ cmd. arg ( & schema) . arg ( "--output" ) . arg ( "list" ) ;
841+ let output = cmd. output ( ) . unwrap ( ) ;
842+ assert ! ( !output. status. success( ) ) ;
843+
844+ let stdout = String :: from_utf8_lossy ( & output. stdout ) ;
845+ let json: serde_json:: Value = serde_json:: from_str ( & stdout) . expect ( "Should be valid JSON" ) ;
846+
847+ assert_eq ! ( json[ "output" ] , "list" ) ;
848+ assert_eq ! ( json[ "payload" ] [ "valid" ] , false ) ;
849+ assert ! ( json[ "schema" ] . as_str( ) . unwrap( ) . ends_with( "schema.json" ) ) ;
850+ }
851+
852+ #[ test]
853+ fn test_validate_invalid_schema_hierarchical_output ( ) {
854+ let dir = tempdir ( ) . unwrap ( ) ;
855+ let schema = create_temp_file (
856+ & dir,
857+ "schema.json" ,
858+ r#"{"type": "invalid_type", "minimum": "not a number"}"# ,
859+ ) ;
860+
861+ let mut cmd = cli ( ) ;
862+ cmd. arg ( & schema) . arg ( "--output" ) . arg ( "hierarchical" ) ;
863+ let output = cmd. output ( ) . unwrap ( ) ;
864+ assert ! ( !output. status. success( ) ) ;
865+
866+ let stdout = String :: from_utf8_lossy ( & output. stdout ) ;
867+ let json: serde_json:: Value = serde_json:: from_str ( & stdout) . expect ( "Should be valid JSON" ) ;
868+
869+ assert_eq ! ( json[ "output" ] , "hierarchical" ) ;
870+ assert_eq ! ( json[ "payload" ] [ "valid" ] , false ) ;
871+ assert ! ( json[ "schema" ] . as_str( ) . unwrap( ) . ends_with( "schema.json" ) ) ;
872+ }
873+
874+ #[ test]
875+ fn test_validate_schema_with_json_parse_error ( ) {
876+ let dir = tempdir ( ) . unwrap ( ) ;
877+ let schema = create_temp_file ( & dir, "schema.json" , r#"{"type": "string"# ) ;
878+
879+ let mut cmd = cli ( ) ;
880+ cmd. arg ( & schema) . arg ( "--output" ) . arg ( "flag" ) ;
881+ let output = cmd. output ( ) . unwrap ( ) ;
882+ assert ! ( !output. status. success( ) ) ;
883+
884+ let stdout = String :: from_utf8_lossy ( & output. stdout ) ;
885+ assert ! ( stdout. contains( "Error:" ) ) ;
886+ }
0 commit comments