【问题标题】:How to have one route with multiple schema for different parameters using fastify?如何使用 fastify 为不同的参数创建一个具有多个模式的路由?
【发布时间】:2020-12-24 06:07:20
【问题描述】:

我在使用 fastify 的中间件中有以下路由:

Route.ts server.get('/rest/api/cars/:twofourdoors/:autoManual/', { schema: getCarsSchema }, getCars(server));

基本上用户在这条路线上有 4 个选项,两门或四门,自动或手动。我的 .schema 中有一个模式,例如: 架构.ts

const carSchema = {
  type: 'object',
  properties: {
    wheels: { type: 'string' },
    color: { type: 'string' },
    gears: { type: 'string' },
    maxAutoSpeed: { type: 'string' },
    maxManualSpeed: { type: 'string' },
    twoDoorSize: { type: 'string' },
    fourDoorSize: { type: 'string' },
  },
};
    export const getCarsSchema = {
      summary: 'Get cars',
      description: 'Get cars by types',
      query: querySchema,
      response: {
        200: {
          type: 'object',
          properties: {
            totalItems: { type: 'number' },
            pageNumber: { type: 'number' },
            items: { type: 'array', items: carSchema },
          },
        },
        501: logMessageSchema,
      },
    };

carSchema 只是所有汽车属性的列表。我想要的是以下内容:

  1. 我可以使用一种模式来根据路由中的不同选项返回自定义属性吗?
  2. 如果无法达到 1,我能否为每个选项组合为路由提供 4 种不同的架构?

我不确定如何处理这个问题。谢谢!

【问题讨论】:

  • 我认为您可能需要在此处提供更多信息。请提供您的完整架构,包括carSchema,并提供您要验证的有效负载示例。目前,我不能确切地告诉你你在问什么。您可以扩展“根据路线中的不同选项返回自定义属性”吗?
  • 对不起,我更新了代码。如您所见,主要的汽车模式有 2 门和 4 门以及手动/自动功能,我想根据路线变量将它们分开。我可以对数据进行后检索删除(更昂贵)还是通过一条路线获取特定架构?
  • 哦,您希望根据路由参数使架构动态化。这不是 JSON Schema 之类的东西。抱歉,我对此无能为力。通常 JSON 模式不是运行时动态的,因为它们作为合同提供给其他人作为文档。我认为您正在寻找更通用的验证。

标签: typescript docker routes jsonschema fastify


【解决方案1】:

我可以使用一种模式来根据路由中的不同选项返回自定义属性吗?

是的,你可以。

在这个例子中,返回的数据是一样的,但是一个端点返回:

# /
{ items: [ { wheels: 4, serial: '123ASC', color: 'red' } ] }

# /no-serial
{ items: [ { wheels: 4, color: 'red' } ] }
const fastify = require('fastify')()

fastify.get('/', {
  handler,
  schema: {
    response: {
      200: {
        type: 'object',
        properties: {
          items: {
            type: 'array',
            items: {
              type: 'object',
              properties: {
                wheels: { type: 'number' },
                serial: { type: 'string' },
                color: { type: 'string' }
              }
            }
          }
        }
      }
    }
  }
})

fastify.get('/no-serial', {
  handler,
  schema: {
    response: {
      200: {
        type: 'object',
        properties: {
          items: {
            type: 'array',
            items: {
              type: 'object',
              properties: {
                wheels: { type: 'number' },
                color: { type: 'string' }
              }
            }
          }
        }
      }
    }
  }
})

fastify.inject('/', (err, res) => {
  console.log(res.json())
})

fastify.inject('/no-serial', (err, res) => {
  console.log(res.json())
})

function handler (request, reply) {
  reply.send({
    items: [
      { wheels: 4, serial: '123ASC', color: 'red' }
    ]
  })
}

【讨论】:

  • 我如何区分模式和我对它的路由调用?从路线的角度来看,我该怎么做?
  • 我不明白这一点,你能详细说明一下吗?为什么 jour 句柄应该知道应用的架构?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-10-01
  • 2017-10-23
  • 2013-12-27
  • 1970-01-01
  • 1970-01-01
  • 2016-08-30
  • 1970-01-01
相关资源
最近更新 更多