MSON到JSON-Schema"其中一个"问题

时间:2017-10-20 12:34:16

标签: json apiblueprint mson

我尝试在ApiBlueprint MSON表示法中描述具有可变部分的对象。 这里是ApiBlueprint中的简单代码:

 FORMAT: 1A

# Test API

## Services [/Service/{id}]

### GET Service info [GET]


+ Request (application/json)

    + Headers

            Authorization: JWT <token>

+ Response 200 (application/json)

    + Attributes (array[ServiceResource], fixed)



# Data Structures

## Resource (object)
### Properties

+ id: `a6vhAo3FG` (string, fixed)
+ created_at: `2016-07-01T15:11:09.553Z` (string, required)
+ updated_at: `2017-11-22T08:07:55.002Z` (string, required)


## Service (object)

### Properties
+ type: tcp_service (string, required)
- One Of
    - config (TcpService, required)
    - config (IcmpService, required)


## ServiceResource (Resource)
### Properties

- Include Service

## TcpService (object)

### Properties

+ port: `80` (number, required) 
+ request_str: `HEAD` (string, required)
+ expect_response_str: `200 OK` (string, required)

## IcmpService (object)

### Properties

+ timeout_ms: `1000` (number, required)
+ packet_size_bytes: `1000` (number, required)
+ ttl: `128` (number, required)

它在apiary.io中完美呈现,但https://json-schema-validator.herokuapp.com中生成的Json Schema的验证报告错误:

[ {
  "level" : "error",
  "schema" : {
    "loadingURI" : "#",
    "pointer" : "/items"
  },
  "instance" : {
    "pointer" : "/0"
  },
  "domain" : "validation",
  "keyword" : "additionalProperties",
  "message" : "object instance has properties which are not allowed by the schema: [\"config\"]",
  "unwanted" : [ "config" ]
} ]

也许我做错了什么?

有没有办法在MSON中描述对象数组,其中对象具有提供正确JSON模式的变体部分?

1 个答案:

答案 0 :(得分:0)

我相信你正在寻找fixed-type type attributeoutput=[] cursor=0 while cursor < len(input): name=input[cursor] val=input[cursor+1] ntags=int(input[cursor+2]) optslice=input[cursor+3:cursor+3+2*ntags] cursor = cursor+3+2*ntags print name, val, ntags, optslice, cursor output.append([name, val, optslice]) print output > name val 0 [] 3 > name val 1 ['tag', 'val'] 8 > name val 2 ['tag', 'val', 'tag', 'val'] 15 > name val 0 [] 18 > [['name', 'val', []], ['name', 'val', ['tag', 'val']], ['name', 'val', ['tag', 'val', 'tag', 'val']], ['name', 'val', []]] 意味着值是固定的,不能是提供的示例值以外的任何值。使用fixed时,表示类型已修复,但值不是。

fixed-type