前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >jsonschema校验json数据_xml schema校验

jsonschema校验json数据_xml schema校验

作者头像
全栈程序员站长
发布2022-09-20 11:06:50
2.4K0
发布2022-09-20 11:06:50
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是你们的朋友全栈君。

ajv 使用

在使用前,需要知道 json-schema 是什么。

json-schema

json-schema 是一个用来描述json 数据格式。

ajv

ajv 是一个校验 json-schema 的数据格式工具(也有其他的,这里具体讲解 ajv)。

ajv 引入

代码语言:javascript
复制
import Ajv from "ajv";
const options = {}; // 具体的配置
const ajv = new Ajv(options); // 某些情况下,需要改为 new Ajv.default()

// 开启校验
const isValid = ajv.validate(schemas, data); // schemas 具体配置,data数据
if (!iaValid) {
  throw new Error(ajv.errorsText());
}

json-schema 默认含有下面 6 种数据结构string ,number, object ,array ,boolean ,null

通过 ajv-keywords 可以进行扩展更多类型。

同时支持自定义类型

代码语言:javascript
复制
class MyClass {}

const instanceofDef = require("ajv-keywords/dist/definitions/instanceof");
instanceofDef.CONSTRUCTORS.MyClass = MyClass;
ajv.validate({ instanceof: "MyClass" }, new MyClass()); // true

文档太枯燥,这些基本知识又不想炒闲饭式地再叙述一遍,举几个示例吧,简洁明了,走起。

基本类型

代码语言:javascript
复制
// 规定校验类型
const schema = {
  type: "object",
  properties: {
    // 属性
    get: {
      type: "object", // 类型
      properties: {
        url: {
          type: "string",
        },
        method: {
          type: "string",
        },
      },
      required: ["url"], // 必须包含 url 属性
    },
  },
};

// 具体数据
const data = {
  get: {
    url: "http://localhost:8080/get",
  },
};

重复代码块如何处理

代码语言:javascript
复制
// 规定校验类型
 const schema = {
   type: 'object',
   properties: { // 属性
   get: {
+	 $id: '#getType',
	 type: 'object', // 类型
     properties: {
	   url: {
		  type: 'string'
	   },
	   method: {
		 type: 'string'
	   },
	 },
 	 required: ['url'] // 必须包含 url 属性
   },
   put: {
-	 type: 'object', // 类型
-	 properties: {
-		url: {
-		  type: 'string'
-		},
-		method: {
-		  type: 'string'
-		},
-	  },
-     required: ['url'] // 必须包含 url 属性
+     $ref: '#getType' // 关联上面get,与之属性保持一致
    },
    delete: {
	  $ref: '#getType'
    }
  }
}

不支持的格式如何处理

由于 json-schemas 不支持 js 里复杂数据类型的具体类型,比如 function, date …,因而需要引入 ajv-keywords 进行额外补充,但是类型只支持上面列出的类型。

代码语言:javascript
复制
import Ajv from "ajv";
import AjvKeywords from "ajv-keywords";

const ajv = new Ajv();
AjvKeywords(ajv, ["typeof", "instanceof"]); // 除了 type 定义类型外,还可以通过 typeof,instanceof

// 规定校验类型
const schema = {
  type: "object",
  properties: {
    // 属性
    get: {
      type: "object", // 类型
      properties: {
        url: {
          type: "string",
        },
        method: {
          type: "string",
        },
      },
      required: ["url"], // 必须包含 url 属性
    },
    getMethod: {
      instanceof: "Function", // typeof 类似,只是支持的类型不同
    },
    list: {
      instanceof: ["Function", "Array"],
    },
  },
};

const data = {
  get: {
    url: "http://localhost:8080/get",
  },
  getMethod() {},
  list: [],
};

通过上面的方式,便可以对日常使用 json 格式的数据进行校验,保证在处理数据前,拿到的数据是有效的,可以避免很多繁琐的数据格式校验,而且也有了一个统一的规则。

参考链接

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/167483.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • ajv 使用
    • json-schema
      • ajv
        • ajv 引入
        • 基本类型
        • 重复代码块如何处理
        • 不支持的格式如何处理
      • 参考链接
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档