简体   繁体   English

独立于编程语言的模型验证

[英]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. 假设您在基础架构中使用几种不同的编程语言和框架来处理大量流量等。

Example Stack: 示例堆栈:

  1. event driven API servers (using Scala, node.js, Ruby EM) 事件驱动的API服务器(使用Scala,node.js,Ruby EM)
  2. a standard full stack webapp (eg Rails) 标准的全栈Web应用程序(例如Rails)
  3. (maybe more technologies) (也许更多技术)

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. 保持同步当然很痛苦。

How would you handle this without something like CORBA? 没有像CORBA这样的东西,您将如何处理?

Your best bet would be a framework that allows you to specify model validation in a language agnostic format, like JSON. 最好的选择是一个框架,该框架使您可以使用与语言无关的格式(例如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. 验证器只需要编写一次,然后为每个模型维护一个模式。

However, this is probably sounding a lot like CORBA/SOAP/Thrift/ProtocolBuffers/etc. 但是,这听起来很像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. 这是因为它们是为解决这类问题而编写的,如果您自己编写,最终将需要重新发明一些轮子。

I would go on a "dictionary" of Regular Expressions. 我将继续学习正则表达式的“字典”。 Regular Expressions are supported by all the languages you counted - and - translating their string representation from one language to another can be done by a passing the expressions themselves through regular expressions... 正则表达式受您计数的所有语言的支持-通过将正则表达式本身传递给正则表达式可以完成将其字符串表示形式从一种语言转换为另一种语言的操作...

To my observation - it's a lot less work then composing a parse and execute mechanism for each language... 据我观察-与每种语言组成一个解析和执行机制相比,要做的工作要少得多。

Like advised before - you can save this "dictionary" of Reg-Exps in an agnostic format, such as JSON. 像之前建议的那样-您可以以不可知的格式(例如JSON)保存Reg-Exps的此“词典”。 This narrows down the duplicated work to - 这样可以将重复的工作缩小为-

  • one source file of validation expressions that you maintain 您维护的验证表达式的一个源文件
  • per programming language: 每种编程语言:
    • converter from the main file to the format of the target language 从主文件转换为目标语言的格式
    • thin mechanism of 薄机制
      1. reading the JSON, 阅读JSON,
      2. selecting from it the configured checks 从中选择配置的检查
      3. executing them 执行它们
    • edge cases (if any) 边缘情况(如果有)

Have fun :) 玩得开心 :)

To add to @Nathan Ostgard's post, XML, XSD and, when necessary, XSLT might work as well. 要添加到@Nathan Ostgard的帖子中,XML,XSD以及必要时XSLT也可能会起作用。 The advantage to this would be a) XSD has the simple validation built into it b) most languages have good support for this c) you wouldn't have to write validation in each language; 这样的好处是:a)XSD内置了简单的验证功能; b)大多数语言对此都有很好的支持; c)您不必用每种语言编写验证; stuff that isn't handled in the schema could be written once in XSLT (with the caveat that XSLT implementations tend to vary :) ) 模式中未处理的内容可以在XSLT中编写一次(需要注意的是XSLT实现往往会有所不同:))

If you want to go the way of full validation, I'd use SOAP. 如果您想采用完全验证的方式,我将使用SOAP。 At least, as long as you have SOAP libraries, all you need to feed them is the WSDL for your interfaces. 至少,只要拥有SOAP库,提供它们所需的全部就是接口的WSDL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM