用Ajv来验证data数据是否符合自己设定的格式
安装
npm install ajv
运用
// 引入 import Ajv from "ajv" // Node.js require: const Ajv = require("ajv") const ajv = new Ajv() // 创建一个新的ajv options can be passed, e.g. {allErrors: true} const schema = { //自己需要的文件格式 type: "object", properties: { foo: {type: "integer"}, bar: {type: "string"} }, required: ["foo"], additionalProperties: false, } const validate = ajv.compile(schema) const valid = validate(data)//验证data是否符合,返回一个布尔 if (!valid) console.log(validate.errors) //判断布尔,提示
官网的使用方式 https://www.npmjs.com/package/ajv
浙公网安备 33010602011771号