在 JSON 模式中使用 CONST
Using CONST in JSON Schema
我正在使用 JSON 架构和 Ajv 来确保所选 JSON 在系统中有效。
我已经通过这样写成功地检查了字符串的最大长度:
"maxLength": 20
但我不想重复自己。干
有没有办法像这样使用const?
const MAX_LENGTH = 20
"maxLength": MAX_LENGTH
同样的数字也用在reducer中。
我尝试了 $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"
}
}
}
我找到了一种导入对象而不是 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"
}
}
}
您可以定义一个新的字符串类型,然后再用 $ref
:
"definitions": {
"short_string": {
"type": "string",
"maxLength": 20
},
...
},
"properties": {
"name": { "$ref": "#/definitions/short_string" },
...
}
我正在使用 JSON 架构和 Ajv 来确保所选 JSON 在系统中有效。
我已经通过这样写成功地检查了字符串的最大长度:
"maxLength": 20
但我不想重复自己。干
有没有办法像这样使用const?
const MAX_LENGTH = 20
"maxLength": MAX_LENGTH
同样的数字也用在reducer中。 我尝试了 $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"
}
}
}
我找到了一种导入对象而不是 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"
}
}
}
您可以定义一个新的字符串类型,然后再用 $ref
:
"definitions": {
"short_string": {
"type": "string",
"maxLength": 20
},
...
},
"properties": {
"name": { "$ref": "#/definitions/short_string" },
...
}