Use ajv

Before you can use it, you need to know what JSON-schema is.

json-schema

Json-schema is a format used to describe JSON data.

ajv

Ajv is a data format tool that validates JSON-Schema (there are others, too, but AJV is covered here).

Introduced ajv

import Ajv from "ajv";
const options = {}; // Specify the configuration
const ajv = new Ajv(options); // In some cases, change to new ajv.default ()

// Enable verification
const isValid = ajv.validate(schemas, data); // schemas Specify configurations, data data
if(! iaValid) {throw new Error(ajv.errorsText());
}
Copy the code

By default, jSON-schema contains the following six data structures: String,number, Object,array, Boolean, and NULL.

More types can be extended with ajV-keywords.

Custom types are also supported

class MyClass {}

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

The documentation is too boring, and I don’t want to go over the basics again, just to give you a few examples, simple and clear. Let’s go.

Basic types of

// Specify the verification type
const schema = {
  type: "object".properties: {
    / / property
    get: {
      type: "object"./ / type
      properties: {
        url: {
          type: "string",},method: {
          type: "string",}},required: ["url"].// Must contain the URL attribute,}}};// Specific data
const data = {
  get: {
    url: "http://localhost:8080/get",}};Copy the code

What about duplicate code blocks

Const schema = {type: 'object', properties: {// get: {+ $id: '#getType',Type: 'object', // Properties: {URL: {type: 'string'}, method: {type: 'string'},}, required: ['url'] // Must contain url attribute}, put: {- type: 'object', // Type
- properties: {
- url: {
- type: 'string'
-},
- method: {
- type: 'string'
-},
-},
- Required: [' URL '] // Url attributes must be included
+ $ref: '#getType
    },
    delete: {
	  $ref: '#getType'
    }
  }
}
Copy the code

How to deal with unsupported formats

Because JSON-schemas do not support specific types of complex data types in JS, such as function, date… , so you need to introduce additional ajV-keywords, but only the types listed above are supported.

import Ajv from "ajv";
import AjvKeywords from "ajv-keywords";

const ajv = new Ajv();
AjvKeywords(ajv, ["typeof"."instanceof"]); // In addition to type, you can also use typeof, instanceof

// Specify the verification type
const schema = {
  type: "object".properties: {
    / / property
    get: {
      type: "object"./ / type
      properties: {
        url: {
          type: "string",},method: {
          type: "string",}},required: ["url"].// Must contain the URL attribute
    },
    getMethod: {
      instanceof: "Function".// Typeof is similar, but supports different types
    },
    list: {
      instanceof: ["Function"."Array"],}}};const data = {
  get: {
    url: "http://localhost:8080/get",},getMethod() {},
  list: [],};Copy the code

By using the above method, you can verify the daily USE of JSON data to ensure that the data obtained before processing is valid, avoiding a lot of tedious data format verification, and also has a unified rule.

Refer to the link

  • Jane’s book
  • Ajv Chinese