简体   繁体   English

JOI上调用的多个function如何工作?

[英]How does multiple function called on JOI work?

How does Joi.string().aphanum() and so on work. Joi.string().aphanum() 等如何工作。 How is it possible to stack function calls one after the other like this, as shown in name and birthyear.怎么可能像这样一个接一个地堆叠 function 调用,如姓名和出生年份所示。 Is this function all in a class or something and if it is how is this possible to chain of function calls like this这是 function 都在 class 还是什么的,如果这怎么可能链 function 调用这样

const Joi = require('joi'); 
const schema = Joi.object().keys({ 
  name: Joi.string().alphanum().min(3).max(30).required(),
  birthyear: Joi.number().integer().min(1970).max(2013), 
}); 

That's exactly right.这是完全正确的。 Each of these methods return a new object (that also has those methods).这些方法中的每一个都返回一个新的 object(也具有这些方法)。 It's very similar to doing something like someString.toUpperCase().split("") in normal Javascript.这与在普通 Javascript 中执行类似someString.toUpperCase().split("")的操作非常相似。 This will first get the string, run the built-in toUpperCase function on it, and then run another built-in function split on the returned string.这将首先获取字符串,在其上运行内置的toUpperCase function,然后在返回的字符串上运行另一个内置的 function split

You're just stacking methods and building new objects after each call.您只是在每次调用后堆叠方法并构建新对象。

Its possible because each of these methods return the newly modified object.这是可能的,因为这些方法中的每一个都返回新修改的 object。

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

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