22
33namespace App \Services ;
44
5- use JsonSchema \Validator ;
6- use JsonSchema \Constraints \Constraint ;
7- use App \Services \HttpService ;
85use Illuminate \Support \Facades \Cache ;
6+ use JsonSchema \Constraints \Constraint ;
7+ use JsonSchema \Validator ;
98
109abstract class SchemaValidatorService
1110{
1211 public function __construct (
13- protected string $ schema_url ,
14- protected string $ cache_key ,
12+ protected string $ schema_url ,
13+ protected string $ cache_key ,
1514 protected int $ cache_ttl
1615 ) {}
1716
1817 /**
1918 * Validate the blueprint config against the JSON schema
2019 *
21- * @param array $config The config array to validate
20+ * @param array $config The config array to validate
2221 * @return array Returns ['valid' => bool, 'errors' => array]
2322 */
2423 public function validate (array $ config ): array
2524 {
2625 $ schema = $ this ->getSchema ();
2726 $ configObject = json_decode (json_encode ($ config ));
28- $ validator = new Validator () ;
27+ $ validator = new Validator ;
2928 $ validator ->validate (
3029 $ configObject ,
3130 $ schema ,
@@ -35,7 +34,7 @@ public function validate(array $config): array
3534 if ($ validator ->isValid ()) {
3635 return [
3736 'valid ' => true ,
38- 'errors ' => []
37+ 'errors ' => [],
3938 ];
4039 }
4140 $ errors = [];
@@ -46,17 +45,17 @@ public function validate(array $config): array
4645 'constraint ' => $ error ['constraint ' ] ?? null ,
4746 ];
4847 }
48+
4949 return [
5050 'valid ' => false ,
51- 'errors ' => $ errors
51+ 'errors ' => $ errors,
5252 ];
5353 }
5454
5555 /**
5656 * Get validation errors as a formatted string
5757 *
58- * @param array $errors The errors array from validate()
59- * @return string
58+ * @param array $errors The errors array from validate()
6059 */
6160 public function formatErrors (array $ errors ): string
6261 {
@@ -72,17 +71,16 @@ public function formatErrors(array $errors): string
7271 /**
7372 * Fetch and cache the JSON schema
7473 *
75- * @return object
7674 * @throws \RuntimeException
7775 */
7876 private function getSchema (): object
7977 {
80- $ schema = Cache::remember ($ this ->cache_key , $ this ->cache_ttl , function () {
78+ $ schema = Cache::remember ($ this ->cache_key , $ this ->cache_ttl , function () {
8179 try {
8280 return HttpService::makeRequest ('get ' , $ this ->schema_url );
8381 } catch (\Exception $ e ) {
8482 throw new \RuntimeException (
85- " Unable to fetch schema from " . $ this ->schema_url . " : " . $ e ->getMessage ()
83+ ' Unable to fetch schema from ' . $ this ->schema_url . ' : ' . $ e ->getMessage ()
8684 );
8785 }
8886 });
@@ -97,4 +95,4 @@ public function clearCache(): void
9795 {
9896 Cache::forget ($ this ->schema_url );
9997 }
100- }
98+ }
0 commit comments