・2019/11/10
Raspberry Piで Opis JSON Schema Validateを動かす
(JSONの構造が正しいかを、JSONスキーマの定義ファイルを用いて検証する方法)
Tags: [Raspberry Pi], [電子工作], [セキュリティ]
● Raspberry Piで Opis JSON Schema Validateを動かす
JSONの構造が正しいかを、JSONスキーマの定義ファイルを用いて検証する方法。
Opis JSON Schema Validate JSON documents
JSON Schema validator for PHP https://opis.io/json-schema
Opis JSON Schema is a PHP implementation for the JSON Schema standard (draft-07 and draft-06), that will help you validate all sorts of JSON documents, whether they are configuration files or a set of data sent to an RESTful API endpoint.
Opis JSON Schema Validate JSON documents
Quick start
Opis JSON Schema Validateは PHP言語で動く JSON構造チェッカーです。
pi@raspberrypi:~ $ php -v
PHP 7.3.14-1~deb10u1 (cli) (built: Feb 16 2020 15:07:23) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.14, Copyright (c) 1998-2018 Zend Technologies
with Zend OPcache v7.3.14-1~deb10u1, Copyright (c) 1999-2018, by Zend Technologies
pi@raspberrypi:~/json-schema $ composer -V
Composer 1.8.4 2019-02-11 10:52:10
pi@raspberrypi:~ $ composer require opis/json-schema
Using version ^1.0 for opis/json-schema
./composer.json has been created
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 1 install, 0 updates, 0 removals
- Installing opis/json-schema (1.0.18): Downloading (100%)
opis/json-schema suggests installing opis/string (A standalone library for manipulating multibyte strings)
Writing lock file
Generating autoload files
cd
mkdir json-schema
cd json-schema
composer require opis/json-schema
# Installing opis/json-schema (1.0.18): Loading from cache
test_data.json
{
"name": "John Doe",
"age": 31,
"email": "john@example.com",
"website": null,
"location": {
"country": "US",
"address": "Sesame Street, no. 5"
},
"available_for_hire": true,
"interests": ["php", "html", "css", "javascript", "programming", "web design"],
"skills": [
{
"name": "HTML",
"value": 100
},
{
"name": "PHP",
"value": 55
},
{
"name": "CSS",
"value": 99.5
},
{
"name": "JavaScript",
"value": 75
}
]
}
● The validation schema
schema.json
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "http://api.example.com/profile.json#",
"type": "object",
"properties": {
"name": {
"type": "string",
"minLength": 1,
"maxLength": 64,
"pattern": "^[a-zA-Z0-9\\-]+(\\s[a-zA-Z0-9\\-]+)*$"
},
"age": {
"type": "integer",
"minimum": 18,
"maximum": 100
},
"email": {
"type": "string",
"maxLength": 128,
"format": "email"
},
"website": {
"type": ["string", "null"],
"maxLength": 128,
"format": "hostname"
},
"location": {
"type": "object",
"properties": {
"country": {
"enum": ["US", "CA", "GB"]
},
"address": {
"type": "string",
"maxLength": 128
}
},
"required": ["country", "address"],
"additionalProperties": false
},
"available_for_hire": {
"type": "boolean"
},
"interests": {
"type": "array",
"minItems": 3,
"maxItems": 100,
"uniqueItems": true,
"items": {
"type": "string",
"maxLength": 120
}
},
"skills": {
"type": "array",
"maxItems": 100,
"uniqueItems": true,
"items": {
"type": "object",
"properties": {
"name": {
"type": "string",
"minLenght": 1,
"maxLength": 64
},
"value": {
"type": "number",
"minimum": 0,
"maximum": 100,
"multipleOf": 0.25
}
},
"required": ["name", "value"],
"additionalProperties": false
}
}
},
"required": ["name", "age", "email", "location",
"available_for_hire", "interests", "skills"],
"additionalProperties": false
}
test.php
<?php
require __DIR__ . '/vendor/autoload.php';
use Opis\JsonSchema\{
Validator, ValidationResult, ValidationError, Schema
};
$data = file_get_contents("./test_data.json");
$data = json_decode($data);
$schema = Schema::fromJsonString(file_get_contents('./schema.json'));
$validator = new Validator();
/** @var ValidationResult $result */
$result = $validator->schemaValidation($data, $schema);
if ($result->isValid()) {
echo '$data is valid', PHP_EOL;
} else {
/** @var ValidationError $error */
$error = $result->getFirstError();
echo '$data is invalid', PHP_EOL;
echo "Error: ", $error->keyword(), PHP_EOL;
echo json_encode($error->keywordArgs(), JSON_PRETTY_PRINT), PHP_EOL;
}
pi@raspberrypi:~ $ php test.php
$data is valid
{
"name": "John Doe",
"age": "ABC",
"email": "john@example.com",
...
pi@raspberrypi:~ $ php test.php
$data is invalid
Error: type
{
"expected": "integer",
"used": "string"
}
Tags: [Raspberry Pi], [電子工作], [セキュリティ]
●関連するコンテンツ(この記事を読んだ人は、次の記事も読んでいます)
Raspberry Piで Googleの Go言語をインストールして動かす
Raspberry Piで Googleの Go言語をインストールして動かす
Raspberry Piで Googleの Go言語 + Gin Webフレームワークをインストールして Webアプリを作る
Raspberry Piで Googleの Go言語 + Gin Webフレームワークをインストールして Webアプリを作る
Raspberry Piで WebAPIモックサーバー JSON Serverを動かしてみるテスト
Raspberry Piで APIモックサーバー JSON Serverを動かしてみるテスト
Raspberry Piで WebAPIモックサーバー Stubcellを動かしてみるテスト
Raspberry Piで APIモックサーバー Stubcellを動かしてみるテスト
Raspberry Piで WebAPIモックサーバー swagger-nodeを動かしてみるテスト
Raspberry Piで APIモックサーバー swagger-nodeを動かしてみるテスト
Raspberry Piに PHP 7.2を公式リポジトリからサクッと apt-getでインストールする方法
PHP7.2をラズパイ公式リポジトリから Raspberry Piに簡単にインストールする方法
Raspberry Pi 3の Linuxコンソール上で使用する各種コマンドまとめ
ラズパイの Raspbian OSのコマンドラインで使用する便利コマンド、負荷試験や CPUシリアル番号の確認方法等も
Raspberry Piに Avahi Bonjourサービスを入れて、ホスト名で接続できる様にする方法
ラズパイに Bonjourサービスを入れて IPアドレスが分からなくてもホスト名で簡単に接続する方法
Raspberry Piや Jetson NANO等をネットワークに接続した場合の IPアドレスの便利ツール xfinder
DHCPで自動で IPアドレスが割り当てられる場合に、ワンボードマイコンの IPアドレスを調べる場合に便利
[HOME]
|
[BACK]
リンクフリー(連絡不要、ただしトップページ以外は Web構成の変更で移動する場合があります)
Copyright (c)
2020 FREE WING,Y.Sakamoto
Powered by 猫屋敷工房 & HTML Generator
http://www.neko.ne.jp/~freewing/raspberry_pi/raspberry_pi_validate_json_structure_with_json_scheme/