Skip to content

Commit 4efb48c

Browse files
authored
[5.x] Ensure asset validation rules are returned as strings to GraphQL (#11781)
1 parent ac1ab0c commit 4efb48c

7 files changed

Lines changed: 89 additions & 1 deletion

File tree

src/Fieldtypes/Assets/DimensionsRule.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,4 +124,9 @@ protected function failsRatioCheck($parameters, $width, $height)
124124

125125
return abs($numerator / $denominator - $width / $height) > $precision;
126126
}
127+
128+
public function __toString()
129+
{
130+
return 'dimensions:'.implode(',', $this->parameters);
131+
}
127132
}

src/Fieldtypes/Assets/ImageRule.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,9 @@ public function message()
4949
{
5050
return __((Statamic::isCpRoute() ? 'statamic::' : '').'validation.image');
5151
}
52+
53+
public function __toString()
54+
{
55+
return 'image:'.implode(',', $this->parameters);
56+
}
5257
}

src/Fieldtypes/Assets/MimesRule.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,9 @@ public function message()
5151
{
5252
return str_replace(':values', implode(', ', $this->parameters), __((Statamic::isCpRoute() ? 'statamic::' : '').'validation.mimes'));
5353
}
54+
55+
public function __toString()
56+
{
57+
return 'mimes:'.implode(',', $this->parameters);
58+
}
5459
}

src/Fieldtypes/Assets/MimetypesRule.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,9 @@ public function message()
4646
{
4747
return str_replace(':values', implode(', ', $this->parameters), __((Statamic::isCpRoute() ? 'statamic::' : '').'validation.mimetypes'));
4848
}
49+
50+
public function __toString()
51+
{
52+
return 'mimetypes:'.implode(',', $this->parameters);
53+
}
4954
}

src/Fieldtypes/Assets/SizeBasedRule.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,9 @@ protected function getFileSize($id)
6666

6767
return false;
6868
}
69+
70+
public function __toString()
71+
{
72+
return 'size:'.implode(',', $this->parameters);
73+
}
6974
}

src/GraphQL/Types/FormType.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,21 @@ public function fields(): array
3535
'rules' => [
3636
'type' => GraphQL::type(ArrayType::NAME),
3737
'resolve' => function ($form, $args, $context, $info) {
38-
return $form->blueprint()->fields()->validator()->rules();
38+
return collect($form->blueprint()->fields()->validator()->rules())
39+
->map(function ($rules) {
40+
return collect($rules)->map(function ($rule) {
41+
if (is_string($rule)) {
42+
return $rule;
43+
}
44+
45+
if ($rule instanceof \Stringable) {
46+
return (string) $rule;
47+
}
48+
49+
return $rule;
50+
});
51+
})
52+
->all();
3953
},
4054
],
4155
'sections' => [

tests/Feature/GraphQL/FormTest.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,4 +317,53 @@ public function it_queries_the_sections()
317317
],
318318
]]);
319319
}
320+
321+
#[Test]
322+
public function it_returns_string_based_validation_rules_for_mimes_mimetypes_dimension_size_and_image()
323+
{
324+
Form::make('contact')->title('Contact Us')->save();
325+
326+
$blueprint = Blueprint::makeFromFields([
327+
'name' => [
328+
'type' => 'assets',
329+
'display' => 'Asset',
330+
'validate' => [
331+
'mimes:image/jpeg,image/png',
332+
'mimetypes:image/jpeg,image/png',
333+
'dimensions:1024',
334+
'size:1000',
335+
'image:jpeg',
336+
],
337+
],
338+
]);
339+
340+
BlueprintRepository::shouldReceive('find')->with('forms.contact')->andReturn($blueprint);
341+
342+
$query = <<<'GQL'
343+
{
344+
form(handle: "contact") {
345+
rules
346+
}
347+
}
348+
GQL;
349+
$this
350+
->withoutExceptionHandling()
351+
->post('/graphql', ['query' => $query])
352+
->assertGqlOk()
353+
->assertExactJson(['data' => [
354+
'form' => [
355+
'rules' => [
356+
'name' => [
357+
'mimes:image/jpeg,image/png',
358+
'mimetypes:image/jpeg,image/png',
359+
'dimensions:1024',
360+
'size:1000',
361+
'image:jpeg',
362+
'array',
363+
'nullable',
364+
],
365+
],
366+
],
367+
]]);
368+
}
320369
}

0 commit comments

Comments
 (0)