一个POST REST请求,具有如下3个主体参数:
{
"name" : "ABC",
"age": 34,
"uniqueID": "12345sdfgh"
}
我的要求是为每个字段名称、年龄和唯一id定义约束(类型、最大长度、最小长度、正则表达式等(。
我该如何定义?
有一些不同的定义方法。"纯"RAML方法是使用类型的RAML定义为数据对象定义数据类型片段。这些应该能满足你的所有需求。
示例:
dataType.raml
#%RAML 1.0 DataType
type: object
displayName: Booking
properties:
BookingDetail:
type: object
required: true
displayName: "BookingDetail"
description: "BookingDetail"
properties:
Name:
type: string
required: true
displayName: "Name"
description: "Name"
example: "John"
NumberOfDays:
type: integer
required: true
minimum: 1
maximum: 10
API:
#%RAML 1.0
title: so-type
/bookings:
post:
body:
application/json:
type: !include dataType.raml
如果您愿意,也可以使用JSON模式:
/orders:
post:
body:
application/json:
type: !include schemas/OrdersSchema.json
我想还有一件事。要要求输入符合正则表达式,可以执行以下操作:
properties:
Name:
type: string
required: true
displayName: "Name"
description: "Name"
pattern: ^[-A-Za-z ]+$
example: "John"
这种模式过于严格,但确实与许多西方传统名称相匹配。您自己的正则表达式可能构造得更为谨慎。