@@ -884,3 +884,125 @@ fn test_validate_schema_with_json_parse_error() {
884884 let stdout = String :: from_utf8_lossy ( & output. stdout ) ;
885885 assert ! ( stdout. contains( "Error:" ) ) ;
886886}
887+
888+ #[ test]
889+ fn test_validate_schema_with_invalid_referenced_schema ( ) {
890+ // This test verifies that when a schema references another schema via $ref,
891+ // and that referenced schema is invalid, the validation should fail.
892+ let dir = tempdir ( ) . unwrap ( ) ;
893+
894+ // Main schema is structurally valid
895+ let main_schema = create_temp_file (
896+ & dir,
897+ "main.json" ,
898+ r#"{
899+ "$schema": "http://json-schema.org/draft-07/schema#",
900+ "type": "object",
901+ "properties": {
902+ "user": { "$ref": "user.json" }
903+ }
904+ }"# ,
905+ ) ;
906+
907+ // Referenced schema is structurally INVALID (bad type value)
908+ let _ref_schema = create_temp_file (
909+ & dir,
910+ "user.json" ,
911+ r#"{
912+ "type": "invalid_type_here",
913+ "properties": {
914+ "name": { "type": "string" }
915+ }
916+ }"# ,
917+ ) ;
918+
919+ let mut cmd = cli ( ) ;
920+ cmd. arg ( & main_schema) ;
921+ let output = cmd. output ( ) . unwrap ( ) ;
922+
923+ // Schema validation should fail because the referenced schema is invalid
924+ assert ! ( !output. status. success( ) ) ;
925+ let stdout = String :: from_utf8_lossy ( & output. stdout ) ;
926+ assert ! ( stdout. contains( "Schema is invalid" ) ) ;
927+ }
928+
929+ #[ test]
930+ fn test_validate_schema_with_valid_referenced_schema ( ) {
931+ // This test verifies that when all referenced schemas are valid, validation succeeds.
932+ let dir = tempdir ( ) . unwrap ( ) ;
933+
934+ let main_schema = create_temp_file (
935+ & dir,
936+ "main.json" ,
937+ r#"{
938+ "$schema": "http://json-schema.org/draft-07/schema#",
939+ "type": "object",
940+ "properties": {
941+ "user": { "$ref": "user.json" }
942+ }
943+ }"# ,
944+ ) ;
945+
946+ // Referenced schema is structurally VALID
947+ let _ref_schema = create_temp_file (
948+ & dir,
949+ "user.json" ,
950+ r#"{
951+ "type": "object",
952+ "properties": {
953+ "name": { "type": "string" }
954+ }
955+ }"# ,
956+ ) ;
957+
958+ let mut cmd = cli ( ) ;
959+ cmd. arg ( & main_schema) ;
960+ let output = cmd. output ( ) . unwrap ( ) ;
961+
962+ // Schema validation should succeed because all schemas are valid
963+ assert ! ( output. status. success( ) ) ;
964+ let stdout = String :: from_utf8_lossy ( & output. stdout ) ;
965+ assert ! ( stdout. contains( "Schema is valid" ) ) ;
966+ }
967+
968+ #[ test]
969+ fn test_validate_schema_with_invalid_ref_structured_output ( ) {
970+ // This test verifies structured output when root schema is valid but referenced schema is invalid.
971+ // This exercises the code path where flag_output.valid is true, but build fails.
972+ let dir = tempdir ( ) . unwrap ( ) ;
973+
974+ let main_schema = create_temp_file (
975+ & dir,
976+ "main.json" ,
977+ r#"{
978+ "$schema": "http://json-schema.org/draft-07/schema#",
979+ "type": "object",
980+ "properties": {
981+ "user": { "$ref": "user.json" }
982+ }
983+ }"# ,
984+ ) ;
985+
986+ // Referenced schema is structurally INVALID
987+ let _ref_schema = create_temp_file (
988+ & dir,
989+ "user.json" ,
990+ r#"{
991+ "type": "invalid_type_here",
992+ "properties": {
993+ "name": { "type": "string" }
994+ }
995+ }"# ,
996+ ) ;
997+
998+ let mut cmd = cli ( ) ;
999+ cmd. arg ( & main_schema) . arg ( "--output" ) . arg ( "flag" ) ;
1000+ let output = cmd. output ( ) . unwrap ( ) ;
1001+
1002+ // Should fail
1003+ assert ! ( !output. status. success( ) ) ;
1004+
1005+ // Should get an error message (not structured output since build fails before we can output)
1006+ let stdout = String :: from_utf8_lossy ( & output. stdout ) ;
1007+ assert ! ( stdout. contains( "Error:" ) ) ;
1008+ }
0 commit comments