cloudcreativity/laravel-json-api

Exception during validation when string only contains NULL characters

ben221199 opened this issue · 4 comments

When creating and updating resources I want certain attributes/relationships required during creation and filled during updating. This will ensure a required attribute/relationship during creation and an optional attribute/relationship during updating. It works very well, but there is one problem.

I have a string that contains binary information. When updating my resource without changing the value of this string, my API will return an error because of filled. When looking to the content of the string, the string then only contains NULL chars.

protected function rules($record,array $data): array{
	return [
		'data_raw' => [
			'string',
			($record?'filled':'required'),
		],
	];
}

I think that the filled rule does not check if the length of the string isn't equal to zero. My string does contain \x00\x00 (2 bytes) and filled throws an error.

I'm not sure this is a problem with this package? If the filled rule is failing then that's down to Laravel not this package.

You might need to write a custom rule for this one?

Hmm okay, I will take look at the Laravel package.

Also, is it possible to set have a group of attributes/relationships where you can say that at only one field of that group is required?

There's a whole load of required_* rules that you should check out:
https://laravel.com/docs/8.x/validation#available-validation-rules

If none of those do the job, then you'll need to resort to complex conditional validation:
https://laravel.com/docs/8.x/validation#complex-conditional-validation

Which can be wired in using this package as per these instructions:
https://laravel-json-api.readthedocs.io/en/latest/basics/validators/#conditionally-adding-rules

I'm going to close this issue because it's to do with validation rules, which aren't anything to do with this package as the package uses Laravel validation rules.

Ah thanks