gpt4 book ai didi

javascript - 如何使用 2 个不同的查询组合选项指定 1 个 api 路由

转载 作者:太空宇宙 更新时间:2023-11-04 00:37:32 27 4
gpt4 key购买 nike

如何使用 2 个不同的查询组合选项指定 1 个 api 路由

我们有 2 条路线:

  GET /api/v1/resource?filter=byName&key=restaurant&city=chicago
GET /api/v1/resource?filter=byLocation&lat=34&long=78

soaJS 中,我们这样描述我们的模式:

//文件 config.js

  module/exports = {
type: 'service',
...
schemas: {
...
commonFields: {
filter: {
source: ["query.filter"],
required: true,
validation: {
type: "string",
enum: ["byName", "byLocation"]
}
},
key: {
source: ["query.key"],
required: true,
validation: {
type: "string"
}
},
lat: {
source: ["query.lat"],
required: true,
validation: {
type: "number"
}
},
long: {
source: ["query.long"],
required: true,
validation: {
type: "number"
}
},
... // city

},
get: {
"/api/v1/resource": {
"_apiInfo": {
"l": "get resource by ?",
"m": "get"
},
"commonFields": [ ? ],
}
...
}

filter=byName时,则key为必填项,...

filter=byLocation时,则lat是强制的,...

我们应该如何根据 REST 理念处理 soajs 路由定义中的这两条路由差异?

谢谢

最佳答案

SOAJS IMFV 基于 json 模式。您可以通过使用 oneOf 来实现此目的。请查看此网址https://spacetelescope.github.io/understanding-json-schema/

具体操作方法如下:

{
schema : {
post : {
"/imfvOneOf": {
"_apiInfo": {
"l": "Example of IMFV & oneOf",
"group": "Basic"
},
"data": {
"source": ["body.data"],
"required": true,
"validation": {
"oneOf": [
{
filter: {
required: true,
type: "string",
enum: ["byName"]
},
key: {
required: true,
type: "string"
}
},
{
filter: {
required: true,
type: "string",
enum: ["byLocation"]
},
lat: {
required: true,
type: "number"
}
}
]
}
},
long: {
source: ["body.long"],
required: true,
validation: {
type: "number"
}
}
}
}
}
}

关于javascript - 如何使用 2 个不同的查询组合选项指定 1 个 api 路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38330994/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com