What is Joi?

Joi lets you describe your data using a simple, intuitive, and readable language. Simple to understand: a language that simply and directly describes your data model. So the point is to describe it and then verify it is easy. Dev/API /? V =17.4…

The installation

Execute NPM I joi in the project directory

The trial environment

@google-cloud/functions-framework

Simple trial

To verify a string parameter, run the following code:

const Joi = require('joi');
const schema = Joi.object({
username: Joi.string()
  .alphanum()
  .min(3)
  .max(30)
})

exports.helloWorld = (req, res) => {
  const { error, value } = schema.validate(req.query)
  if (error) {
    return res.status(422).json({ error: error })
  }
  res.send('Hello, World') 
}
Copy the code

http://localhost:8080/ returns Hello, World returns http://localhost:8080/? Username =aaaa return Hello, World visit http://localhost:8080/? The username = aa to return

{
    "error": {
        "_original": {
            "username": "aa"
        },
        "details": [
            {
                "message": "\"username\" length must be at least 3 characters long",
                "path": [
                    "username"
                ],
                "type": "string.min",
                "context": {
                    "limit": 3,
                    "value": "aa",
                    "label": "username",
                    "key": "username"
                }
            }
        ]
    }
}
Copy the code

Go to http://localhost:8080/? username=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

return

{
    "error": {
        "_original": {
            "username": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
        },
        "details": [
            {
                "message": "\"username\" length must be less than or equal to 30 characters long",
                "path": [
                    "username"
                ],
                "type": "string.max",
                "context": {
                    "limit": 30,
                    "value": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
                    "label": "username",
                    "key": "username"
                }
            }
        ]
    }
}
Copy the code

Go to http://localhost:8080/? username=aaaa&password=1234

return

{
    "error": {
        "_original": {
            "username": "aaaa",
            "password": "1234"
        },
        "details": [
            {
                "message": "\"password\" is not allowed",
                "path": [
                    "password"
                ],
                "type": "object.unknown",
                "context": {
                    "child": "password",
                    "label": "password",
                    "value": "1234",
                    "key": "password"
                }
            }
        ]
    }
}
Copy the code

Verify the body

Add required verification code:

const Joi = require('joi');
const schema = Joi.object({
    username: Joi.string()
        .alphanum()
        .min(3)
        .max(30),
    password: Joi.string()
        .alphanum()
        .min(6)
        .max(12)
})

exports.helloWorld = (req, res) => {
    const { error, value } = schema.validate(req.body)
    if (error) {
        return res.status(422).json({ error: error })
    }
    console.info(value)
    res.send(value)
};
Copy the code

Use postman to access http://localhost:8080 to enter data:

{
    "username": "postman",
    "password": "123456"
}
Copy the code

Returns:

{
    "username": "postman",
    "password": "123456"
}
Copy the code

Input data:

{
    "username": "postman",
    "password": "123456",
		"age":30
}
Copy the code

Returns:

{
    "error": {
        "_original": {
            "username": "postman",
            "password": "123456",
            "age": "30"
        },
        "details": [
            {
                "message": "\"age\" is not allowed",
                "path": [
                    "age"
                ],
                "type": "object.unknown",
                "context": {
                    "child": "age",
                    "label": "age",
                    "value": "30",
                    "key": "age"
                }
            }
        ]
    }
}
Copy the code

Input data:

{
    "username": "postman",
    "password": ""
}
Copy the code

Returns:

{
    "error": {
        "_original": {
            "username": "postman",
            "password": ""
        },
        "details": [
            {
                "message": "\"password\" is not allowed to be empty",
                "path": [
                    "password"
                ],
                "type": "string.empty",
                "context": {
                    "label": "password",
                    "value": "",
                    "key": "password"
                }
            }
        ]
    }
}
Copy the code

Verify that both parameters are entered

There is a difference between required and required:

const Joi = require('joi');
const schema = Joi.object({
    username: Joi.string()
        .alphanum()
        .min(3)
        .max(30),
    password: Joi.string()
        .alphanum()
        .min(6)
        .max(12),
    mobile: Joi.string()
        .alphanum()
}).and('username', 'password')

exports.helloWorld = (req, res) => {
    const { error, value } = schema.validate(req.query)
    if (error) {
        return res.status(422).json({ error: error })
    }
    console.info(value)
    res.send(value)
};
Copy the code

Input data:

http://localhost:8080/?username=aaaaaaa&password=123456
Copy the code

Returns:

{
    "username": "aaaaaaa",
    "password": "123456"
}
Copy the code

Input data:

http://localhost:8080/?username=aaaaaaa&mobile=123456
Copy the code

Returns:

{
    "error": {
        "_original": {
            "username": "aaaaaaa",
            "mobile": "123456"
        },
        "details": [
            {
                "message": "\"value\" contains [username] without its required peers [password]",
                "path": [],
                "type": "object.and",
                "context": {
                    "present": [
                        "username"
                    ],
                    "presentWithLabels": [
                        "username"
                    ],
                    "missing": [
                        "password"
                    ],
                    "missingWithLabels": [
                        "password"
                    ],
                    "label": "value",
                    "value": {
                        "username": "aaaaaaa",
                        "mobile": "123456"
                    }
                }
            }
        ]
    }
}
Copy the code

Input: http://localhost:8080/? Mobile = 123456 returned:

{
    "mobile": "123456"
}
Copy the code

One of the two options and accompanying

code

const Joi = require('joi');
const schema = Joi.object({
    username: Joi.string()
        .alphanum()
        .min(3)
        .max(30),
    password: Joi.string()
        .alphanum()
        .min(6)
        .max(12),
    mobile: Joi.string()
        .alphanum()
}).xor('username', 'mobile')
  .with('username', 'password')
  .with('mobile','password')

exports.helloWorld = (req, res) => {
    const { error, value } = schema.validate(req.query)
    if (error) {
        return res.status(422).json({ error: error })
    }
    console.info(value)
    res.send(value)
};
Copy the code

Input: http://localhost:8080/? Mobile = 123456789 & password = 654321 output:

{
    "mobile": "123456789",
    "password": "654321"
}
Copy the code

Input: http://localhost:8080/? Mobile = 123456 output:

{
    "error": {
        "_original": {
            "mobile": "123456"
        },
        "details": [
            {
                "message": "\"mobile\" missing required peer \"password\"",
                "path": [],
                "type": "object.with",
                "context": {
                    "main": "mobile",
                    "mainWithLabel": "mobile",
                    "peer": "password",
                    "peerWithLabel": "password",
                    "label": "value",
                    "value": {
                        "mobile": "123456"
                    }
                }
            }
        ]
    }
}
Copy the code

Input: http://localhost:8080/? The username = abcdfe&password = 654321 output:

{
    "username": "abcdfe",
    "password": "654321"
}
Copy the code

Input: http://localhost:8080/? The username = abcdfe&password mobile = = 654321 & 13999999999 output:

{
    "error": {
        "_original": {
            "username": "abcdfe",
            "password": "654321",
            "mobile": "13999999999"
        },
        "details": [
            {
                "message": "\"value\" contains a conflict between exclusive peers [username, mobile]",
                "path": [],
                "type": "object.xor",
                "context": {
                    "peers": [
                        "username",
                        "mobile"
                    ],
                    "peersWithLabels": [
                        "username",
                        "mobile"
                    ],
                    "present": [
                        "username",
                        "mobile"
                    ],
                    "presentWithLabels": [
                        "username",
                        "mobile"
                    ],
                    "label": "value",
                    "value": {
                        "username": "abcdfe",
                        "password": "654321",
                        "mobile": "13999999999"
                    }
                }
            }
        ]
    }
}
Copy the code

Other Common functions

Directly validate a variable or value: Joi.attempt(‘x’, joi.number ()); Allow an undefined key in an object: joi.object ({a: joi.any ()}).unknown(); Defining parameters cannot occur simultaneously:

const schema = Joi.object({
    a: Joi.any(),
    b: Joi.any()
}).nand('a', 'b');
Copy the code