带有无序数组的JSON Schema draft 4

时间:2017-10-12 13:13:37

标签: arrays json validation schema jsonschema

我想创建架构,允许我在满足以下条件时添加多个地址:

  • 1)通讯地址必须在那里
  • 2)居住地址必须在那里
  • 3)其他类型的地址可以在那里
  • 4)它们可以按任何顺序出现

这可以用JSON模式解决吗?我听说"包含"因为我们正在使用Altova XML SPY(2018),它只支持草案4,用于模式创建,我想知道如何在草案4中解决这个问题。你是否知道草案6的任何好的编辑器?

我看了JSON schema to enforce array contents,但在那里找不到答案。我还阅读了https://gist.github.com/anonymous/7359001,其中解释了如何让#34;包含"在草案4中,但无法将其应用于我的案例。

因此,如果您可以指导我,如何将它用于以下架构(满足我的要求,除了4号),我将非常感谢。

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "description": "Comment describing your JSON Schema",
    "type": "object",
    "properties": {
        "addresses": {
            "type": "array",
            "items": [
                {
                    "type": "object",
                    "properties": {
                        "type": {
                            "type": "string",
                            "enum": [
                                "Corespondence"
                            ]
                        },
                        "otherData": {}
                    },
                    "required": [
                        "type",
                        "otherData"
                    ]
                },
                {
                    "type": "object",
                    "properties": {
                        "type": {
                            "type": "string",
                            "enum": [
                                "Residence"
                            ]
                        },
                        "otherData": {}
                    },
                    "required": [
                        "type",
                        "otherData"
                    ]
                }
            ],
            "additionalItems": {
                "$ref": "#/definitions/Address"
            }
        }
    },
    "definitions": {
        "Address": {
            "type": "object",
            "properties": {
                "type": {
                    "type": "string",
                    "enum": [
                        "Corespondence",
                        "Residence",
                        "Other"
                    ]
                },
                "otherData": {}
            },
            "required": [
                "type",
                "otherData"
            ]
        }
    }
}

2 个答案:

答案 0 :(得分:0)

draft-06有“包含”关键字,允许测试与数组中的模式匹配的项目的存在。

或者,您可以使用在不同属性中定义的差异地址类型的地图,而不是异构数组,因此您的数据可能是:

{
  addresses: {
    correspondence: {/*...*/},
    residence: {/*...*/}
    // etc...
  }
}

答案 1 :(得分:0)

草案06中添加import java.sql.Connection; import java.sql.DriverManager; import java.sql.Statement; public class PostgreSQLJDBC_Create { public static void main(String args[] ) { Connection c = null; Statement stmt = null; try { Class.forName("org.postgresql.Driver"); c = DriverManager .getConnection("jdbc:postgresql://localhost:5432/postgres", "postgres", "redacted"); System.out.println("Opened database successfully"); stmt = c.createStatement(); String sql = "CREATE TABLE public.feeding_schedules " + "schedule_id VARCHAR(80) COLLATE pg_catalog. default, " + "feeding_time VARCHAR(80) COLLATE pg_catalog. default, " + "recurrence VARCHAR(80) COLLATE pg_catalog. default, " + "food VARCHAR(80) COLLATE pg_catalog. default, " + "notes VARCHAR(80) COLLATE pg_catalog. default, " + "animalid integer NOT NULL" + "PRIMARY KEY (schedule_id) "; String addConstraint ="alter table public.feeding_schedules "+ "ADD CONSTRAINT feeding_schedule_animalid_fkey FOREIGN KEY "+ "FOREIGN KEY(animalid) "+ "REFERENCES public.animals (animalid) "+ "ON UPDATE NO ACTION "+ "ON DELETE NO ACTION"; stmt.executeUpdate(sql); stmt.executeUpdate(addConstraint); stmt.close(); c.close(); } catch ( Exception e ) { System.err.println( e.getClass().getName()+": "+ e.getMessage() ); System.exit(0); } System.out.println("Table created successfully"); } } 的原因是草案04关键字无法实现相同的目标。

在软件页面上有几个支持draft-06的实现:http://json-schema.org/implementations.html