LINUX.ORG.RU

[JSON-Schema][JSV] References


0

1

Хочу хранить кое-какие данные в JSON.

Написал схему, пользуюсь валидатором JSV.

Я хочу схему, в которой будут содержаться несколько схем, и ссылаться друг на друга в описании.

{ "id" : "forms-schema",
  "properties"
  : { "form-definition"
      : { "type" : "object",
          "properties"
          : { "form-name" : { "type" : "string" },
              "widgets"
              : { "type"  : "array",
                  "items" : { "$ref" : "#properties.widget-definition" } } } },
      "widget-definition"
      : { "type" : "object",
          "properties"
          : [ { "$ref" : "#properties.entry-definition" },
              { "$ref" : "#properties.choice-definition" },
              { "$ref" : "#properties.block-definition" } ] },
      "entry-definition"
      : { "type" : "object",
          "properties"
          : { "type"       : { "type" : "string",
                               "enum" : [ "entry" ] },
              "field-name" : { "type" : "string" },
              "format"     : { "type" : "string",
                               "enum" : [ "default",
                                          "cyrillic-only",
                                          "float",
                                          "integer",
                                          "date",
                                          "time",
                                          "date-time" ] } } },  
      "choice-definition"
      : { "type" : "object",
          "properties"
          : { "type"       : { "type" : "string",
                               "enum" : [ "choice" ] },
              "field-name" : { "type" : "string" },
              "mode"       : { "type" : "string",
                               "enum" : [ "one-of",
                                          "many-of",
                                          "one-of-or-other",
                                          "many-of-or-other" ] },
              "items"      : { "type" : "array",
                               "items" : { "type" : "string" } } } },
      "block-definition"
      : { "type" : "object",
          "properties"
          : { "type" : { "type" : "string",
                         "enum" : [ "block" ] },
              "options"
              : { "type" : [ "null",
                             { "type" : "object",
                               "properties"
                               : { "mode" : { "type" : "string",
                                              "enum" : [ "or-mode" ] },
                                   "field-name" : { "type" : "string" } } } ],
                  "default" : null },
              "widgets" : { "type" : "array",
                            "items" : { "$ref" : "#properties.widget-definition" } } } } },
  "fragmentResolution" : "dot-delimited",
  "links"
  : [ { "rel" : "self", "href" : "{id}" } ] }

Далее:

var JSV = require('./jsv').JSV;
JSV.registerSchema(schema, schema["id"]);

// Схема корректно добавляется, видна в свойствах.

// Пробую провести валидацию:

JSV.validate(null, {"$ref" : "forms-schema#.properties.entry-definition"});

Возвращает errors=[], т.е. форма валидная. WTF?

Похоже что я неправильно задаю/использую ссылки.

Раскуривал вот эти документы:

http://tools.ietf.org/html/draft-zyp-json-schema-02

http://groups.google.com/group/json-schema/web/json-schema-proposal-working-draft

★★

Последнее исправление: vladimir-vg (всего исправлений: 2)

Блин, неужто никто с JSON не работает?

vladimir-vg ★★
() автор топика

Схема в первом посте вообще не корректна.

Решил проблему раскидав схемы частей по разным схемам.

Нашёл забавный способ описания union-схем:

{ "id"   : "ui:widget",
  "type" :  [ { "$ref" : "ui:entry" },
              { "$ref" : "ui:choice" },
              { "$ref" : "ui:block" } ] }
vladimir-vg ★★
() автор топика
Вы не можете добавлять комментарии в эту тему. Тема перемещена в архив.