使用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 //錯誤詳細信息

用法

  1. 單個錯誤信息

    用一條消息替換當前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'
    };
    
  2. 關鍵字錯誤

    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'
      }
    };
    

    requireddependencies可以使用對象指定特定的屬性的錯誤信息。示例如下:

    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"'
        }
      }
    };
    
  3. 屬性的錯誤信息

    無論屬性是在哪裏創建的

    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'
        }
      }
    };
    
  4. 默認錯誤信息

    當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'
      }
    };
    
  5. 模板語法

    配合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}`
        }
      }
    }
    

參考

  1. ajv-errors readme
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章