又一种数据格式描述语言

yaddle的Python项目详细描述


Build Status

另一种数据格式描述语言

@role: admin | author | collaborator | "role with space"

user:
  name: str{3,20}
  age: int{10,200}
  gender: male | female
  roles: [@role]
  description?: str{,200}

转换为JSON模式

{"additionalProperties":false,"definitions":{"role":{"enum":["admin","author","collaborator","role with space"]}},"required":["user"],"type":"object","properties":{"user":{"additionalProperties":false,"required":["name","age","gender","roles"],"type":"object","properties":{"gender":{"enum":["male","female"]},"age":{"minimum":10,"type":"integer","maximum":200},"name":{"minLength":3,"type":"string","maxLength":20},"roles":{"items":{"$ref":"#/definitions/role"},"type":"array"},"description":{"type":"string","maxLength":200}}}}}

API

使用load/loads将yaddle转换为json模式

fromyaddleimportload,loadsload(open("some.ydl"))loads("""[str]{,3}""")

cli

cat schema.ydl | python -m yaddle.tool

更多详细信息

数量

int{100,200}
{"type":"integer","minimum":100,"maximum":200}
num{,,0.1}
{"type":"number","multipleOf":0.1}

字符串

str{1,2} /pattern/
{"type":"string","minLength":1,"maxLength":20,"pattern":"pattern"}
/pattern/
{"type":"string","pattern":"pattern"}

格式date-timeemailhostnameipv4ipv6uri

%email
{"format":"email"}

数组

[str]{1,10}
{"type":"array","minItems":1,"maxItems":10,"items":{"type":"string"}}
[str|num]
{"type":"array","items":{oneOf:[{"type":"string"},{"type":"number"}]}}
[str, num]
{"type":"array","items":[{"type":"string"},{"type":"number"}}}

!用于唯一项

[num]!
{"type":"array","items":{{type:"number"}},"uniqueItems":true}

对象

  • 所有属性都是必需的,但后缀为?的属性除外
  • ...允许additionalProperties
key: str
size?: number
...
{"type":"object","properties":{"key":{"type":"string"},"size":{"type":"number"},"required":["key"]}"additionalProperties":true}

其中一个,任意一个,允许

  • |对于类似的@ref | @ref2
  • /用于任何
  • &用于分配

参考值

本地参考

@address:
    street_address: str
    city: str
    state: str

billing_address: @address
shipping_address: @address
{"additionalProperties":false,"definitions":{"address":{"additionalProperties":false,"required":["street_address","city","state"],"type":"object","properties":{"city":{"type":"string"},"state":{"type":"string"},"street_address":{"type":"string"}}}},"required":["billing_address","shipping_address"],"type":"object","properties":{"billing_address":{"$ref":"#/definitions/address"},"shipping_address":{"$ref":"#/definitions/address"}}}

参考远程模式(待定)

@"http://example.com/schema"

@product:
    price: num{0,}
    title: str{,200}

在另一个模式中引用它

@example: "http://example.com/schema"

products: [@example:product]

示例

示例从http://json-schema.org/example2.html翻译为yaddle

@diskDevice:
    type: disk
    divice: /^/dev/[^/]+(/[^/]+)*$/
@diskUUID:
    type: disk
    label: /^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}$/
@nfs:
    type: nfs
    remotePath: /^(/[^/]+)+$/
    server: %host-name | %ipv4 | %ipv6
@tmpfs:
    type: tmpfs
    sizeInMB: int{16,512}

storage: @diskDevice | @diskUUID | @nfs | @tmpfs
fstype?: ext3 | ext4 | btrfs
options?: [str]{1,}!
readonly?: bool

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
spring JSTL:绝对uri:http://java.sun.com/jsp/jstl/core无法在两个站点中解析。xml或与此应用程序一起部署的jar文件   java在Junit测试中找不到失败的符号   java为什么在没有同步块的情况下调用wait()、notify()或notifyAll()不是编译器错误?   使用onDraw()绘制多个位图的java Android在曲面视图上放大缩小画布   在java中比较args值和字符串   java更改文件中的特定字节   java TransformerFactory破坏<html>标记中的<input>和<br>标记   azure appinsights java自定义指标不起作用   Java泛型在实现接口的类上指定上界,接口本身指定上界   java AS400 prog和动态类路径   java应用程序截图时收到空消息   java无法从片段中获取上下文   找不到java配置单元UDF类   linux是一个提供java的接口。lang.NoClassDefFoundError   java如何使用面向对象建模创建此数据库