@@ -369,6 +369,7 @@ private function executeValidation(object $validation, ConnectionInterface $to)
369
369
// Our purpose here is to set the $pathForSchema and $dataPath variables.
370
370
$ pathForSchema = null ;
371
371
$ dataPath = null ;
372
+ $ responseType = 'JSON ' ;
372
373
373
374
// Source data checks validate data directly in the filesystem, not through the API
374
375
if ($ validation ->category === 'sourceDataCheck ' ) {
@@ -591,8 +592,27 @@ private function executeValidation(object $validation, ConnectionInterface $to)
591
592
}
592
593
}
593
594
594
- // $dataPath could be either a source file or an API path, file_get_contents can handle both
595
- $ data = file_get_contents ($ dataPath );
595
+ $ data = false ;
596
+ if (property_exists ($ validation , 'responsetype ' )) {
597
+ $ responseType = $ validation ->responsetype ;
598
+ //get the index of the responsetype from the ReturnType class
599
+ $ responseTypeIdx = array_search ($ responseType , ReturnType::$ values );
600
+ //get the corresponding accept mime type
601
+ $ acceptMimeType = AcceptHeader::$ values [$ responseTypeIdx ];
602
+ $ opts = [
603
+ "http " => [
604
+ "method " => "GET " ,
605
+ "header " => "Accept: $ acceptMimeType \r\n"
606
+ ]
607
+ ];
608
+ $ context = stream_context_create ($ opts );
609
+ // $dataPath is probably an API path in this case
610
+ $ data = file_get_contents ($ dataPath , false , $ context );
611
+ } else {
612
+ // $dataPath is probably a source file in the filesystem in this case
613
+ $ data = file_get_contents ($ dataPath );
614
+ }
615
+
596
616
if (false === $ data ) {
597
617
$ message = new \stdClass ();
598
618
$ message ->type = "error " ;
@@ -618,39 +638,83 @@ private function executeValidation(object $validation, ConnectionInterface $to)
618
638
$ message ->classes = ". $ validation ->validate .file-exists " ;
619
639
$ this ->sendMessage ($ to , $ message );
620
640
621
- $ jsonData = json_decode ($ data );
622
- if (json_last_error () === JSON_ERROR_NONE ) {
623
- $ message = new \stdClass ();
624
- $ message ->type = "success " ;
625
- $ message ->text = "The Data file $ dataPath was successfully decoded as JSON " ;
626
- $ message ->classes = ". $ validation ->validate .json-valid " ;
627
- $ this ->sendMessage ($ to , $ message );
641
+ switch ($ responseType ) {
642
+ case 'YML ' :
643
+ try {
644
+ $ yamlData = json_decode (json_encode (yaml_parse ($ data )));
645
+ if ($ yamlData ) {
646
+ $ message = new \stdClass ();
647
+ $ message ->type = "success " ;
648
+ $ message ->text = "The Data file $ dataPath was successfully decoded as YAML " ;
649
+ $ message ->classes = ". $ validation ->validate .json-valid " ;
650
+ $ this ->sendMessage ($ to , $ message );
628
651
629
- if (null !== $ schema ) {
630
- $ validationResult = $ this ->validateDataAgainstSchema ($ jsonData , $ schema );
631
- if (gettype ($ validationResult ) === 'boolean ' && $ validationResult === true ) {
652
+ if (null !== $ schema ) {
653
+ $ validationResult = $ this ->validateDataAgainstSchema ($ yamlData , $ schema );
654
+ if (gettype ($ validationResult ) === 'boolean ' && $ validationResult === true ) {
655
+ $ message = new \stdClass ();
656
+ $ message ->type = "success " ;
657
+ $ message ->text = "The Data file $ dataPath was successfully validated against the Schema $ schema " ;
658
+ $ message ->classes = ". $ validation ->validate .schema-valid " ;
659
+ $ this ->sendMessage ($ to , $ message );
660
+ } elseif (gettype ($ validationResult === 'object ' )) {
661
+ $ validationResult ->classes = ". $ validation ->validate .schema-valid " ;
662
+ $ this ->sendMessage ($ to , $ validationResult );
663
+ }
664
+ } else {
665
+ $ message = new \stdClass ();
666
+ $ message ->type = "error " ;
667
+ $ message ->text = "Unable to detect schema for dataPath {$ dataPath } and category {$ validation ->category }" ;
668
+ $ message ->classes = ". $ validation ->validate .schema-valid " ;
669
+ $ this ->sendMessage ($ to , $ message );
670
+ }
671
+ }
672
+ } catch (\Exception $ ex ) {
673
+ $ message = new \stdClass ();
674
+ $ message ->type = "error " ;
675
+ $ message ->text = "There was an error decoding the Data file $ dataPath as YAML: " . $ ex ->getMessage () . " :: Raw data = <<<JSON \n$ data \n>>> " ;
676
+ $ message ->classes = ". $ validation ->validate .json-valid " ;
677
+ $ this ->sendMessage ($ to , $ message );
678
+ }
679
+ break ;
680
+ case 'JSON ' :
681
+ // no break
682
+ default :
683
+ $ jsonData = json_decode ($ data );
684
+ if (json_last_error () === JSON_ERROR_NONE ) {
632
685
$ message = new \stdClass ();
633
686
$ message ->type = "success " ;
634
- $ message ->text = "The Data file $ dataPath was successfully validated against the Schema $ schema " ;
635
- $ message ->classes = ". $ validation ->validate .schema-valid " ;
687
+ $ message ->text = "The Data file $ dataPath was successfully decoded as JSON " ;
688
+ $ message ->classes = ". $ validation ->validate .json-valid " ;
689
+ $ this ->sendMessage ($ to , $ message );
690
+
691
+ if (null !== $ schema ) {
692
+ $ validationResult = $ this ->validateDataAgainstSchema ($ jsonData , $ schema );
693
+ if (gettype ($ validationResult ) === 'boolean ' && $ validationResult === true ) {
694
+ $ message = new \stdClass ();
695
+ $ message ->type = "success " ;
696
+ $ message ->text = "The Data file $ dataPath was successfully validated against the Schema $ schema " ;
697
+ $ message ->classes = ". $ validation ->validate .schema-valid " ;
698
+ $ this ->sendMessage ($ to , $ message );
699
+ } elseif (gettype ($ validationResult === 'object ' )) {
700
+ $ validationResult ->classes = ". $ validation ->validate .schema-valid " ;
701
+ $ this ->sendMessage ($ to , $ validationResult );
702
+ }
703
+ } else {
704
+ $ message = new \stdClass ();
705
+ $ message ->type = "error " ;
706
+ $ message ->text = "Unable to detect schema for dataPath {$ dataPath } and category {$ validation ->category }" ;
707
+ $ message ->classes = ". $ validation ->validate .schema-valid " ;
708
+ $ this ->sendMessage ($ to , $ message );
709
+ }
710
+ } else {
711
+ $ message = new \stdClass ();
712
+ $ message ->type = "error " ;
713
+ $ message ->text = "There was an error decoding the Data file $ dataPath as JSON: " . json_last_error_msg () . " :: Raw data = <<<JSON \n$ data \n>>> " ;
714
+ $ message ->classes = ". $ validation ->validate .json-valid " ;
636
715
$ this ->sendMessage ($ to , $ message );
637
- } elseif (gettype ($ validationResult === 'object ' )) {
638
- $ validationResult ->classes = ". $ validation ->validate .schema-valid " ;
639
- $ this ->sendMessage ($ to , $ validationResult );
640
716
}
641
- } else {
642
- $ message = new \stdClass ();
643
- $ message ->type = "error " ;
644
- $ message ->text = "Unable to detect schema for dataPath {$ dataPath } and category {$ validation ->category }" ;
645
- $ message ->classes = ". $ validation ->validate .schema-valid " ;
646
- $ this ->sendMessage ($ to , $ message );
647
- }
648
- } else {
649
- $ message = new \stdClass ();
650
- $ message ->type = "error " ;
651
- $ message ->text = "There was an error decoding the Data file $ dataPath as JSON: " . json_last_error_msg () . " :: Raw data = <<<JSON \n$ data \n>>> " ;
652
- $ message ->classes = ". $ validation ->validate .json-valid " ;
653
- $ this ->sendMessage ($ to , $ message );
717
+ break ;
654
718
}
655
719
}
656
720
}
0 commit comments