独立于编程语言的模型验证 [英] Programming Language independent Model Validation
问题描述
假设您在基础架构中使用多种不同的编程语言和框架来处理大量流量等.
Let's say you use several different programming languages and frameworks in your infrastructure to handle large amounts of traffic etc.
示例堆栈:
- 事件驱动的 API 服务器(使用 Scala、node.js、Ruby EM)
- 一个标准的全栈 web 应用程序(例如 Rails)
- (可能还有更多技术)
当使用不同的语言和框架时,我通常会重复大部分模型验证,因为每个客户入口点"都需要验证其输入.这当然很难保持同步.
When using different languages and frameworks I usually end up duplicating most of the model validations because every "customer entry point" needs to validate its input. This is of course a pain to keep in sync.
如果没有像 CORBA 这样的东西,你会如何处理这个问题?
How would you handle this without something like CORBA?
推荐答案
您最好的选择是一个框架,它允许您以与语言无关的格式(如 JSON)指定模型验证.您最终可能会得到各种验证模式,例如:
Your best bet would be a framework that allows you to specify model validation in a language agnostic format, like JSON. You might end up with a validation schema of sorts, say:
{
"name": [
{
"validate": "length",
"minLength": 6,
"maxLength": 10
},
...
],
...
}
然后,您将拥有可以解析此格式的特定于语言的验证器.验证器只需编写一次,然后您就可以为每个模型维护一个模式.
You would then have language-specific validators that could parse this format. The validators only need to be written once, and then you maintain a single schema for each model.
然而,这可能听起来很像 CORBA/SOAP/Thrift/ProtocolBuffers/等.这一点.那是因为它们是为解决这些类型的问题而编写的,如果你自己编写它,你最终会重新发明一些轮子.
However, this is probably sounding a lot like CORBA/SOAP/Thrift/ProtocolBuffers/etc. at this point. That's because they were written to solve these types of problems, and you'll end up reinventing a few wheels if you write it yourself.
这篇关于独立于编程语言的模型验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!