繁体   English   中英

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

[英]Verify data, how can I make null type count as string type

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

例:

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

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

例:

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

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

这是我的功能要点和一些测试

<?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正文(标头'Content-Type'设置为'application/json;charset=utf-8' )执行POST请求:

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

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

我使用该数组,通过将其与我之前提供的$fields示例进行比较来验证其数据类型,以检查主体数据是否为正确的类型。

如果其中任何一个不是正确的类型,我都会抛出一个Exception,并且用户会收到一个包含所有错误的响应(请参见下文)。

例如,如果用户已发布:

{"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
  }
}

您为什么不这样做:遍历所有元素,如果一个元素为null,则将其更改为“ null”(字符串),另一方面,将其更改回。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM