How to implement validation for custom plugin fields?

The linked post here (this one) pointed me in the right direction. It seems the validation check itself (which conditionally prevents a user from saving an entry) has to be defined in PHP (even for custom client-side plugins/fields).

The basic idea is to throw an exception if the validation does not pass.

Something like this:

use Kirby\Exception\Exception;

// ...

'fields' => [
   'validations' => [
      'required' => function ($values) {
         $parsed = Json::decode($values);
         if (!$parsed || (!$parsed['parents'] && !$parsed['children'])) {
            throw new Exception('Please select at least one category');
         }
      },
   ],
   // ...
]