jsonschema 引用问题
发布于 3 个月前 作者 ddzyan 928 次浏览 来自 问答

问题: 我现在 balance.json 文件中的 address 引用 address.json 定义的规则,我尝试了如下的修改 “修改后的balance.json” ,但是找不到 address 规则,感谢大神指导下,网上没找到相关操作文档

修改后的balance.json

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "title": "getBalance",
  "description": "Parameters for getBalances",
  "type": "object",
  "properties": {
    "address": {
      "type": "array",
      "items": {
        "type": "object",
        "$ref":"address"
      },
      "minItems": 1,
      "uniqueItems": true
    },
    "name": { "type": "string" },
    "votes": { "type": "integer", "minimum": 1 }
  },
  "additionalProperties": true,
  "required": ["address"]
}

balance.json

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "title": "getBalance",
  "description": "Parameters for getBalances",
  "type": "object",
  "properties": {
    "address": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "lines": {
            "type": "array",
            "items": { "type": "string" }
          },
          "zip": { "type": "string" },
          "city": { "type": "string" },
          "country": { "type": "string" }
        },
        "required": ["country"]
      },
      "minItems": 1,
      "uniqueItems": true
    },
    "name": { "type": "string" },
    "votes": { "type": "integer", "minimum": 1 }
  },
  "additionalProperties": true,
  "required": ["address"]
}

address.json

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "title": "address",
  "description": " ",
  "type": "string",
  "format": "address",
  "link": "address",
  "properties": {
    "lines": {
      "type": "array",
      "items": { "type": "string" }
    },
    "zip": { "type": "string" },
    "city": { "type": "string" },
    "country": { "type": "string" }
  },
  "required": ["country"]
}

4 回复

别沉了啊

我在两年前是这样用的,当时用的模块是 “ajv”: “^4.11.5”

const point = {
  type: 'object',
  properties: {
    id: { type: 'string' },
    point: {
      type: 'object',
      properties: {
        left: { type: 'number' },
        top: { type: 'number' },
      },
      required: ['left', 'top'],
    },
  },
  required: ['id', 'point'],
};

export const schema = {
  $schema: 'http://json-schema.org/draft-04/schema#',
  title: 'xxxxx',
  description: 'xxxx',
  type: 'object',
  properties: {
    points: {
      type: 'array',
      items: point, // 直接引用
    },
  },
  required: ['points'],
};

@zengming00 感谢回复,但是这没有解决我的问题…

参考官方标准:Schema References

  "address": {
    "type": "array",
    "items": {
      "$ref":"address.json"
    },
    "minItems": 1,
    "uniqueItems": true
  }
回到顶部