@@ -169,14 +169,30 @@ public function render($request, Throwable $exception)
169169 */
170170 public function invalidJson ($ request , ValidationException $ exception )
171171 {
172- $ errors = collect ($ exception ->errors ())->map (function ($ errors , $ field ) use ($ exception ) {
172+ $ codes = collect ($ exception ->validator ->failed ())->mapWithKeys (function ($ reasons , $ field ) {
173+ $ cleaned = [];
174+ foreach ($ reasons as $ reason => $ attrs ) {
175+ $ cleaned [] = snake_case ($ reason );
176+ }
177+
178+ return [str_replace ('. ' , '_ ' , $ field ) => $ cleaned ];
179+ })->toArray ();
180+
181+ $ errors = collect ($ exception ->errors ())->map (function ($ errors , $ field ) use ($ codes , $ exception ) {
173182 $ response = [];
174183 foreach ($ errors as $ key => $ error ) {
175- $ converted = self ::convertToArray ($ exception )['errors ' ][0 ];
176- $ response [] = array_merge ($ converted , [
177- 'detail ' => $ error ,
184+ $ meta = [
178185 'source_field ' => $ field ,
179- ]);
186+ 'rule ' => str_replace (self ::PTERODACTYL_RULE_STRING , 'p_ ' , array_get (
187+ $ codes , str_replace ('. ' , '_ ' , $ field ) . '. ' . $ key
188+ )),
189+ ];
190+
191+ $ converted = self ::convertToArray ($ exception )['errors ' ][0 ];
192+ $ converted ['detail ' ] = $ error ;
193+ $ converted ['meta ' ] = is_array ($ converted ['meta ' ]) ? array_merge ($ converted ['meta ' ], $ meta ) : $ meta ;
194+
195+ $ response [] = $ converted ;
180196 }
181197
182198 return $ response ;
0 commit comments