Пример #1
0
 public function validate($data)
 {
     if (empty($data)) {
         $objData = new \stdClass();
     } else {
         $objData = json_decode(json_encode($data));
     }
     $result = \Jsv4::coerce($objData, $this->schemaObj);
     if (!$result->valid) {
         throw new \Exception($result->errors[0]->message, 400);
     }
     return $result->value;
 }
Пример #2
0
function runJsonTest($key, $test)
{
    global $totalTestCount;
    global $failedTests;
    $totalTestCount++;
    try {
        if ($test->method == "validate") {
            $result = Jsv4::validate($test->data, $test->schema);
        } else {
            if ($test->method == "isValid") {
                $result = Jsv4::isValid($test->data, $test->schema);
            } else {
                if ($test->method == "coerce") {
                    $result = Jsv4::coerce($test->data, $test->schema);
                } else {
                    $failedTests[$key][] = "Unknown method: {$test->method}";
                    return;
                }
            }
        }
        if (is_object($test->result)) {
            foreach ($test->result as $path => $expectedValue) {
                $actualValue = pointerGet($result, $path, TRUE);
                if (!recursiveEqual($actualValue, $expectedValue)) {
                    $failedTests[$key][] = "{$path} does not match - should be:\n    " . json_encode($expectedValue) . "\nwas:\n    " . json_encode($actualValue);
                }
            }
        } else {
            if (!recursiveEqual($test->result, $result)) {
                $failedTests[$key][] = "{$path} does not match - should be:\n    " . json_encode($test->result) . "\nwas:\n    " . json_encode($result);
            }
        }
    } catch (Exception $e) {
        $failedTests[$key][] = $e->getMessage();
        $failedTests[$key][] .= "    " . str_replace("\n", "\n    ", $e->getTraceAsString());
    }
}