1. 1.验证枚举类型
  2. var schema = {
  3. "properties": {
  4. "data": {
  5. "type": "object",
  6. "required": ["code", "status", "message", "data", "token"],
  7. "properties": {
  8. "code": {
  9. "type": "number"
  10. },
  11. "status": {
  12. "type": "number",
  13. "enum": [0, 1] //枚举
  14. },
  15. "message": {
  16. "type": "string"
  17. },
  18. "data": {
  19. "type": "array", //数组
  20. items:{
  21. type:"number"
  22. }
  23. },
  24. "token": {
  25. "type": "string"
  26. }
  27. }
  28. }
  29. }
  30. };
  31. 2.验证email(formatminLengthminimumdefault)
  32. const Ajv = require('ajv');
  33.  
  34. let schema = {
  35. type: 'object',
  36. required: ['username', 'email', 'password'],
  37. properties: {
  38. username: {
  39. type: 'string',
  40. minLength: 4
  41. },
  42. email: {
  43. type: 'string',
  44. format: 'email'
  45. },
  46. password: {
  47. type: 'string',
  48. minLength: 6
  49. },
  50. age: {
  51. type: 'integer',
  52. minimum: 0
  53. },
  54. sex: {
  55. enum: ['boy', 'girl', 'secret'],
  56. default: 'secret'
  57. },
  58. }
  59. };
  60.  
  61. let ajv = new Ajv();
  62. let validate = ajv.compile(schema);
  63.  
  64. let valid = validate(data);
  65. if (!valid) console.log(validate.errors);
  66. 3.if/then/else
  67. {
  68. type: "object",
  69. if: {properties: {foo: {minimum: 10}}},
  70. then: {required: ["bar"]},
  71. else: {required: ["baz"]}
  72. }
  73. {
  74. type: "integer",
  75. minimum: 1,
  76. maximum: 1000,
  77. if: {minimum: 100},
  78. then: {multipleOf: 100},
  79. else: {
  80. if: {minimum: 10},
  81. then": {multipleOf: 10}
  82. }
  83. }
  84. 4.regex:
  85. const schema = {
  86. type: "object",
  87. properties: {
  88. foo: {type: "string", regexp: "/foo/i"},
  89. bar: {type: "string", regexp: {pattern: "bar", flags: "i"}},
  90. },
  91. }
  92. 5. Keywords for arrays
  93. const schema = {
  94. type: "array",
  95. uniqueItemProperties: ["id", "name"],
  96. }
  97.  
  98. const validData = [{id: 1}, {id: 2}, {id: 3}]
  99.  
  100. const invalidData1 = [
  101. {id: 1},
  102. {id: 1}, // duplicate "id"
  103. {id: 3},
  104. ]
  105.  
  106. const invalidData2 = [
  107. {id: 1, name: "taco"},
  108. {id: 2, name: "taco"}, // duplicate "name"
  109. {id: 3, name: "salsa"},
  110. ]
  111. 6.验证日期格式
  112. onst schema = {
  113. type: "object",
  114. dynamicDefaults: {
  115. ts: "datetime",
  116. r: {func: "randomint", args: {max: 100}},
  117. id: {func: "seq", args: {name: "id"}},
  118. },
  119. properties: {
  120. ts: {
  121. type: "string",
  122. format: "date-time",
  123. },
  124. r: {
  125. type: "integer",
  126. minimum: 0,
  127. exclusiveMaximum: 100,
  128. },
  129. id: {
  130. type: "integer",
  131. minimum: 0,
  132. },
  133. },
  134. }
  135.  
  136. const data = {}
  137. ajv.validate(data) // true
  138. 7. JSON data type
  139. ==Type can be: number, integer, string, boolean, array, object or null==
  140. 1.复合类型: {type: ["number", "string"]}
  141. 2. nullable:This keyword can be used to allow null value in addition to the defined type.
  142. {
  143. "type": "string",
  144. "nullable": true
  145. },
  146. {
  147. "type": ["string", "null"]
  148. }
  149. 3.Keywords for numbers:
  150. maximum / minimum and exclusiveMaximum / exclusiveMinimum
  151.  
  152. schema: {type: "number", not: {minimum: 3}}
  153. 4.Keywords for strings
  154. maxLength / minLength
  155. schema: {type: "string", maxLength: 5}
  156. {type: "string", minLength: 2}
  157. 3.pattern: 内容为“正则”
  158. schema: {type: "string", pattern: "[abc]+", enum: ["foo", "bar"]}
  159. 5.format
  160. The value of the keyword should be a string. The data to be valid should match the format with this name.
  161.  
  162. Ajv does not include any formats, they can be added with ajv-formats (opens new window) plugin.
  163.  
  164. Example
  165.  
  166. schema: {type: "string", format: "ipv4"}
  167. 6.验证array
  168. 1. {
  169. "type": "array",
  170. items:{
  171. type:'number'
  172. },
  173. "minItems": 3,
  174. "maxItems": 5,
  175. "uniqueItems": true
  176. }
  177.  
  178. 2.{
  179. type: "array",
  180. items: [{type: "integer"}, {type: "string"}]
  181. }

  本文来自ajv官网整理

ajv参数验证的更多相关文章

  1. C# 中参数验证方式的演变

    一般在写方法的时候,第一步就是进行参数验证,这也体现了编码者的细心和缜密,但是在很多时候这个过程很枯燥和乏味,比如在拿到一个API设计文档的时候,通常会规定类型参数是否允许为空,如果是字符可能有长度限 ...

  2. Java和C#下的参数验证

    参数的输入和验证问题是开发时经常遇到的,一般的验证方法如下: public bool Register(string name, int age) { if (string.IsNullOrEmpty ...

  3. DUBBO参数验证

    public class ValidationParameter implements Serializable {           private static final long seria ...

  4. ASP.NET WebAPI 11 参数验证

    在绑定完Action的所有参数后,WebAPI并不会马上执行该方法,而要对参数进行验证,以保证输入的合法性. ModelState 在ApiController中一个ModelState属性用来获取参 ...

  5. C# 中参数验证方式

    C# 中参数验证方式 一般在写方法的时候,第一步就是进行参数验证,这也体现了编码者的细心和缜密,但是在很多时候这个过程很枯燥和乏味,比如在拿到一个API设计文档的时候,通常会规定类型参数是否允许为空, ...

  6. 自动化CodeReview - ASP.NET Core请求参数验证

    自动化CodeReview系列目录 自动化CodeReview - ASP.NET Core依赖注入 自动化CodeReview - ASP.NET Core请求参数验证 参数验证实现 在做服务端开发 ...

  7. Kotlin + Spring Boot 请求参数验证

    编写 Web 应用程序的时候,经常要做的事就是要对前端传回的数据进行简单的验证,比如是否非空.字符长度是否满足要求,邮箱格式是否正确等等.在 Spring Boot 中,可以使用 Bean Valid ...

  8. 参数验证 @Validated 和 @Valid 的区别

    来源:blog.csdn.net/qq_27680317/article/details/79970590 整编:Java技术栈(公众号ID:javastack) Spring Validation验 ...

  9. Dubbo -- 系统学习 笔记 -- 示例 -- 参数验证

    示例 想完整的运行起来,请参见:快速启动,这里只列出各种场景的配置方式 参数验证 参数验证功能是基于JSR303实现的,用户只需标识JSR303标准的验证Annotation,并通过声明filter来 ...

  10. ASP.NET Web API 2 之参数验证

    Ø  前言 目前 C# 比较流行使用 ASP.NET Web API 来承载 Web 接口,提供与客户端之间的数据交互,现在的版本已经是 2.0 了.既然是接口就少不了对输入参数的验证,所以本文主要探 ...

随机推荐

  1. LinkedHashmap简要说明

    https://segmentfault.com/a/1190000012964859 LinkedHashMap 继承自 HashMap,在 HashMap 基础上,通过维护一条双向链表,解决了 H ...

  2. 移除链表元素&反转链表&设计链表

    一.移除链表元素 203.移除链表元素 leetcode链接 1.方法概述 带傀儡节点的方法: 创建一个傀儡节点puppet来充当该链表的假头节点,当真正的头结点head不为null时,且在真正的头节 ...

  3. FineUI通过js事件条用后台方法实现弹窗

    一.html标签的onclick事件 <button type="button" class="btnUpdate" onclick="< ...

  4. 无法从“System.ReadOnlyMemory<byte>”转换为“byte[]”

    1.问题复现 RabbitMQ的官方示例:RabbitMQ消费端(接收端)获取消息时抛出异常,具体代码如下 var consumer = new EventingBasicConsumer(chann ...

  5. Ubuntu/linux下最强大的下载工具-aria2

    aria2 是 Linux 下一个不错的高速下载工具 .由于它具有分段下载引擎,所以支持从多个地址或者从一个地址的多个连接来下载同一个文件.这样自然就大大加快了文件的下载速 度.aria2 也具有断点 ...

  6. 安装redhat6.10 出现的问题

    安装redhat6.10 操作系统不定时重启情况说明   曾出现报错如下: 在UEFI模式下安装RHEL6.10,安装完毕后系统第一次重启无法进入操作系统,同时屏幕上出现错误提示: Invalid m ...

  7. Linux下文件实时自动同步备份

    转载简书: https://www.jianshu.com/p/fc2f3ec661c0

  8. HACLABS: NO_NAME

    HACLABS: NO_NAME 目录 HACLABS: NO_NAME 1 信息收集 1.1 端口扫描 1.2 后台目录扫描 1.2.1 目录分析 2 命令注入利用 2.1 尝试反弹Shell 2. ...

  9. CCRD_TOC_2008年第11期

    中信国健临床通讯 2008年第11期(总第24期) 目 录   脊柱关节炎 1. 一项多中心.大型.随机.双盲.对照试验证实依那西普治疗AS的疗效优于柳氮磺吡啶 Braun J, et al. ACR ...

  10. 基线MRI与CRP是依那西普对nr-axSpA的疗效预测因素

    基线MRI与CRP是依那西普对nr-axSpA的疗效预测因素 EULAR2015; PresentID: SAT0258 BASELINE MRI/CRP AS PREDICTORS OF RESPO ...