Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrated Rules to ValidationRules #966

Merged
merged 4 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions app/Http/Requests/StoreEntityAsset.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use App\Facades\Limit;
use App\Models\EntityAsset;
use App\Rules\EntityFileRule;
use App\Rules\EntityFile;
use App\Rules\FontAwesomeIcon;
use App\Traits\ApiRequest;
use Illuminate\Foundation\Http\FormRequest;
Expand Down Expand Up @@ -37,7 +37,7 @@ public function rules()
'required_if:type_id,' . EntityAsset::TYPE_FILE,
'file',
'max:' . Limit::upload(),
new EntityFileRule()
new EntityFile()
],
'metadata.url' => 'required_if:type_id,' . EntityAsset::TYPE_LINK . '|string|url',
'metadata.icon' => ['max:45', new FontAwesomeIcon()],
Expand Down
4 changes: 2 additions & 2 deletions app/Http/Requests/StoreEntityAssets.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use App\Facades\Limit;
use App\Models\EntityAsset;
use App\Rules\EntityFileRule;
use App\Rules\EntityFile;
use App\Rules\FontAwesomeIcon;
use App\Traits\ApiRequest;
use Illuminate\Foundation\Http\FormRequest;
Expand Down Expand Up @@ -37,7 +37,7 @@ public function rules()
'required_if:type_id,' . EntityAsset::TYPE_FILE,
'file',
'max:' . Limit::upload(),
new EntityFileRule()
new EntityFile()
],
'metadata.url' => 'required_if:type_id,' . EntityAsset::TYPE_LINK . '|string|url',
'metadata.icon' => ['max:45', new FontAwesomeIcon()],
Expand Down
4 changes: 2 additions & 2 deletions app/Http/Requests/StoreEntityFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace App\Http\Requests;

use App\Facades\Limit;
use App\Rules\EntityFileRule;
use App\Rules\EntityFile;
use Illuminate\Foundation\Http\FormRequest;

class StoreEntityFile extends FormRequest
Expand All @@ -30,7 +30,7 @@ public function rules()
'required',
'file',
'max:' . Limit::upload(),
new EntityFileRule()
new EntityFile()
],
'name' => 'nullable|string|max:45',
'visibility_id' => 'nullable|exists:visibilities,id'
Expand Down
26 changes: 9 additions & 17 deletions app/Rules/AccountEmail.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,21 @@

namespace App\Rules;

use Illuminate\Contracts\Validation\Rule;
use Closure;
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Support\Str;

class AccountEmail implements Rule
class AccountEmail implements ValidationRule
{
/**
* Determine if the validation rule passes.
* Run the validation rule.
*
* @param string $attribute
* @return bool
* @param \Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail
*/
public function passes($attribute, $value)
public function validate(string $attribute, mixed $value, Closure $fail): void
{
return !Str::contains($value, ['@boxmail.lol', '@fireboxmail.lol']);
}

/**
* Get the validation error message.
*
* @return string
*/
public function message()
{
return 'The validation error message.';
if (Str::contains($value, ['@boxmail.lol', '@fireboxmail.lol'])) {
$fail('The validation error message.');
}
}
}
35 changes: 9 additions & 26 deletions app/Rules/AccountName.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,21 @@

namespace App\Rules;

use Illuminate\Contracts\Validation\Rule;
use Closure;
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Support\Str;

class AccountName implements Rule
class AccountName implements ValidationRule
{
/**
* Create a new rule instance.
* Run the validation rule.
*
* @return void
* @param \Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail
*/
public function __construct()
public function validate(string $attribute, mixed $value, Closure $fail): void
{
}

/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @return bool
*/
public function passes($attribute, $value)
{
return !Str::contains($value, ['<', '>', 'https', 'http://', 'www.', 'Ђ', ' Illuro']) && Str::length($value) < 31;
}

/**
* Get the validation error message.
*
* @return string
*/
public function message()
{
return 'Invalid account name.';
if (Str::contains($value, ['<', '>', 'https', 'http://', 'www.', 'Ђ', ' Illuro']) && Str::length($value) < 31) {
$fail('Invalid account name.');
}
}
}
35 changes: 9 additions & 26 deletions app/Rules/CalendarFormat.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,20 @@

namespace App\Rules;

use Illuminate\Contracts\Validation\Rule;
use Closure;
use Illuminate\Contracts\Validation\ValidationRule;

class CalendarFormat implements Rule
class CalendarFormat implements ValidationRule
{
/**
* Create a new rule instance.
* Run the validation rule.
*
* @return void
* @param \Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail
*/
public function __construct()
public function validate(string $attribute, mixed $value, Closure $fail): void
{
}

/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @return bool
*/
public function passes($attribute, $value)
{
return preg_match('/^[ymMds\s,-]+$/', $value);
}

/**
* Get the validation error message.
*
* @return string
*/
public function message()
{
return __('calendars.validators.format');
if (preg_match('/^[ymMds\s,-]+$/', $value)) {
$fail(__('calendars.validators.format'));
}
}
}
36 changes: 8 additions & 28 deletions app/Rules/CalendarMoonOffset.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,50 +2,30 @@

namespace App\Rules;

use Illuminate\Contracts\Validation\Rule;
use Closure;
use Illuminate\Contracts\Validation\ValidationRule;

class CalendarMoonOffset implements Rule
class CalendarMoonOffset implements ValidationRule
{
/**
* Create a new rule instance.
* Run the validation rule.
*
* @return void
* @param \Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail
*/
public function __construct()
{
}

/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @return bool
*/
public function passes($attribute, $value)
public function validate(string $attribute, mixed $value, Closure $fail): void
{
// Max value
$lengths = request()->get('month_length');
if (!is_array($lengths) || count($lengths) === 0) {
return false;
$fail(__('calendars.validators.moon_offset'));
}
$max = $lengths[0];
$min = 0 - $max;

foreach ($value as $offset) {
if ($offset > $max || $offset < $min) {
return false;
$fail(__('calendars.validators.moon_offset'));
}
}
return true;
}

/**
* Get the validation error message.
*
* @return string
*/
public function message()
{
return __('calendars.validators.moon_offset');
}
}
36 changes: 9 additions & 27 deletions app/Rules/CampaignDelete.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,21 @@

namespace App\Rules;

use Illuminate\Contracts\Validation\Rule;
use Closure;
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Support\Str;

class CampaignDelete implements Rule
class CampaignDelete implements ValidationRule
{
/**
* Create a new rule instance.
* Run the validation rule.
*
* @return void
* @param \Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail
*/
public function __construct()
public function validate(string $attribute, mixed $value, Closure $fail): void
{
}

/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param string $value
* @return bool
*/
public function passes($attribute, $value)
{
return Str::is(mb_strtolower($value), 'delete');
}

/**
* Get the validation error message.
*
* @return string
*/
public function message()
{
return (__('validation.delete_campaign', ['code' => 'delete']));
if (!Str::is(mb_strtolower($value), 'delete')) {
$fail(__('validation.delete_campaign', ['code' => 'delete']));
}
}
}
51 changes: 15 additions & 36 deletions app/Rules/EntityFileRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,67 +2,46 @@

namespace App\Rules;

use Illuminate\Contracts\Validation\Rule;
use Closure;
use Illuminate\Contracts\Validation\ValidationRule;
use Symfony\Component\HttpFoundation\File\UploadedFile;

class EntityFileRule implements Rule
class EntityFile implements ValidationRule
{
/**
* Create a new rule instance.
* Run the validation rule.
*
* @return void
* @param \Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail
*/
public function __construct()
{
}

/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @return bool
*/
public function passes($attribute, $value)
public function validate(string $attribute, mixed $value, Closure $fail): void
{
// Not a valid file, don't go further
if ($value instanceof UploadedFile && !$value->isValid()) {
return false;
$fail(__('validation.mimes', ['values' => 'jpg, jpeg, png, gif, webp, pdf, xls(x), csv, mp3, ogg, json']));
}

// Block any hacking shenanigans
if ($this->shouldBlockPhpUpload($value, [])) {
return false;
$fail(__('validation.mimes', ['values' => 'jpg, jpeg, png, gif, webp, pdf, xls(x), csv, mp3, ogg, json']));
}

if (empty($value->getPath())) {
return false;
$fail(__('validation.mimes', ['values' => 'jpg, jpeg, png, gif, webp, pdf, xls(x), csv, mp3, ogg, json']));
}

$validExtensions = explode(',', 'jpeg,png,jpg,gif,webp,pdf,xls,xlsx,mp3');
if (in_array($value->guessExtension(), $validExtensions)) {
return true;
if (!in_array($value->guessExtension(), $validExtensions)) {
$fail(__('validation.mimes', ['values' => 'jpg, jpeg, png, gif, webp, pdf, xls(x), csv, mp3, ogg, json']));
}

// It wasn't an image, maybe it's an audio file
if (empty($value->getClientOriginalExtension())) {
return false;
$fail(__('validation.mimes', ['values' => 'jpg, jpeg, png, gif, webp, pdf, xls(x), csv, mp3, ogg, json']));
}

return !(!in_array($value->getClientOriginalExtension(), ['mp3', 'ogg', 'json', 'csv']))



;
}

/**
* Get the validation error message.
*
* @return string
*/
public function message()
{
return __('validation.mimes', ['values' => 'jpg, jpeg, png, gif, webp, pdf, xls(x), csv, mp3, ogg, json']);
if (in_array($value->getClientOriginalExtension(), ['mp3', 'ogg', 'json', 'csv'])) {
$fail(__('validation.mimes', ['values' => 'jpg, jpeg, png, gif, webp, pdf, xls(x), csv, mp3, ogg, json']));
}
}

protected function shouldBlockPhpUpload($value, $parameters)
Expand Down
Loading
Loading