It's a Drupal 7 with Webservices module, and there is a service with something like this (example):

'create-product' => array(
  'operations' => array(
    'create' => array(
      'help' => t('Add Product'),
      'file' => array(
        'type' => 'endpoints.inc',
        'module' => 'custommodule',
        'name' => 'custommodule',
      ),
        'callback' => 'custommodule_create_product',
        'args' => array(
            array(
                'name' => 'appID',
                'type' => 'int',
                'description' => t("App ID"),
                'source' => array('data' => 'appID'),
                'optional' => FALSE
            ),
            ...

Okay, so the type here is "int". Is it possible to make Drupal return some error automatically, if it's not an int that is being sent as appID. Like if it's a string being sent, then it will throw an error. I can't make it work. Why else is there a "type" key-value?

  • What does the service return if you don't send an int? – tyler.frankenstein Dec 6 at 14:28
  • it returns whatever I want it to return... I want a way to trigger an exception, if it's even possible... (this is about INCOMING data) – Raf A. Dec 6 at 14:30
up vote 1 down vote accepted

Although it appears Services doesn't validate the incoming data based on the type, you can use an access callback to build your own validation.

Add something like this to your Service Resource definition:

// ...
'access callback' => '_example_resource_access',
'access callback file' => array(
  'type' => 'inc',
  'module' => 'example',
  'name' => 'example.resource-access',
),
'access arguments' => array('foo bar'),
'access arguments append' => TRUE,
// ...

Then create a file called example.resource-access.inc with a function like this:

function _example_resource_access($op, $args = array()) {    
  if ($op == 'foo bar' && !is_numeric($args[0])) {
    return services_error(t('That is not an integer.'), 406);
  }    
  return TRUE;    
}

Note, $args[0] will likely come in as a string representing an integer, so using is_numeric() should be accurate, unless you specifically use (int) to cast $args[0] beforehand, then you can run it through is_int() for validation instead.

  • Thats what I did – Raf A. Dec 6 at 17:17

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.