mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-07-09 03:14:30 -04:00
Add twig linting
This commit is contained in:
parent
2b8f96d208
commit
5937414ad3
@ -155,7 +155,6 @@ class PreviewController extends BaseController
|
||||
return response()->json(['message' => ctrans('texts.invalid_design_object')], 400);
|
||||
}
|
||||
|
||||
// $entity = ucfirst(request()->input('entity'));
|
||||
$entity = Str::camel(request()->input('entity'));
|
||||
|
||||
$class = "App\Models\\$entity";
|
||||
|
@ -12,6 +12,7 @@
|
||||
namespace App\Http\Requests\Preview;
|
||||
|
||||
use App\Http\Requests\Request;
|
||||
use App\Http\ValidationRules\Design\TwigLint;
|
||||
use App\Utils\Traits\MakesHash;
|
||||
|
||||
class ShowPreviewRequest extends Request
|
||||
@ -31,6 +32,7 @@ class ShowPreviewRequest extends Request
|
||||
public function rules()
|
||||
{
|
||||
$rules = [
|
||||
'design.design.body' => ['sometimes', 'required_if:design.design.is_template,true', new TwigLint()],
|
||||
];
|
||||
|
||||
return $rules;
|
||||
|
44
app/Http/ValidationRules/Design/TwigLint.php
Normal file
44
app/Http/ValidationRules/Design/TwigLint.php
Normal file
@ -0,0 +1,44 @@
|
||||
<?php
|
||||
/**
|
||||
* Invoice Ninja (https://invoiceninja.com).
|
||||
*
|
||||
* @link https://github.com/invoiceninja/invoiceninja source repository
|
||||
*
|
||||
* @copyright Copyright (c) 2024. Invoice Ninja LLC (https://invoiceninja.com)
|
||||
*
|
||||
* @license https://www.elastic.co/licensing/elastic-license
|
||||
*/
|
||||
|
||||
namespace App\Http\ValidationRules\Design;
|
||||
|
||||
use Closure;
|
||||
use App\Services\Template\TemplateService;
|
||||
use Illuminate\Contracts\Validation\ValidationRule;
|
||||
|
||||
class TwigLint implements ValidationRule
|
||||
{
|
||||
/**
|
||||
* Run the validation rule.
|
||||
*
|
||||
* @param \Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail
|
||||
*/
|
||||
public function validate(string $attribute, mixed $value, Closure $fail): void
|
||||
{
|
||||
|
||||
$ts = new TemplateService();
|
||||
$twig = $ts->twig;
|
||||
|
||||
try {
|
||||
$twig->parse($twig->tokenize(new \Twig\Source(preg_replace('/<!--.*?-->/s', '', $value), '')));
|
||||
} catch (\Twig\Error\SyntaxError $e) {
|
||||
// echo json_encode(['status' => 'error', 'message' => $e->getMessage()]);
|
||||
nlog($e->getMessage());
|
||||
$fail($e->getMessage());
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -28,7 +28,7 @@ class ClientTransformer extends BaseTransformer
|
||||
*/
|
||||
public function transform($data)
|
||||
{
|
||||
if (isset($data->name) && $this->getString($data, 'client.name')) {
|
||||
if (isset($data['client.name']) && $this->hasClient($data['client.name'])) {
|
||||
throw new ImportException('Client already exists');
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user