验证数据,如何将null类型计为字符串类型

时间:2015-07-31 22:52:37

标签: php string null

我正在尝试在将数据插入数据库之前验证用户的数据。我有一个字段列表数组,其中包含输入数据必须具有的各种字段类型。

示例:

$fields = ['id' => 'integer', 'contents' => 'string', 'display' => 'boolean'];

因此,如果用户想要发出POST请求,则用户需要首先提供所有必需的字段,然后这些字段的类型必须正确。

示例:

$data = ['id' => 123, 'contents' => 'hello', 'display' => true];

在字段列表中,我将某些类型值设置为'string'。问题是我希望所有'string'值都包含用户可能提供的null值类型。

Here's my gist of the function and some tests

<?php

function verifyData (array $fields, array $data, array $excludeFields = []) {
  $array = [
    'data'  => [],
    'debug' => []
  ];

  foreach ($fields as $key => $value) {
    // If key is in exclude: ignore field
    if (!empty($excludeFields) && in_array($key, $excludeFields)) {
      continue;
    }

    $type = gettype($data[$key]);

    // How can I make null count as a string?
    // If data type is null, and it's field value is a string it should NOT get added to $array['data']
    if ($type !== $value || ($value === 'string' && is_null($data[$key]))) {
      $array['data'][] = [
        'field'   => $key,
        'message' => "Type of '$key' field is incorrect. Current type is: '$type', it should be: '$value'"
      ];
    } else {
      $array['debug'][] = "$key, $type, $value";
    }
  }

  print_r($array);
  echo '<hr>';

  // return $array;
}



// -----------------------------------------------------------------------------
// Tests
// -----------------------------------------------------------------------------
echo '<pre>';

// -----------------------------------------------------------------------------

$fields  = ['id' => 'integer', 'contents' => 'string', 'display' => 'boolean'];
$data    = ['id' => 123,       'contents' => 'hello',  'display' => true];
$exclude = [];

echo 'Output OK <br>';
verifyData($fields, $data, $exclude);

// -----------------------------------------------------------------------------

$fields  = ['id' => 'integer', 'contents' => 'string', 'display' => 'boolean'];
$data    = ['id' => 123,       'contents' => 'hi',     'display' => true];
$exclude = ['id'];

echo 'Output OK - Field "id" is excluded from debug output <br>';
verifyData($fields, $data, $exclude);

// -----------------------------------------------------------------------------

$fields  = ['id' => 'integer', 'contents' => 'string', 'display' => 'boolean'];
$data    = ['id' => 123,       'contents' => 123,      'display' => true];
$exclude = [];

echo 'Output OK - Field "contents" should not be an integer <br>';
verifyData($fields, $data, $exclude);

// -----------------------------------------------------------------------------

$fields  = ['id' => 'integer', 'contents' => 'string', 'display' => 'boolean'];
$data    = ['id' => 123,       'contents' => null,     'display' => true];
$exclude = [];

echo 'Output failed - Field "contents" should be in the debug output (null should be counted as a string) <br>';
verifyData($fields, $data, $exclude);

我希望我已经明确了解问题(英语不是我的主要语言)。

(可选阅读)我现在的整个工作流程:

我正在使用Slim Framework来处理请求和响应。

用户使用JSON正文执行POST请求(标题'Content-Type'设置为'application/json;charset=utf-8'):

{"contents": "Some text", "display": true}

我处理正文数据并使用json_decode($body, true)将其转换为php数组。

我使用该数组验证它的数据类型,方法是将它与我​​之前提供的$fields示例进行比较,以检查正文数据的类型是否正确。

如果它们中的任何一个类型不正确,我会抛出一个异常,并且用户会得到一个包含所有错误的响应(见下文)。

如果用户发布了例如:

{"contents": null, "display": true}

目前,用户将收到此回复:

{
  "status": 400,
  "data": {
    "type": "invalid_request_error",
    "message": "Malformed request: Field(s) doesn't match the required type",
    "code": 203,
    "errors": [
      {
        "field": "contents",
        "message": "Type of 'contents' field is incorrect. Current type is: 'NULL', it should be: 'string'"
      }
    ]
  }
}

我希望验证将null作为一个字符串处理,使上面的错误消息不显示,一切正常,结果如下:

{
  "status": 201,
  "data": {
    "id": 1,
    "contents": null,
    "display": true
  }
}

1 个答案:

答案 0 :(得分:0)

为什么不执行此操作:迭代所有元素,如果一个为null,则将其更改为&#34; null&#34; (字符串),另一方面,将其更改回来。