简体   繁体   English

在 JSON 模式中使用 CONST

[英]Using CONST in JSON Schema

I'm using JSON schema and Ajv to make sure that the selected JSON is valid in the system.我正在使用 JSON 模式和 Ajv 来确保所选的 JSON 在系统中有效。
I've succeeded in checking the maximum length of the string by writing like this: "maxLength": 20我已经成功地通过这样写来检查字符串的最大长度: "maxLength": 20

But I don't want to repeat myself.但我不想重复自己。 DRY.干燥。
Is there any way to use const like this?有没有办法像这样使用const?

const MAX_LENGTH = 20
"maxLength": MAX_LENGTH

The same number is also used in reducer.减速器中也使用了相同的数字。 I tried $ref and succeeded, but it's not enough.我尝试了 $ref 并成功了,但这还不够。

My code is as follows:我的代码如下:

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"
        }
    }
}

You can make a definition for a new string type, and then reuse it with $ref :你可以定义一个新的字符串类型,然后用$ref重用它:

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

I've found a way by importing an object instead of a json file.我找到了一种通过导入对象而不是 json 文件的方法。 It's easy to put a variable in an object.将变量放入对象中很容易。

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