如何使用tv4在postman中测试JSON模式?

时间:2018-01-04 15:33:15

标签: postman tv4

这就是我正在尝试的,但它总是通过测试,即使是糟糕的结果。

<?php
/**
 * edit.php
 */

// If the form is submitted
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $stmt = "UPDATE Persons
    SET name='".$_POST['name']."',
    city='".$_POST['city']."'
    WHERE id='".$_POST['id']."'
    LIMIT 1
    ";

    $database->execute($stmt);

    echo "Data saved";
    exit;
}

// Get the data for the chosen record to edit
$personId = $_GET['id'];

$stmt = "SELECT
*
FROM Persons
WHERE id='".$personId."'
";

// Again, get your data
$person = $database->execute();
?>

<!-- Your form -->
<form action="<?= $_SERVER['PHP_SELF']; ?>" method="POST" role="form">
    Name: <input type="text" name="name" value="<?= $person['name']; ?>"><br />
    City: <input type="text" name="city" value="<?= $person['city']; ?>"><br />
    <input type="hidden" name="id" value="<?= $person['id']; ?>">
    <button type="submit" class="btn btn-primary">Submit</button>
</form>

1 个答案:

答案 0 :(得分:7)

这不起作用的原因(简而言之)是Postman(tv4)使用的基础库不再维护。在今天早些时候遇到问题后,我找到了solution

tv4.validate(data, schema, false, true)

后两个参数是checkRecursive和banUnknownProperties。如上所示设置这两个标志使验证按预期工作。

您可能还会发现此代码段很有用,它会通过控制台报告任何验证错误:

pm.test("Response body is valid", function() {
  var data = JSON.parse(responseBody);
  var valid = tv4.validate(data, schema, false, true);
  if (valid !== true) {
      console.log(tv4.error);
  }
  pm.expect(valid).to.be.true;
});