AJV 如何将一个模式包含在另一个模式中

问题描述 投票:0回答:1

我想在另一种模式中使用一种模式,而无需手动输入字段。

我可以像这样对数组执行此操作:

import schemaAccountInfo from './schemaAccountInfo';
const schemaAccount = {
  type: 'array',
  items: schemaAccountInfo,
}

似乎无法想象它只是一个物体:

import schemaAccountInfo from './schemaAccountInfo';

const schemaAccount = {
  type: 'object',
  properties: {
    id: { type: 'string' },
    active: { type: 'boolean' },
    accountInfo: {
      type: 'object',
      properties: schemaAccountInfo, // problem here
    },
  },
  required: [
    'id',
  ],
  additionalProperties: false,
};
javascript object schema ajv
1个回答
0
投票

您的数组版本还应该使用

$ref
语法。

import schemaAccountInfo from './schemaAccountInfo';
const schemaAccount = {
  type: 'array',
  items: 
    $ref: schemaAccountInfo,
}
import schemaAccountInfo from './schemaAccountInfo';

const schemaAccount = {
  type: 'object',
  properties: {
    id: { type: 'string' },
    active: { type: 'boolean' },
    accountInfo: {
      $ref: schemaAccountInfo, // problem here
    },
  },
  required: [
    'id',
  ],
  additionalProperties: false,
};
© www.soinside.com 2019 - 2024. All rights reserved.