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

在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/


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值