Skip to content

Instantly share code, notes, and snippets.

@lotsofbytes
Last active October 6, 2018 19:02
Show Gist options
  • Save lotsofbytes/a77c78fdaf3fede4418caeb09cf284b3 to your computer and use it in GitHub Desktop.
Save lotsofbytes/a77c78fdaf3fede4418caeb09cf284b3 to your computer and use it in GitHub Desktop.
[バリデーション:json] #laravel #L55 #validation

バリデーション:json

入力値が、jsonデータであることを判定。

laravel 5.5

gistの画面

以下の実行で、ダウンロードできます

$ git clone git@gist.github.com:a77c78fdaf3fede4418caeb09cf284b3.git tests/Unit/Validations/Json
<?php
namespace Tests\Unit\Validations\Json;
use Tests\TestCase;
use Validator;
class JsonTest extends TestCase
{
/**
* @test
* @dataProvider provider_json
*/
public function json_test($input, $expected)
{
$v = Validator::make(
$input,
['values' => 'required|json']
);
$this->assertEquals($expected, $v->passes());
}
public function provider_json()
{
return [
[['values' => '{}'], true], // 空でもrequiredでOK
[['values' => '{"a":1}'], true],
[['values' => 'abcd'], false],
];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment