Search code examples
jsonlaravelpostman

send json to laravel api route with json validate


i sending post data to laravel route like this photo enter image description here

my validate request is this:

return [
            'mobile' => ['required', 'string'],
            'withdraws' => 'required|json',
        ];

but i got this error: "The withdraws field must be a valid JSON string."


Solution

  • As the error message says (and the official documentation) the "json" validation rule asserts that the field contains a valid JSON string not an object

    for your case, you can use dot notation to validate the nested fields

    otherwise, if you want to just validate that it contains an object no matter what fields it contains, you'll need to create your own custom validator for that, as Laravel AFAIK doesn't have a rule for that.