简体   繁体   English

Javascript运行时类型检查引擎

[英]Javascript runtime type checking engine

I've been using the Closure Compiler for some time and i've been wondering if there's some engine to make runtime validations of data types . 我已经使用Closure Compiler一段时间了,我一直想知道是否有一些引擎可以对数据类型进行运行时验证

The fact is types allow people to make less mistakes . 事实是类型允许人们减少错误 For example, in Haskell , lets say Int , any datatype must have a value, you just can't specify null or undefined to a value if it doesn't allow it. 例如,在Haskell ,让我们说Int ,任何数据类型都必须有一个值,如果它不允许,你就不能指定nullundefined给一个值。 Anyways you can specify a wrapper like Maybe Int to make it nullable . 无论如何,你可以指定一个像Maybe Int这样的包装器使它可以为 Which means that the value is null or not, and if it's not null, you can extract the value . 这意味着该值为null或不为null,如果它不为null,则可以提取该值 But you have first to check if it's null, there's no way to access the value first. 但是你首先要检查它是否为空,否则无法首先访问该值。

Closure Compiler does a pretty good job validating data types statically. Closure Compiler在静态验证数据类型方面做得非常好。 Some things are missing, like stronger null/undefined validations. 有些东西丢失了,比如更强的null / undefined验证。 Other things which escape from Closure Compiler are type checks with data downloaded from server. Closure Compiler中逃脱的其他事情是使用从服务器下载的数据进行类型检查 Even if you trust your server, specifications change and your code does also. 即使您信任您的服务器,规格也会发生变化,您的代码也会变更。 Tests can validate this kind of mismatches, but it would be better if those validations could occur directly in code. 测试可以验证这种不匹配,但如果这些验证可以直接在代码中进行则会更好。 They could even log errors found trying to match a type. 他们甚至可以记录试图匹配类型的错误。

The following code is a minimalist example of a server response: 以下代码是服务器响应的极简主义示例:

/**
 * Server result data types
 * @param {T} t [description]
 * @template T
 */
ServerResult = {
  /**
   * Result code from server
   * @type {Number}
   */
  code: 0,

  /**
   * Result data from server
   * @type {T}
   */
   data: null,

  /**
   * Message of the server in case something didn't go as expected
   * @type {String}
   */
  message: ''
};

Result.data could even be another datatype (a record, for example). Result.data甚至可以是另一种数据类型(例如记录)。 Right now what i'm doing is to recursively check the type contained in the default instance and compare it to the downloaded version . 现在我正在做的是以递归方式检查默认实例中包含类型 ,并将其与下载的版本进行比较。

  1. Is there a better way to do this? 有一个更好的方法吗?
  2. Is there a library I could be using instead of rewritting the wheel? 有没有我可以使用的图书馆而不是重写轮子?
  3. Is this a nonsense? 这是胡说八道吗?

There are quite a few libraries out there to help validate data types in JavaScript, but I believe the most comprehensive and popular one is tcomb (and its related library tcomb-validation ). 有很多库可以帮助验证JavaScript中的数据类型,但我相信最全面和最受欢迎的库是tcomb (及其相关库tcomb-validation )。

There's also joi from the folks who created hapi for Node.js. 还有从谁对Node.js的创建高致病性禽流感的人 However, I've found that trying to run joi in the browser can be a hassle. 但是,我发现尝试在浏览器中运行joi可能很麻烦。 Also, I've found it more difficult to capture complex data types using joi compared to tcomb, but this will depend on your use case. 此外,与tcomb相比,我发现使用joi捕获复杂数据类型更加困难,但这取决于您的用例。

You can also try Runtyper - a Babel plugin that performs types validation in runtime. 您还可以尝试Runtyper - 一个在运行时执行类型验证的Babel插件。 Comparing to tcomb it does not force you to write type annotations. tcomb相比,它不会强制您编写类型注释。 Instead it guess variable type from operation itself. 相反,它从操作本身猜测变量类型。

For your example, if server result contains incorrect code value: 例如,如果服务器结果包含错误的code值:

var ServerResult = {
  code: 'unknown',
  ...
}

and later you compare code to number : 然后你将code数字进行比较:

if (ServerResult.code === 0) { ... }

Runtyper will show warning in console: Runtyper将在控制台中显示警告:

Strict compare of different types: "unknown" (string) === 0 (number) 严格比较不同类型:“未知”(字符串)=== 0(数字)

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

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