Skip to content

Commit 14c34e1

Browse files
committed
allow testing yaml on resource validation
1 parent 704fe4e commit 14c34e1

File tree

3 files changed

+98
-30
lines changed

3 files changed

+98
-30
lines changed

src/Health.php

+94-30
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,7 @@ private function executeValidation(object $validation, ConnectionInterface $to)
369369
// Our purpose here is to set the $pathForSchema and $dataPath variables.
370370
$pathForSchema = null;
371371
$dataPath = null;
372+
$responseType = 'JSON';
372373

373374
// Source data checks validate data directly in the filesystem, not through the API
374375
if ($validation->category === 'sourceDataCheck') {
@@ -591,8 +592,27 @@ private function executeValidation(object $validation, ConnectionInterface $to)
591592
}
592593
}
593594

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+
596616
if (false === $data) {
597617
$message = new \stdClass();
598618
$message->type = "error";
@@ -618,39 +638,83 @@ private function executeValidation(object $validation, ConnectionInterface $to)
618638
$message->classes = ".$validation->validate.file-exists";
619639
$this->sendMessage($to, $message);
620640

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);
628651

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) {
632685
$message = new \stdClass();
633686
$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";
636715
$this->sendMessage($to, $message);
637-
} elseif (gettype($validationResult === 'object')) {
638-
$validationResult->classes = ".$validation->validate.schema-valid";
639-
$this->sendMessage($to, $validationResult);
640716
}
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;
654718
}
655719
}
656720
}

src/Paths/Events.php

+3
Original file line numberDiff line numberDiff line change
@@ -708,6 +708,9 @@ private function produceResponse(): void
708708
} else {
709709
switch (self::$Core->getResponseContentType()) {
710710
case AcceptHeader::YAML:
711+
// We must make sure that any nested stdClass objects are converted to associative arrays
712+
$responseStr = json_encode($responseObj);
713+
$responseObj = json_decode($responseStr, true);
711714
echo yaml_emit($responseObj, YAML_UTF8_ENCODING);
712715
break;
713716
case AcceptHeader::JSON:

src/Paths/Metadata.php

+1
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@ public static function response()
254254
} else {
255255
echo $response;
256256
}
257+
die();
257258
}
258259

259260
/**

0 commit comments

Comments
 (0)