【问题标题】:JSON Validation against JSON Schema针对 JSON Schema 的 JSON 验证
【发布时间】:2013-02-13 14:39:00
【问题描述】:

我正在尝试根据我的给定架构验证 JSON 对象。

JSON数据如下:

{
"list": {
    "places": [
        {
            "name": "Loopsiloo",
            "foursquareID": "54a6s5D4a6s5d4a6s5D4",
            "lat": 26.6546845354889,
            "lon": -99.6846873700158
        },
        {
            "name": "Loopsiloo",
            "foursquareID": "54a6s5D4a6s5d4a6s5D4",
        }
    ],
    "title": "Foo Bar",
    "dateCreated": "2013-01-29T14: 19: 30Z"
}

JSON Schema 如下:

{
"type":"object",
"$schema": "http://json-schema.org/draft-03/schema",
"required":true,
"properties":{
    "list": {
        "type":"object",
        "id": "list",
        "required":true,
        "properties":{
            "dateCreated": {
                "type":"string",
                "id": "dateCreated",
                "required":true
            },
            "places": {
                "type":"array",
                "minitems": "1",
                "id": "places",
                "required":true,
                "items":
                {
                    "type":"object",
                    "required":true,
                    "properties":{
                        "note": {
                            "type":"string",
                            "id": "note",
                            "required":false
                        },
                        "foursquareID": {
                            "type":"string",
                            "id": "foursquareID",
                            "required":true
                        },
                        "lat": {
                            "type":"number",
                            "id": "lat",
                            "required":true
                        },
                        "lon": {
                            "type":"number",
                            "id": "lon",
                            "required":true
                        },
                        "name": {
                            "type":"string",
                            "id": "name",
                            "required":true
                        }
                    }
                }


            },
            "title": {
                "type":"string",
                "id": "title",
                "required":true
            }
        }
    }
}

}

我正在使用 PHP 中的 JsonSchema\Validator 验证此 JSON。

$validator = new JsonSchema\Validator;
$validator->check($data, file_get_contents(__DIR__ . '/../model/api-schema.json'));

我的问题是验证器每次都验证 JSON 对象是否正确。在顶部的示例中,缺少属性“lat”和“lon”。即使我省略了整个“places”、“title”或“dateCreated”属性,它也被验证为正确。

我有什么遗漏吗?我浏览了 JSON 模式的文档,但没有任何帮助。

【问题讨论】:

  • 您的验证库使用哪个版本的架构标准?在 v4 中,required 现在是一个数组而不是布尔值。
  • 有谁知道一个好的 php schema4 验证器。似乎没有任何工作正常。

标签: php json validation jsonschema


【解决方案1】:

这对我有用。

$validator = new JsonSchema\Validator;
$schema = file_get_contents(__DIR__ . '/../model/api-schema.json');
$validator->check(json_decode($data), json_decode($schema));

【讨论】:

    猜你喜欢
    • 2021-10-21
    • 2018-05-24
    • 2021-11-04
    • 2020-12-01
    • 2019-04-21
    • 1970-01-01
    • 2022-08-16
    • 1970-01-01
    • 2015-10-24
    相关资源
    最近更新 更多