Register rest field for specific user

I found a better workaround using the same filter suggested by @ben-casey in his answer.

Firstly I registered throuhg register_rest_field all the custom fields of all the user types I have in the database.

Then in rest_prepare_user, insted adding the wanted fields, I removed the unwanted ones. In this way I can still use the same endpoint to update the field.

Below an example of the JSON clean up.

function my_rest_prepare_user( WP_REST_Response $response, WP_User $user, WP_REST_Request $request ){

    if( in_array( 'administrator', $user->roles ) ){

        $data = $response->get_data();

        unset($data['custom_field']);

        $response->set_data( $data );

    }

    return $response;

}
add_filter( 'rest_prepare_user', 'my_rest_prepare_user', 10, 3 );