简体   繁体   中英

Using CONST in JSON Schema

I'm using JSON schema and Ajv to make sure that the selected JSON is valid in the system.
I've succeeded in checking the maximum length of the string by writing like this: "maxLength": 20

But I don't want to repeat myself. DRY.
Is there any way to use const like this?

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.

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 :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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