繁体   English   中英

在 JSON 模式中使用 CONST

[英]Using CONST in JSON Schema

我正在使用 JSON 模式和 Ajv 来确保所选的 JSON 在系统中有效。
我已经成功地通过这样写来检查字符串的最大长度: "maxLength": 20

但我不想重复自己。 干燥。
有没有办法像这样使用const?

const MAX_LENGTH = 20
"maxLength": MAX_LENGTH

减速器中也使用了相同的数字。 我尝试了 $ref 并成功了,但这还不够。

我的代码如下:

import schema from './schema.json'

export const isValid = ( importedJson ) => {
  const ajv = new Ajv()

  ajv.addMetaSchema(require('ajv/lib/refs/json-schema-draft-06.json'))

  const validate = ajv.compile( schema )

  return validate( importedJson )
}
// schema.json
{
    "$schema": "http://json-schema.org/draft-06/schema#",
    "$ref": "#/definitions/Schema",
    "definitions": {
        "Schema": {
            "type": "object",
            "additionalProperties": false,
            "properties": {
                "groups": {
                    "type": "array",
                    "items": {
                        "$ref": "#/definitions/Group"
                    }
                }
            },
            "required": [
                "groups"
            ],
            "title": "Schema"
        },
        "Group": {
            "type": "object",
            "additionalProperties": false,
            "properties": {
                "name": {
                    "type": "string",
                    "maxLength": 20
                },
                "users": {
                    "type": "array",
                    "items": {
                        "$ref": "#/definitions/User"
                    }
                }
            },
            "required": [
                "name",
                "users"
            ],
            "title": "Group"
        },
        "User": {
            "type": "object",
            "additionalProperties": false,
            "properties": {
                "name": {
                    "type": "string",
                    "maxLength": 20
                }
            },
            "required": [
                "name"
            ],
            "title": "User"
        }
    }
}

你可以定义一个新的字符串类型,然后用$ref重用它:

  "definitions": {
    "short_string": {
      "type": "string",
      "maxLength": 20
    },
    ...
  },
  "properties": {
    "name": { "$ref": "#/definitions/short_string" },
    ...
  }

我找到了一种通过导入对象而不是 json 文件的方法。 将变量放入对象中很容易。

import Ajv from 'ajv'
import { schema } from './schema'

export const isValid = ( importedJson ) => {
  const ajv = new Ajv()

  return ajv.validate( schema, importedJson )
}
// schema.js
import { MAX_STRING_LENGTH } from '../consts'

export const schema = {
    "$ref": "#/definitions/Schema",
    "definitions": {
        "Schema": {
            "type": "object",
            "additionalProperties": false,
            "properties": {
                "groups": {
                    "type": "array",
                    "items": {
                        "$ref": "#/definitions/Group"
                    }
                }
            },
            "required": [
                "groups"
            ],
            "title": "Schema"
        },
        "Group": {
            "type": "object",
            "additionalProperties": false,
            "properties": {
                "name": {
                    "type": "string",
                    "maxLength": MAX_STRING_LENGTH
                },
                "users": {
                    "type": "array",
                    "items": {
                        "$ref": "#/definitions/User"
                    }
                }
            },
            "required": [
                "name",
                "users"
            ],
            "title": "Group"
        },
        "User": {
            "type": "object",
            "additionalProperties": false,
            "properties": {
                "name": {
                    "type": "string",
                    "maxLength": MAX_STRING_LENGTH
                }
            },
            "required": [
                "name"
            ],
            "title": "User"
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM