惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

有赞技术团队
有赞技术团队
M
MIT News - Artificial intelligence
Hugging Face - Blog
Hugging Face - Blog
博客园 - 聂微东
量子位
S
SegmentFault 最新的问题
V
Visual Studio Blog
博客园 - 【当耐特】
Apple Machine Learning Research
Apple Machine Learning Research
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
小众软件
小众软件
Stack Overflow Blog
Stack Overflow Blog
Vercel News
Vercel News
D
Docker
J
Java Code Geeks
博客园 - 三生石上(FineUI控件)
博客园 - Franky
Recent Announcements
Recent Announcements
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
MongoDB | Blog
MongoDB | Blog
D
DataBreaches.Net
Y
Y Combinator Blog
云风的 BLOG
云风的 BLOG
V
V2EX

时间的朋友

Windows 命令行密码重置 Anaconda安装 typescript 注解解读1 Konvajs Shape加载自定义图片 sshpass 使用 why-is-node-running webgl笔记 SharedArrayBuffer is not defined blender 常用快捷键 | 时间的朋友 vue -- v3.4commit提交记录2 vue2 升级vue3报错问题整理 着色器 expressjs 源码 hyper-V arch linux 网络配置 element input数字格式化 three 拼接货架 WebAudio笔记 Windows nginx重启bat脚本 vue -- v3.4commit提交记录 URI malformed vue3 -- Class 对象在组件中使用范例 | 时间的朋友 ruby 安装和升级 element-plus 老版本cascader使用卡死问题 vue3 内置Transition组件 | 时间的朋友 前端memo的实现 | 时间的朋友 Vue -- vue-class-component源码 | 时间的朋友 linux 优化脚本 typescript 装饰器 | 时间的朋友 microbundle 源码 | 时间的朋友 WSL2问题解决WslRegisterDistribution failed with error: 0x800701bc
webpack5 schema-utils | 时间的朋友
2022-09-29 · via 时间的朋友

Published: · LastMod: September 29, 2022 · 500 words

schema-utils 🔗

webpack中工具类schema-utils 🔗

yarn add -D schema-utils

webpack中调用 schema-utils 校验配置对象

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
// options.json
{
  "type": "object",
  "properties": {
    "name": {
      "type": "boolean"
    }
  },
  "required": ["name"],
  "additionalProperties": false
}

schema-utils底层依赖ajv js

ajv 🔗

ajv支持7种默认基本数据类型

  • number

  • interger

  • string

  • boolean

  • array

  • null

  • object

    • maxProperties / minProperties:限定对象支持的最多、最少属性数量;

    • required:声明哪些属性不可为空,例如 required = ['name', 'age'] 时,传入的值必须至少提供 name/age 属性;

    • properties:定义特定属性的 Schema 描述,与 arrayitems 属性类似,支持嵌套规则,例如:

  • patternProperties: 支持属性名正则表达式

  • additionalProperties

  • enum: 枚举数组, 必须相等

  • const 静态变量

  • not指令: 数值必须不能等于这个值

  • anyof指令: 必须满足条件之一

  • oneof指令: 数值必须满足且只能满足条件之一

  • allof指令: 必须满足所有条件

  • if/then/else: 条件复合条件

异步校验 🔗

shema必须加上$async属性

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
jv.addKeyword({
  keyword: "idExists",
  async: true,
  type: "number",
  validate: checkIdExists,
})

async function checkIdExists(schema, data) {
  // .....
}

const schema = {
  $async: true,
  properties: {
    userId: {
      type: "integer",
      idExists: {table: "users"},
    },
    postId: {
      type: "integer",
      idExists: {table: "posts"},
    },
  },
}

const validate = ajv.compile(schema)

validate({userId: 1, postId: 19})
  .then(function (data) {
    // ....
  })

最终在校验对象时,validate会返回一个Promise对象