Files
jsonlint/app/Http/Controllers/JsonApiController.php
T

183 lines
5.4 KiB
PHP
Raw Normal View History

2025-04-20 17:54:08 +03:00
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Validator;
class JsonApiController extends Controller
{
/**
* Validate JSON data
*
* @param Request $request
* @return JsonResponse
*/
public function validateJson(Request $request): JsonResponse
{
$validator = Validator::make($request->all(), [
'json' => 'required|json',
]);
if ($validator->fails()) {
return response()->json([
'status' => 'error',
'message' => 'Invalid request',
'errors' => $validator->errors()
], 400);
}
try {
$json = json_decode($request->input('json'), true, 512, JSON_THROW_ON_ERROR);
return response()->json([
'status' => 'success',
'message' => 'Valid JSON',
'data' => $json
]);
} catch (\JsonException $e) {
return response()->json([
'status' => 'error',
'message' => 'Invalid JSON',
'error' => $e->getMessage()
], 400);
}
}
/**
* Validate JSON against a schema
*
* @param Request $request
* @return JsonResponse
*/
public function validateSchema(Request $request): JsonResponse
{
$validator = Validator::make($request->all(), [
'json' => 'required|json',
'schema' => 'required|json',
]);
if ($validator->fails()) {
return response()->json([
'status' => 'error',
'message' => 'Invalid request',
'errors' => $validator->errors()
], 400);
}
try {
$json = json_decode($request->input('json'), true, 512, JSON_THROW_ON_ERROR);
$schema = json_decode($request->input('schema'), true, 512, JSON_THROW_ON_ERROR);
// Use Ajv for schema validation
$ajv = new \Ajv();
$validate = $ajv->compile($schema);
$valid = $validate($json);
if ($valid) {
return response()->json([
'status' => 'success',
'message' => 'JSON is valid according to the schema',
'data' => $json
]);
}
return response()->json([
'status' => 'error',
'message' => 'JSON does not match schema',
'errors' => $validate->errors
], 400);
} catch (\JsonException $e) {
return response()->json([
'status' => 'error',
'message' => 'Invalid JSON or schema',
'error' => $e->getMessage()
], 400);
}
}
/**
* Format JSON
*
* @param Request $request
* @return JsonResponse
*/
public function formatJson(Request $request): JsonResponse
{
$validator = Validator::make($request->all(), [
'json' => 'required|json',
'indent' => 'integer|min:0|max:8',
]);
if ($validator->fails()) {
return response()->json([
'status' => 'error',
'message' => 'Invalid request',
'errors' => $validator->errors()
], 400);
}
try {
$json = json_decode($request->input('json'), true, 512, JSON_THROW_ON_ERROR);
$indent = $request->input('indent', 2);
return response()->json([
'status' => 'success',
'message' => 'JSON formatted successfully',
'data' => json_encode($json, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE, $indent)
]);
} catch (\JsonException $e) {
return response()->json([
'status' => 'error',
'message' => 'Invalid JSON',
'error' => $e->getMessage()
], 400);
}
}
/**
* Compare two JSON documents
*
* @param Request $request
* @return JsonResponse
*/
public function compareJson(Request $request): JsonResponse
{
$validator = Validator::make($request->all(), [
'json1' => 'required|json',
'json2' => 'required|json',
]);
if ($validator->fails()) {
return response()->json([
'status' => 'error',
'message' => 'Invalid request',
'errors' => $validator->errors()
], 400);
}
try {
$json1 = json_decode($request->input('json1'), true, 512, JSON_THROW_ON_ERROR);
$json2 = json_decode($request->input('json2'), true, 512, JSON_THROW_ON_ERROR);
$diff = new \Diff($json1, $json2);
return response()->json([
'status' => 'success',
'message' => 'JSON comparison completed',
'data' => [
'are_equal' => $json1 === $json2,
'differences' => $diff->getDifferences()
]
]);
} catch (\JsonException $e) {
return response()->json([
'status' => 'error',
'message' => 'Invalid JSON',
'error' => $e->getMessage()
], 400);
}
}
}