作为一名后端研发,工作中避免不了的是与前端进行接口交互,有时候传的是json格式的数据,之前传的json数据也都是比较简单的格式,只有一层key-value,这次中有好多层的数据,校验起来相当麻烦,问了同事也没有好的解决办法。后来在网上查找资料发下有了灵丹妙药。就是今天要介绍的justinrainbow/json-schema,github地址。
下面介绍下用法,先上代码:
需要一个模板文件,格式为json,里面定义了数据的属性和类型,类似许多框架的校验类。
<?php
$tmp = [
"type" => "object",
"properties" => [
"index_page" => [
"type" => "object",
"properties" => [
"tpl_no" => [
"type" => "integer"
],
"primary_vision" => ['type' => "string"],
"coupon_pic" => ['type' => "string"],
"fetch_button" => [
"type" => "object",
"properties" => [
"text" => ['type' => "string"],
"text_color" => ['type' => "string"],
"color" => ['type' => "string"],
]
],
"bg_color" => ['type' => "string"],
"page_text_color" => ['type' => "string"],
"slide" => [
"type" => "array",
"minItems" => 1,
"items" => [
"type" => "object",
"properties" => [
"url" => ["type" => "string"],
"router" => ["type" => "string"],
],
"required" => ['url', 'router']
]
],
"logo" => ["type" => "string"],
],
"required"=>['tpl_no',"primary_vision","coupon_pic","fetch_button","bg_color","page_text_color","slide","logo"]
],
"copywriting" => [
"type" => "object",
"properties" => [
"share_pic" => ['type' => "string"],
"index_share_title" => ['type' => "string"],
"index_share_desc" => ['type' => "string"],
"rule" => ['type' => "string"],
],
"required"=>['share_pic',"index_share_title","index_share_desc","rule"]
],
"submit" => ['type' => "string"],
"activity_code" => ['type' => "string"],
"email_prefix" => ['type' => "string"]
],
"required"=>['index_page',"copywriting","activity_code","email_prefix"]
];
$tmp = json_encode($tmp);
$data = '{"index_page":{"tpl_no":1,"primary_vision":"http://10.16.34.42:8080/group3/M00/10/91/ChAiKlyYTXiAAdpnAAEDFxTmJWw032.jpg","coupon_pic":"http://10.16.34.42:8080/group3/M00/10/91/ChAiKlyYTXuAYaFqAAEDFxTmJWw580.jpg","fetch_button":{"text":"立即领取","text_color":"#fff","color":"#ff961e"},"bg_color":"#3d4347","rule_color":"#ccc","slide":[{"url":"http://10.16.34.42:8080/group3/M00/10/91/ChAiKlyYTZ6AbiWEAAEDFxTmJWw983.jpg","router":"123"}],"logo":"http://10.16.34.46:8080/group2/M00/16/EE/ChAiLlyYTbmARJh1AAEDFxTmJWw337.jpg"},"copywriting":{"share_pic":"http://10.16.34.46:8080/group2/M00/16/EE/ChAiLlyYTaaAfZcMAAEDFxTmJWw903.jpg","index_share_title":"122","index_share_desc":"11","rule":"<p>s<span style=\"font-weight: bold;\">assssfgsssssss啧啧啧</span></p>"},"submit":"1","activity_code":"rentLuckyCoupon","email_prefix":"ac"}';
require __DIR__ . '/../vendor/autoload.php';
$data = json_decode($data);
// Validate
$validator = new JsonSchema\Validator();
$validator->check($data, json_decode($tmp));
if ($validator->isValid()) {
echo "The supplied JSON validates against the schema.\n";
} else {
echo "JSON does not validate. Violations:\n";
foreach ($validator->getErrors() as $error) {
echo sprintf("[%s] %s\n", $error['property'], $error['message']);
}
}
上面就也可以校验json的结构了,方便了好多。目前仅用到了上面的功能,并不能过滤掉不需要的结构,更多的功能请参考文档。
https://json-schema.org/understanding-json-schema/reference/object.html
https://github.com/justinrainbow/json-schema