@@ -250,9 +250,16 @@ impl SemConvSpecWithProvenance {
250250 pub fn from_file < P : AsRef < Path > > (
251251 registry_id : & str ,
252252 path : P ,
253- validator : & JsonSchemaValidator ,
253+ unversioned_validator : & JsonSchemaValidator ,
254+ versioned_validator : & JsonSchemaValidator ,
254255 ) -> WResult < SemConvSpecWithProvenance , Error > {
255- Self :: from_file_with_mapped_path ( registry_id, path, validator, |path| path)
256+ Self :: from_file_with_mapped_path (
257+ registry_id,
258+ path,
259+ unversioned_validator,
260+ versioned_validator,
261+ |path| path,
262+ )
256263 }
257264 /// Creates a semantic convention spec with provenance from a file.
258265 ///
@@ -267,7 +274,8 @@ impl SemConvSpecWithProvenance {
267274 pub fn from_file_with_mapped_path < P , F > (
268275 registry_id : & str ,
269276 path : P ,
270- validator : & JsonSchemaValidator ,
277+ unversioned_validator : & JsonSchemaValidator ,
278+ versioned_validator : & JsonSchemaValidator ,
271279 path_fixer : F ,
272280 ) -> WResult < SemConvSpecWithProvenance , Error >
273281 where
@@ -277,7 +285,8 @@ impl SemConvSpecWithProvenance {
277285 fn from_file_or_fatal (
278286 path : & Path ,
279287 provenance : & str ,
280- json_schema_validator : & JsonSchemaValidator ,
288+ unversioned_validator : & JsonSchemaValidator ,
289+ versioned_validator : & JsonSchemaValidator ,
281290 ) -> Result < SemConvSpec , Error > {
282291 use serde_yaml:: Value ;
283292 use std:: io:: Seek ;
@@ -299,7 +308,18 @@ impl SemConvSpecWithProvenance {
299308 let original_error = e. to_string ( ) ;
300309 let value: Result < Value , _ > = serde_yaml:: from_reader ( & mut semconv_file) ;
301310 if let Ok ( yaml_value) = value {
302- json_schema_validator. validate_yaml ( yaml_value, provenance, e) ?;
311+ // TODO - Check if we should use versioned or unversioned validator.
312+ if yaml_value
313+ . as_mapping ( )
314+ . and_then ( |m| m. get ( "version" ) )
315+ . map ( |v| v. is_string ( ) )
316+ . unwrap_or ( false )
317+ {
318+ // Use versioned validator.
319+ versioned_validator. validate_yaml ( yaml_value, provenance, e) ?;
320+ } else {
321+ unversioned_validator. validate_yaml ( yaml_value, provenance, e) ?;
322+ }
303323 }
304324
305325 // Fallback: return original serde error
@@ -312,7 +332,12 @@ impl SemConvSpecWithProvenance {
312332 }
313333 let path = path. as_ref ( ) . display ( ) . to_string ( ) ;
314334 let provenance = Provenance :: new ( registry_id, & path_fixer ( path. clone ( ) ) ) ;
315- let raw_spec = match from_file_or_fatal ( path. as_ref ( ) , & path, validator) {
335+ let raw_spec = match from_file_or_fatal (
336+ path. as_ref ( ) ,
337+ & path,
338+ unversioned_validator,
339+ versioned_validator,
340+ ) {
316341 Ok ( semconv_spec) => {
317342 // Important note: the resolution process expects this step of validation to be done for
318343 // each semantic convention spec.
@@ -397,26 +422,29 @@ mod tests {
397422
398423 #[ test]
399424 fn test_semconv_spec_from_file ( ) {
400- let validator = JsonSchemaValidator :: new ( ) ;
425+ let validator = JsonSchemaValidator :: new_all_versions ( ) ;
401426 // Existing file
402427 let path = PathBuf :: from ( "data/database.yaml" ) ;
403428
404- let semconv_spec = SemConvSpecWithProvenance :: from_file ( "test" , path, & validator)
405- . into_result_failing_non_fatal ( )
406- . unwrap ( ) ;
429+ let semconv_spec =
430+ SemConvSpecWithProvenance :: from_file ( "test" , path, & validator, & validator)
431+ . into_result_failing_non_fatal ( )
432+ . unwrap ( ) ;
407433 assert_eq ! ( semconv_spec. spec. into_v1( "test" ) . groups. len( ) , 10 ) ;
408434
409435 // Non-existing file
410436 let path = PathBuf :: from ( "data/non-existing.yaml" ) ;
411- let semconv_spec = SemConvSpecWithProvenance :: from_file ( "test" , path, & validator)
412- . into_result_failing_non_fatal ( ) ;
437+ let semconv_spec =
438+ SemConvSpecWithProvenance :: from_file ( "test" , path, & validator, & validator)
439+ . into_result_failing_non_fatal ( ) ;
413440 assert ! ( semconv_spec. is_err( ) ) ;
414441 assert ! ( matches!( semconv_spec. unwrap_err( ) , RegistryNotFound { .. } ) ) ;
415442
416443 // Invalid file structure
417444 let path = PathBuf :: from ( "data/invalid/invalid-semconv.yaml" ) ;
418- let semconv_spec = SemConvSpecWithProvenance :: from_file ( "test" , path, & validator)
419- . into_result_failing_non_fatal ( ) ;
445+ let semconv_spec =
446+ SemConvSpecWithProvenance :: from_file ( "test" , path, & validator, & validator)
447+ . into_result_failing_non_fatal ( ) ;
420448 assert ! ( semconv_spec. is_err( ) ) ;
421449 assert ! ( matches!(
422450 semconv_spec. unwrap_err( ) ,
@@ -604,11 +632,12 @@ mod tests {
604632
605633 #[ test]
606634 fn test_semconv_spec_with_provenance_from_file ( ) {
607- let validator = JsonSchemaValidator :: new ( ) ;
635+ let validator = JsonSchemaValidator :: new_all_versions ( ) ;
608636 let path = PathBuf :: from ( "data/database.yaml" ) ;
609- let semconv_spec = SemConvSpecWithProvenance :: from_file ( "main" , & path, & validator)
610- . into_result_failing_non_fatal ( )
611- . unwrap ( ) ;
637+ let semconv_spec =
638+ SemConvSpecWithProvenance :: from_file ( "main" , & path, & validator, & validator)
639+ . into_result_failing_non_fatal ( )
640+ . unwrap ( ) ;
612641 assert_eq ! ( semconv_spec. spec. into_v1( "test" ) . groups. len( ) , 10 ) ;
613642 assert_eq ! ( semconv_spec. provenance. path, path. display( ) . to_string( ) ) ;
614643 }
0 commit comments