使用ajv校验json schema
使用ajv-errors生成json-schema错误信息
ajv-errors给Json-schema添加了一个errorMessage
关键字,通过这个关键字来输出错误信息
const Ajv = require('ajv')
const ajv = new Ajv({ allErrors: true, jsonPointers: true })
require('ajv-errors')(ajv)
//一个json schema规范的对象
const schema = {}
//待校验的json对象
const data = {}
const validate = ajv.compile(schema)
validate(data) //返回boolean,是否校验通过
validate.errors //错误详细信息
用法
单个错误信息
用一条消息替换当前schema和sub schema中的所有错误
var schema = {type: 'object',required: ['foo'],properties: {foo: { type: 'integer' }},additionalProperties: false,errorMessage: 'should be an object with an integer property foo only' };
关键字错误
var schema = {type: 'object',required: ['foo'],properties: {foo: { type: 'integer' }},additionalProperties: false,errorMessage: {type: 'should be an object', // will not replace internal "type" error for the property "foo"required: 'should have property foo',additionalProperties: 'should not have properties other than foo'} };
required
和dependencies
可以使用对象指定特定的属性的错误信息。示例如下:var schema = {type: 'object',required: ['foo', 'bar'],properties: {foo: { type: 'integer' },bar: { type: 'string' }},errorMessage: {type: 'should be an object', // will not replace internal "type" error for the property "foo"required: {foo: 'should have an integer property "foo"',bar: 'should have a string property "bar"'}} };
属性的错误信息
无论属性是在哪里创建的
var schema = {type: 'object',required: ['foo', 'bar'],allOf: [{properties: {foo: { type: 'integer', minimum: 2 },bar: { type: 'string', minLength: 2 }},additionalProperties: false}],errorMessage: {properties: {foo: 'data.foo should be integer >= 2',bar: 'data.bar should be string with length >= 2'}} };
默认错误信息
当errorMessage是一个对象时,可以指定一个默认错误来兜底错误。
var schema = {type: 'object',required: ['foo', 'bar'],allOf: [{properties: {foo: { type: 'integer', minimum: 2 },bar: { type: 'string', minLength: 2 }},additionalProperties: false}],errorMessage: {type: 'data should be an object',properties: {foo: 'data.foo should be integer >= 2',bar: 'data.bar should be string with length >= 2'},_: 'data should have properties "foo" and "bar" only'} };
模板语法
配合json pointer可以展示当前json的实际值
var schema = {"type": "object","properties": {"size": {"type": "number","minimum": 4}},"errorMessage": {"properties": {"size": "size should be a number bigger or equal to 4, current value is ${/size}"}} }validate({size:1}) //validate.error是个对象。其中会有错误信息如下。error具体结构参考https://www.npmjs.com/package/ajv-errors //"size should be a number bigger or equal to 4, current value is 1"
可以将minimum设置成一个变量,然后errorMessage配合变量和es6模板字符串达到改变变量,自动更改校验提示。
注意:json pointer需要使用\转义
//同上一个校验 const minimum = 4 var schema = {"type": "object","properties": {"size": {"type": "number",minimum}},"errorMessage": {"properties": {"size": `size should be a number bigger or equal to ${minimum}, current value is \${/size}`}} }
参考
- ajv-errors readme
- ajv
使用ajv校验json schema相关推荐
- 如何快速写出Json Schema,校验Json Schema
本文首发于微信公众号: [软测小生] 得到一个Json文件,如何快速的去测试呢? 难道是一个个节点的去验证吗?那显然效率太低了. 一般推荐使用Json Schema(一种Json的数据结构定义)去校验 ...
- json schema多种形式_如何快速写出Json Schema,校验Json Schema
得到一个Json文件,如何快速的去测试呢? 难道是一个个节点的去验证吗?那显然效率太低了. 一般推荐使用Json Schema(一种Json的数据结构定义)去校验. 对于JsonSchema,有很多种 ...
- 3 分钟了解 JSON Schema
大家好,我不是鱼皮. 幸运又不幸,我是一名程序员,他也是一名程序员. 周末,我在开发网站,他在开发游戏,两个人一起写代码,一起写 Bug 头秃,竟也有了一丝别样的浪漫,好不自在! 今天,他遇到了一个后 ...
- JSON Schema定义 +Networknt validator格式校验
一.介绍 JSON Schema是基于JSON的格式的规范,用于定义JSON数据的结构. 官网:https://ajv.js.org/json-schema.html 在线验证:https://www ...
- Vue基于JSON Schema生成表单和数据校验
基于 Vue.js.JSON Schema 和 ElementUi 快速生成表单,支持自定义组件,支持自定义错误提示配置和校验规则... 源码:vue-json-schema-form github ...
- 服务端json参数校验神器Json Schema
目录 目录 json简介 服务端校验参数需求分析 json参数检验简单而繁琐方式 Json Schema Json Schema 入门 Json Schema 表达式 string Numeric t ...
- 如何利用JSON Schema校验JSON数据格式
最近笔者在工作中需要监控一批http接口,并对返回的JSON数据进行校验.正好之前在某前端大神的分享中得知这个神器的存在,调研一番之后应用在该项目中,并取得了不错的效果,特地在此分享给各位读者. 什么 ...
- JSON Schema校验数据
参考 JSON Schema 规范(中文版)官方网站JSON Schema 对于数据对接系统来说,接口的数据入参校验尤为重要,使用javax.validation相关注解进行校验对于java对象的关联 ...
- json Schema 数据校验工具
JSON Schema官网 文章目录 1:JSON Schema简介 1.1:基础知识 1.2:定义关键字解释 1:$schema 2:type支持数据类型 2.1:string字符串 1:正则表达式 ...
最新文章
- Redis之intset(整数集合)
- Greenplum【集群搭建 01】局域网 CentOS 7.9.2009 环境 GreenPlum 6.13.0 集群规划+配置+安装+内核参数调整(应用实例分享)
- php make,安装PHP出现make:
- mysql 用户 类别_从mysql里读取用户类型
- Node.js学习(篇章一)
- 极验创始人吴渊:恶意流量威胁新趋势,洞察网络黑产3大核心本质
- Pentium II Pentium III架构/微架构/流水线 (2) - P6详解 - 前端(指令预取/译码/动态分支预测静态分支预测)
- validate针对checkbox、radio、select标签的验证
- 安装linux系统后修改默认开机启动项
- 谁手握账本?趣讲 ZK 的内存模型
- 冰点文库下载器绿色版V3.2.15(0914)
- 大一html网页制作PPT,HTML网页制作.ppt
- SpringBoot整合腾讯云COS
- 圣诞老人python代码_用Python画圣诞老人的N种方法
- shell执行curl_Linux curl命令详解
- A040_SpringMVC进阶_JSON
- 51单片机DS18B20温度报警器
- Python表白代码:“山河远阔 人间烟火无一是你 无一不是你”(附源码)
- 【全套】Android零基础入门教程(知识精讲+强化实战)
- python 计算器 loop_Python 计算器