简体   繁体   English

如何在不创建对象的情况下调用 Joi 类的方法?

[英]How are methods of the Joi class called without creating an object?

I tried using the Joi NPM module, in the documentation it states that it is a class, but it is used without creating a class object like var joi = new Joi();我尝试使用 Joi NPM 模块,在文档中它声明它是一个类,但使用它时没有创建类对象,如var joi = new Joi(); , how does this work? , 这是如何运作的?

I have a little knowledge of how a class works, it can be used by creating a class instance or by declaring the class static.我对类的工作原理知之甚少,可以通过创建类实例或声明类为静态来使用它。 Below is the code.下面是代码。

const Joi = require('@hapi/joi');
 function validation(args) {
    const schema = {
        "name": Joi.string().min(3).required()
    };
    return Joi.validate(args,schema);

 }
app.put('/api/courses/:id',(req,res)=>{
    const course = courses1.find(c => c.id === parseInt(req.params.id));
    if(!course){
       res.status(404).send('course not found...');
    }
    else{
        const {error} = validation(req.body);
        if(error){
            res.status(400).send(error.details[0].message);
        }
        else{
            course.name = req.body.name;
            res.status(200).send(courses1);
        }   
    }
});
// make new component validation.js

const Joi = require('@hapi/joi');


const signupValidation = (data) => {`enter code here`
    const schema = Joi.object({
        name: Joi.string().required(),
        email: Joi.string().required().email(),
       
        password: Joi.string().required(),
        role:Joi.string(),
    })
    return schema.validate(data)
}

module.exports.signupValidation = signupValidation ; 



//now you can import in your required component
import {signupValidation } from './validation'

    

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

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