使用 PHP 进行 JSON 模式验证
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/2757662/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JSON schema validation with PHP
提问by Omer
Is there any PHP library that validates a JSON object against a JSON Schema?
是否有任何 PHP 库可以根据 JSON 模式验证 JSON 对象?
采纳答案by Bruno Reis
About jsonschemaphpv, although it′s not very well maintained, we use it a lot and it works. It′s a port from the js validator. It has a full test suite that runs against the php and the http://code.google.com/p/jsonschema/.
关于jsonschemaphpv,虽然维护得不是很好,但是我们经常使用它并且它有效。它是来自 js 验证器的端口。它有一个完整的测试套件,可以针对 php 和http://code.google.com/p/jsonschema/ 运行。
And guess what... It passes on more tests than the js. At least at the last time I run the tests. The thing is that the project is not very well structured and looks ugly. But I ensure you it′s very stable. It also allows you to validate associative arrays as objects either.
猜猜看……它通过了比 js 更多的测试。至少在我最后一次运行测试时。问题是该项目的结构不是很好,而且看起来很丑陋。但我向你保证它非常稳定。它还允许您将关联数组验证为对象。
I′ve written the tests in selenium first. After that I wrote them in phpunit. I′m not sure if the phpunit tests are there.
我首先用 selenium 编写了测试。之后我用phpunit写了它们。我不确定 phpunit 测试是否在那里。
If you give it a try and really want to use it I can start a branch for it on github with the phpunit tests.
如果您尝试一下并且真的想使用它,我可以使用 phpunit 测试在 github 上为它创建一个分支。
Bruno Reis (the person who ported the js to php)
Bruno Reis(将js移植到php的人)
回答by paq85
I'm using Justin Rainbow's JSON Schema for PHP https://github.com/justinrainbow/json-schemaand I'm quite happy about it.
我正在为 PHP https://github.com/justinrainbow/json-schema使用 Justin Rainbow 的 JSON Schema ,我对此很满意。
It's a fork of http://jsonschemaphpv.sourceforge.net/
这是http://jsonschemaphpv.sourceforge.net/的一个分支
You can easily install it via Composer.
您可以通过 Composer 轻松安装它。
回答by airboss
Take a look at http://json-schema.org/implementations.html. It has some validators in php and other languages.
看看http://json-schema.org/implementations.html。它有一些 php 和其他语言的验证器。
回答by automatix
On the JSON Schema websitethere is a list of the implementions, amongst other things of validators in/for PHP:
在JSON Schema 网站上有一个实现列表,其中包括 PHP 中/用于验证器的其他内容:
jsv4-php- supports version 4(Public Domain / MIT)
php-json-schema(MIT)
json-schema(Berkeley)
jvalidator(BSD3)
jsv4-php-支持版本 4(Public Domain / MIT)
php-json-schema(MIT)
json 模式(伯克利)
jvalidator(BSD3)
回答by Petr Trofimov
You could validate not JSON, but PHP array that could be obtained from many sources (JSON, XML, Post Data). To check if your array matches pattern you could take a look at https://github.com/ptrofimov/matchmaker
您可以验证的不是 JSON,而是可以从许多来源(JSON、XML、Post Data)获得的 PHP 数组。要检查您的数组是否与模式匹配,您可以查看https://github.com/ptrofimov/matchmaker

