首页 > 编程技术 > js

Ajv format校验使用示例分析

发布时间:2022-11-1 16:46 作者:每天都是不一样的太阳

初始化项目demo

 npm init -y

安装 Ajv 版本 7

 npm install ajv

安装ajv-formats插件

 // ESM/TypeScript import
 import Ajv from "ajv"
 import addFormats from "ajv-formats"
 // Node.js require:
 const Ajv = require("ajv")
 const addFormats = require("ajv-formats")
 const ajv = new Ajv()
 addFormats(ajv)

运行分解

新建index.js文件

 const Ajv = require("ajv")
 const addFormats = require("ajv-formats")
 const ajv = new Ajv()
 addFormats(ajv)
 const schema = {
   type: "string",
   format: 'email',
   minLength: 1,
   maxLength: 255,
   pattern: '/^[a-zA-Z]/'
 };
 const validate = ajv.compile(schema)
 const data = 'string'
 const valid = validate(data)
 console.log(valid)
 if (!valid) console.log(validate.errors)

打开控制台,运行node index.js。

分析

在这里我们就可以利用vscode自带的调试功能,进行代码分析了。首先,我在19行打了断点,这样我们就可以观察到函数的参数和调用情况了。不会调试的同学可以看看这篇文章 新手向:前端程序员必学基本技能——调试JS代码 调试之后,就可以看到编译之后的回调函数了。如下代码

 (function anonymous(self, scope) {
   const schema11 = scope.schema[6];
   const formats0 = scope.formats[0];
   const func2 = scope.func[1];
   const pattern0 = scope.pattern[0];
   return function validate10(data, {instancePath = "", parentData, parentDataProperty, rootData = data} = {}) {
     let vErrors = null;
     let errors = 0;
     if (errors === 0) {
       if (errors === 0) {
         if (typeof data === "string") {
           if (func2(data) > 255) {
             validate10.errors = [{
               instancePath,
               schemaPath: "#/maxLength",
               keyword: "maxLength",
               params: {
                 limit: 255
               },
               message: "must NOT have more than 255 characters"
             }];
             return false;
           } else {
             if (func2(data) < 1) {
               validate10.errors = [{
                 instancePath,
                 schemaPath: "#/minLength",
                 keyword: "minLength",
                 params: {
                   limit: 1
                 },
                 message: "must NOT have fewer than 1 characters"
               }];
               return false;
             } else {
               if (!pattern0.test(data)) {
                 validate10.errors = [{
                   instancePath,
                   schemaPath: "#/pattern",
                   keyword: "pattern",
                   params: {
                     pattern: "/^[a-zA-Z]/"
                   },
                   message: "must match pattern "" + "/^[a-zA-Z]/" + """
                 }];
                 return false;
               } else {
                 if (!formats0.test(data)) {
                   validate10.errors = [{
                     instancePath,
                     schemaPath: "#/format",
                     keyword: "format",
                     params: {
                       format: "email"
                     },
                     message: "must match format "" + "email" + """
                   }];
                   return false;
                 }
               }
             }
           }
         } else {
           validate10.errors = [{
             instancePath,
             schemaPath: "#/type",
             keyword: "type",
             params: {
               type: "string"
             },
             message: "must be string"
           }];
           return false;
         }
       }
     }
     validate10.errors = vErrors;
     return errors === 0;
   };
 });

通过以上文件我们可以看到,ajv对我们定义好的shcma进行编译,编译之后生成了一个回调函数。在回调函数中对,定义好的规则进行判断处理。

首先是对type类型的判断处理,然后是字符串类型的最大长度、最小长度和正则的校验,最后是对format的规则校验。

如果,其中的一项不满足规则时,直接会走到errors里边,把错误信息进行处理输出。

总结

了解Ajv的的判断逻辑,先进行schema的定义,然后compile进行schema的编译、生成回调函数,最后输入data数据进行校验。

在我们定义好schema之后,在string类型中,他会按照先type、字符串最大长度、最小长度、正则判断和format的顺序进行,data的校验。

以上就是Ajv format校验使用示例分析的详细内容,更多关于Ajv format校验的资料请关注猪先飞其它相关文章!

原文出处:https://juejin.cn/post/7160484927572541477

标签:[!--infotagslink--]

您可能感兴趣的文章: