Update validation

This commit is contained in:
philipp lang 2024-01-03 00:09:48 +01:00
parent 78143ec0b3
commit 1aeb520b06
2 changed files with 77 additions and 4 deletions

View File

@ -22,8 +22,8 @@ class MediaController
public function store(Request $request)
{
$request->validate([
'name' => 'string',
'model' => ['required', 'string', Rule::in(app('media-library-helpers')->keys())],
'id' => 'required',
]);
$model = $this->validateModel($request);
@ -34,13 +34,13 @@ class MediaController
$request->validate($isSingle ? [
'payload' => 'array',
'payload.*' => '',
'payload.name' => 'required|string|max:255',
'payload.name' => 'required|string|regex:/\..*$/|max:255',
'payload.content' => 'required|string',
] : [
'payload' => 'required|array|min:1',
'payload.*' => 'array',
'payload.*.name' => 'string',
'payload.*.content' => 'string',
'payload.*.name' => 'required|string|regex:/\..*$/|max:255',
'payload.*.content' => 'required|string',
]);
$content = $isSingle ? [$request->input('payload')] : $request->input('payload');

View File

@ -35,6 +35,79 @@ test('it uploads a single file to a single file collection', function () {
$response->assertJsonMissingPath('model_id');
});
test('test validation', function (array $attributes, string $messages) {
$this->auth()->registerModel();
$post = $this->newPost();
$content = base64_encode($this->pdfFile()->getContent());
$this->postJson('/mediaupload', [
'model' => 'post',
'id' => $post->id,
'collection' => 'defaultSingleFile',
'payload' => [
'content' => $content,
'name' => 'beispiel bild.jpg',
],
...$attributes
])->assertJsonValidationErrors($messages);
})->with([
'missing collection' => [
['collection' => ''],
'collection'
],
'missing id' => [
['id' => ''],
'id'
],
]);
test('test validation for payload', function () {
$this->auth()->registerModel();
$post = $this->newPost();
$this->postJson('/mediaupload', [
'model' => 'post',
'id' => $post->id,
'collection' => 'defaultSingleFile',
'payload' => [
'content' => '',
'name' => 'beispiel bild.jpg',
],
])->assertJsonValidationErrors('payload.content');
});
test('test validation for name', function () {
$this->auth()->registerModel();
$post = $this->newPost();
$content = base64_encode($this->pdfFile()->getContent());
$this->postJson('/mediaupload', [
'model' => 'post',
'id' => $post->id,
'collection' => 'defaultSingleFile',
'payload' => [
'content' => $content,
'name' => '',
],
])->assertJsonValidationErrors('payload.name');
});
test('test validation for extension', function () {
$this->auth()->registerModel();
$post = $this->newPost();
$content = base64_encode($this->pdfFile()->getContent());
$this->postJson('/mediaupload', [
'model' => 'post',
'id' => $post->id,
'collection' => 'defaultSingleFile',
'payload' => [
'content' => $content,
'name' => 'aaa',
],
])->assertJsonValidationErrors('payload.name');
});
test('it uploads a single image to a single file collection', function () {
$this->auth()->registerModel();
$post = $this->newPost();