RAML 1.0中引用JSON Schema的方法详解

来源:互联网 发布:二环十三郎 知乎 编辑:程序博客网 时间:2024/06/09 17:35

在RAML 1.0中,不再推荐使用Schema,但是为了保持与RAML 0.8的兼容,所以仍然保留了Schema相关的关键字。

在RAML 1.0中,放弃了RAML 0.8的资源的概念,取而代之的是数据类型。所以,使用Schema的方式也是在定义数据类型Data Types。

虽然仍然保留了schemas,但是推荐使用types替代。而且,types和schemas,二者只能出现其一。

同样,在引用一个具体的数据类型时,推荐使用type替代schema。而且,type和schema,二者只能出现其一。

即在RAML 1.0中,以types的形式定义Schema,以type的形式引用Schema。

1.命名的Schema(可重用)

1) 借助于JSON Schema定义数据类型

types:  song: !include jukebox-include-song.schema  artist: !include jukebox-include-artist.schema  album: !include jukebox-include-album.schema

types:  song:  type: !include jukebox-include-song.schema  artist:  type: !include jukebox-include-artist.schema  album:  type: !include jukebox-include-album.schema

或(不推荐如下方式)

types:  song:  schema: !include jukebox-include-song.schema  artist:  schema: !include jukebox-include-artist.schema  album:  schema: !include jukebox-include-album.schema

types:  song: |    {      "type": "object",      "$schema": "http://json-schema.org/draft-03/schema",      "id": "http://jsonschema.net",      "required": true,      "properties": {        "songTitle": {          "type": "string",          "required": true        },        "albumId": {          "type": "string",          "required": true,          "minLength": 36,          "maxLength": 36        }      }    }  artist:    ...

2) 对于上述命名的数据类型,使用其数据类型

body:  application/json:    type: song


3) 也可以直接引用从未声明过的JSON Schema
body:  application/json:    type: !include jukebox-include-chronus.schema

或(不推荐如下方式)

body:  application/json:    schema: !include jukebox-include-chronus.schema

2.不预定义数据类型,在使用时直接以Schema给出匿名的数据类型(不可重用)
body:  application/json:    type: |      {        "type": "object",        "$schema": "http://json-schema.org/draft-03/schema",        "id": "http://jsonschema.net",        "required": true,        "properties": {          "songTitle": {            "type": "string",            "required": true          },          "albumId": {            "type": "string",            "required": true,            "minLength": 36,            "maxLength": 36          }        }      }

3.只引用Schema中的子元素
type: !include elements.xsd#Foo

参考链接:

https://github.com/raml-org/raml-spec/blob/master/versions/raml-10/raml-10.md/


原创粉丝点击