簡體   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