init commit
This commit is contained in:
465
app/Http/Requests/Resume/UpdateResumeRequest.php
Normal file
465
app/Http/Requests/Resume/UpdateResumeRequest.php
Normal file
@@ -0,0 +1,465 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests\Resume;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
|
||||
/**
|
||||
* Update Resume Form Request
|
||||
*
|
||||
* Validates data for updating an existing resume with comprehensive validation rules.
|
||||
* Implements business logic validations and data sanitization for resume updates.
|
||||
*
|
||||
* @author David Valera Melendez <david@valera-melendez.de>
|
||||
* @since February 2025
|
||||
*/
|
||||
class UpdateResumeRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
$resume = $this->route('resume');
|
||||
return Auth::check() && $resume && $resume->user_id === Auth::id();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
$resumeId = $this->route('resume')->id ?? null;
|
||||
|
||||
return [
|
||||
'title' => [
|
||||
'sometimes',
|
||||
'required',
|
||||
'string',
|
||||
'min:3',
|
||||
'max:255',
|
||||
'regex:/^[a-zA-Z0-9\s\-\_\.\,\!\?]+$/',
|
||||
],
|
||||
'description' => [
|
||||
'nullable',
|
||||
'string',
|
||||
'max:1000',
|
||||
],
|
||||
'template' => [
|
||||
'sometimes',
|
||||
'required',
|
||||
'string',
|
||||
'in:professional,modern,executive,minimal,technical',
|
||||
],
|
||||
'status' => [
|
||||
'sometimes',
|
||||
'string',
|
||||
'in:draft,published,archived',
|
||||
],
|
||||
'content' => [
|
||||
'nullable',
|
||||
'array',
|
||||
],
|
||||
'content.personal_info' => [
|
||||
'nullable',
|
||||
'array',
|
||||
],
|
||||
'content.personal_info.full_name' => [
|
||||
'nullable',
|
||||
'string',
|
||||
'max:255',
|
||||
],
|
||||
'content.personal_info.title' => [
|
||||
'nullable',
|
||||
'string',
|
||||
'max:255',
|
||||
],
|
||||
'content.personal_info.email' => [
|
||||
'nullable',
|
||||
'email',
|
||||
'max:255',
|
||||
],
|
||||
'content.personal_info.phone' => [
|
||||
'nullable',
|
||||
'string',
|
||||
'max:20',
|
||||
'regex:/^[\+]?[0-9\s\-\(\)]+$/',
|
||||
],
|
||||
'content.personal_info.location' => [
|
||||
'nullable',
|
||||
'string',
|
||||
'max:255',
|
||||
],
|
||||
'content.personal_info.website' => [
|
||||
'nullable',
|
||||
'url',
|
||||
'max:255',
|
||||
],
|
||||
'content.personal_info.linkedin' => [
|
||||
'nullable',
|
||||
'url',
|
||||
'max:255',
|
||||
'regex:/^https:\/\/(www\.)?linkedin\.com\/in\/[a-zA-Z0-9\-]+\/?$/',
|
||||
],
|
||||
'content.personal_info.github' => [
|
||||
'nullable',
|
||||
'url',
|
||||
'max:255',
|
||||
'regex:/^https:\/\/(www\.)?github\.com\/[a-zA-Z0-9\-]+\/?$/',
|
||||
],
|
||||
'content.summary' => [
|
||||
'nullable',
|
||||
'string',
|
||||
'max:2000',
|
||||
],
|
||||
'content.experience' => [
|
||||
'nullable',
|
||||
'array',
|
||||
'max:20',
|
||||
],
|
||||
'content.experience.*.company' => [
|
||||
'required_with:content.experience',
|
||||
'string',
|
||||
'max:255',
|
||||
],
|
||||
'content.experience.*.position' => [
|
||||
'required_with:content.experience',
|
||||
'string',
|
||||
'max:255',
|
||||
],
|
||||
'content.experience.*.location' => [
|
||||
'nullable',
|
||||
'string',
|
||||
'max:255',
|
||||
],
|
||||
'content.experience.*.start_date' => [
|
||||
'required_with:content.experience',
|
||||
'date',
|
||||
'before_or_equal:today',
|
||||
],
|
||||
'content.experience.*.end_date' => [
|
||||
'nullable',
|
||||
'date',
|
||||
'after:content.experience.*.start_date',
|
||||
'before_or_equal:today',
|
||||
],
|
||||
'content.experience.*.current' => [
|
||||
'boolean',
|
||||
],
|
||||
'content.experience.*.description' => [
|
||||
'nullable',
|
||||
'string',
|
||||
'max:2000',
|
||||
],
|
||||
'content.experience.*.achievements' => [
|
||||
'nullable',
|
||||
'array',
|
||||
'max:10',
|
||||
],
|
||||
'content.experience.*.achievements.*' => [
|
||||
'string',
|
||||
'max:500',
|
||||
],
|
||||
'content.education' => [
|
||||
'nullable',
|
||||
'array',
|
||||
'max:10',
|
||||
],
|
||||
'content.education.*.institution' => [
|
||||
'required_with:content.education',
|
||||
'string',
|
||||
'max:255',
|
||||
],
|
||||
'content.education.*.degree' => [
|
||||
'required_with:content.education',
|
||||
'string',
|
||||
'max:255',
|
||||
],
|
||||
'content.education.*.field' => [
|
||||
'nullable',
|
||||
'string',
|
||||
'max:255',
|
||||
],
|
||||
'content.education.*.start_date' => [
|
||||
'required_with:content.education',
|
||||
'date',
|
||||
'before_or_equal:today',
|
||||
],
|
||||
'content.education.*.end_date' => [
|
||||
'nullable',
|
||||
'date',
|
||||
'after:content.education.*.start_date',
|
||||
'before_or_equal:today',
|
||||
],
|
||||
'content.education.*.gpa' => [
|
||||
'nullable',
|
||||
'numeric',
|
||||
'min:0',
|
||||
'max:4',
|
||||
],
|
||||
'content.education.*.description' => [
|
||||
'nullable',
|
||||
'string',
|
||||
'max:1000',
|
||||
],
|
||||
'content.skills' => [
|
||||
'nullable',
|
||||
'array',
|
||||
],
|
||||
'content.projects' => [
|
||||
'nullable',
|
||||
'array',
|
||||
'max:15',
|
||||
],
|
||||
'content.projects.*.name' => [
|
||||
'required_with:content.projects',
|
||||
'string',
|
||||
'max:255',
|
||||
],
|
||||
'content.projects.*.description' => [
|
||||
'nullable',
|
||||
'string',
|
||||
'max:1000',
|
||||
],
|
||||
'content.projects.*.url' => [
|
||||
'nullable',
|
||||
'url',
|
||||
'max:255',
|
||||
],
|
||||
'content.projects.*.technologies' => [
|
||||
'nullable',
|
||||
'array',
|
||||
'max:20',
|
||||
],
|
||||
'content.certifications' => [
|
||||
'nullable',
|
||||
'array',
|
||||
'max:20',
|
||||
],
|
||||
'content.certifications.*.name' => [
|
||||
'required_with:content.certifications',
|
||||
'string',
|
||||
'max:255',
|
||||
],
|
||||
'content.certifications.*.issuer' => [
|
||||
'required_with:content.certifications',
|
||||
'string',
|
||||
'max:255',
|
||||
],
|
||||
'content.certifications.*.date' => [
|
||||
'required_with:content.certifications',
|
||||
'date',
|
||||
'before_or_equal:today',
|
||||
],
|
||||
'content.certifications.*.url' => [
|
||||
'nullable',
|
||||
'url',
|
||||
'max:255',
|
||||
],
|
||||
'content.languages' => [
|
||||
'nullable',
|
||||
'array',
|
||||
'max:10',
|
||||
],
|
||||
'content.languages.*.language' => [
|
||||
'required_with:content.languages',
|
||||
'string',
|
||||
'max:100',
|
||||
],
|
||||
'content.languages.*.level' => [
|
||||
'required_with:content.languages',
|
||||
'string',
|
||||
'in:Native,Fluent,Advanced,Intermediate,Basic',
|
||||
],
|
||||
'settings' => [
|
||||
'nullable',
|
||||
'array',
|
||||
],
|
||||
'settings.color_scheme' => [
|
||||
'nullable',
|
||||
'string',
|
||||
'in:blue,green,red,purple,orange,gray,black',
|
||||
],
|
||||
'settings.font_family' => [
|
||||
'nullable',
|
||||
'string',
|
||||
'in:Arial,Times New Roman,Calibri,Helvetica,Georgia',
|
||||
],
|
||||
'settings.font_size' => [
|
||||
'nullable',
|
||||
'integer',
|
||||
'min:8',
|
||||
'max:16',
|
||||
],
|
||||
'settings.line_spacing' => [
|
||||
'nullable',
|
||||
'numeric',
|
||||
'min:1.0',
|
||||
'max:2.0',
|
||||
],
|
||||
'settings.margin' => [
|
||||
'nullable',
|
||||
'integer',
|
||||
'min:10',
|
||||
'max:30',
|
||||
],
|
||||
'settings.show_photo' => [
|
||||
'boolean',
|
||||
],
|
||||
'is_public' => [
|
||||
'boolean',
|
||||
],
|
||||
'allow_comments' => [
|
||||
'boolean',
|
||||
],
|
||||
'password' => [
|
||||
'nullable',
|
||||
'string',
|
||||
'min:6',
|
||||
'max:50',
|
||||
],
|
||||
'expires_at' => [
|
||||
'nullable',
|
||||
'date',
|
||||
'after:today',
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get custom error messages for validation rules.
|
||||
*/
|
||||
public function messages(): array
|
||||
{
|
||||
return [
|
||||
'title.required' => 'Resume title is required.',
|
||||
'title.min' => 'Resume title must be at least 3 characters.',
|
||||
'title.regex' => 'Resume title contains invalid characters.',
|
||||
'template.in' => 'Selected template is not available.',
|
||||
'status.in' => 'Invalid resume status.',
|
||||
'content.personal_info.email.email' => 'Please enter a valid email address.',
|
||||
'content.personal_info.phone.regex' => 'Please enter a valid phone number.',
|
||||
'content.personal_info.linkedin.regex' => 'Please enter a valid LinkedIn profile URL.',
|
||||
'content.personal_info.github.regex' => 'Please enter a valid GitHub profile URL.',
|
||||
'content.experience.max' => 'You cannot have more than 20 work experiences.',
|
||||
'content.education.max' => 'You cannot have more than 10 education entries.',
|
||||
'content.projects.max' => 'You cannot have more than 15 projects.',
|
||||
'content.certifications.max' => 'You cannot have more than 20 certifications.',
|
||||
'content.languages.max' => 'You cannot have more than 10 languages.',
|
||||
'content.experience.*.end_date.after' => 'End date must be after start date.',
|
||||
'content.education.*.gpa.max' => 'GPA cannot exceed 4.0.',
|
||||
'content.languages.*.level.in' => 'Invalid language proficiency level.',
|
||||
'settings.font_size.min' => 'Font size must be at least 8.',
|
||||
'settings.font_size.max' => 'Font size cannot exceed 16.',
|
||||
'settings.line_spacing.min' => 'Line spacing must be at least 1.0.',
|
||||
'settings.line_spacing.max' => 'Line spacing cannot exceed 2.0.',
|
||||
'expires_at.after' => 'Expiration date must be in the future.',
|
||||
'password.min' => 'Password must be at least 6 characters.',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare the data for validation.
|
||||
*/
|
||||
protected function prepareForValidation(): void
|
||||
{
|
||||
if ($this->has('title')) {
|
||||
$this->merge(['title' => trim($this->title)]);
|
||||
}
|
||||
|
||||
if ($this->has('description')) {
|
||||
$this->merge(['description' => $this->description ? trim($this->description) : null]);
|
||||
}
|
||||
|
||||
$this->merge([
|
||||
'is_public' => $this->boolean('is_public'),
|
||||
'allow_comments' => $this->boolean('allow_comments'),
|
||||
]);
|
||||
|
||||
|
||||
if ($this->has('content.personal_info')) {
|
||||
$personalInfo = $this->input('content.personal_info', []);
|
||||
|
||||
if (isset($personalInfo['email'])) {
|
||||
$personalInfo['email'] = strtolower(trim($personalInfo['email']));
|
||||
}
|
||||
|
||||
$this->merge(['content' => array_merge($this->input('content', []), [
|
||||
'personal_info' => $personalInfo
|
||||
])]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Configure the validator instance.
|
||||
*/
|
||||
public function withValidator($validator): void
|
||||
{
|
||||
$validator->after(function ($validator) {
|
||||
$resume = $this->route('resume');
|
||||
|
||||
if ($this->has('title')) {
|
||||
$userId = Auth::id();
|
||||
$existingResume = \App\Models\Resume::where('user_id', $userId)
|
||||
->where('title', $this->title)
|
||||
->where('id', '!=', $resume->id)
|
||||
->first();
|
||||
|
||||
if ($existingResume) {
|
||||
$validator->errors()->add(
|
||||
'title',
|
||||
'You already have a resume with this title. Please choose a different title.'
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if ($this->has('content.experience') && is_array($this->content['experience'])) {
|
||||
foreach ($this->content['experience'] as $index => $experience) {
|
||||
if (!empty($experience['start_date']) && !empty($experience['end_date'])) {
|
||||
$startDate = Carbon::parse($experience['start_date']);
|
||||
$endDate = Carbon::parse($experience['end_date']);
|
||||
|
||||
if ($startDate->greaterThan($endDate)) {
|
||||
$validator->errors()->add(
|
||||
"content.experience.{$index}.end_date",
|
||||
'End date must be after start date.'
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ($this->has('status') && $this->status === 'published') {
|
||||
$content = $this->input('content', []);
|
||||
$requiredSections = ['personal_info', 'summary'];
|
||||
|
||||
foreach ($requiredSections as $section) {
|
||||
if (empty($content[$section])) {
|
||||
$validator->errors()->add(
|
||||
'status',
|
||||
"Cannot publish resume: {$section} section is required."
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle a failed validation attempt.
|
||||
*/
|
||||
protected function failedValidation($validator): void
|
||||
{
|
||||
$resume = $this->route('resume');
|
||||
|
||||
logger()->warning('Resume update validation failed', [
|
||||
'user_id' => Auth::id(),
|
||||
'resume_id' => $resume->id ?? null,
|
||||
'title' => $this->input('title'),
|
||||
'errors' => $validator->errors()->toArray(),
|
||||
]);
|
||||
|
||||
parent::failedValidation($validator);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user