gpt4 book ai didi

javascript - 如何在 JS 和 Rails 中处理参数验证?

转载 作者:行者123 更新时间:2023-12-03 00:08:03 26 4
gpt4 key购买 nike

我想知道在前端(JS/React)和后端(Rails/Mongoid)中处理参数验证的更好方法是什么。我正在考虑直接在 Rails Controller 中进行验证,因为几乎所有内容都是可选的,它用于 API,并且有许多参数。即使不需要该字段,Mongoid 也会使用 null 值填充每个 field 条目。

我想有一个像这样的配置文件:

search_engine_a:
q:
required: true
explanation: Query to be searched
error: Query is required
num:
explanation: Number of results
type: Integer
interval: 0_100
error: Needs to be an integer between 0 and 100
start
explanation: Results offset
type: Integer
error: Needs to be an integer between 0 and 100
...
search_engine_b:
wd:
required: true
explanation: Query to be searched
...
...
...

然后在 Ruby on Rails 中使用一些代码来解析它以针对我们的 API 请求使用,并在需要时提供错误消息。前端的 JS 也是如此。解析相同的配置文件并使用它在我们的 API Playground 界面中提供错误和解释。

这有意义还是我在重新发明轮子?

[编辑] 为了给出更精确的想法,它适用于这个 API:https://serpapi.com/search-api

最佳答案

您是否考虑过使用 json-schemavalidate it server side 有很多插件, typescript integration ,可能还有更多我不知道的事情!

关于javascript - 如何在 JS 和 Rails 中处理参数验证?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54874181/

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