我正在使用json-schema和一个名为“ feathers-plus”的nodeJs后端,该后端使用流行的ajv验证数据。
我有两个对象-> ObjectA&ObjectB,其中ObjectA是父对象。 ObjectA包含ObjectB的集合,这些集合具有由patternProperties定义的任意名称,并且是对象:
exchanges: {
type: 'object',
patternProperties: {
"^[A-Za-z]+$": {
type: 'object'
}
},
additionalProperties: false
},
ObjectB(在我的情况下是交换)在顶层使用此任意键名称开头,然后自身包含更多属性(在第二层等)。
问题是我不知道如何定义这些任意键名。我知道如何使用上面的示例在第二层或更深入地定义它来进行交流。但最重要的是,我想我不能写这样的东西:
"^[A-Za-z]+$": {
type: 'object'
}
借助feathers-plus,我们可以在服务中分离逻辑对象。在我的情况下,顶级服务称为“配置”,子级称为“交换”。
任何帮助将不胜感激。
谢谢!
编辑:根据要求,我发布了两个对象的当前架构:
父对象A->
let schema = {
title: 'Configs',
description: 'Configs database.',
required: [
'name',
'bot',
'GUI',
'ws',
'createdAt',
'updatedAt'
],
uniqueItemProperties: [
],
properties: {
id: { type: 'ID' },
name: {},
pairs: {
type: 'object',
patternProperties: {
"^[A-Za-z]+$": {
type: 'object'
}
},
additionalProperties: false
},
// THIS IS THE CONTAINER FOR EXCHANGES OBJECTS
exchanges: {
type: 'object',
patternProperties: {
"^[A-Za-z]+$": {
type: 'object'
}
},
additionalProperties: false
},
// END
bot: {
type: 'object',
properties: {
debug: { type: 'boolean' },
gui: { type: 'boolean' },
period_storage_ticker: { type: 'number' },
interval_ticker_update: { type: 'number' },
timeout_buy: { type: 'number' },
timeout_sell: { type: 'number' },
WATCH_MODE: { type: 'boolean' },
TELEGRAM_ENABLED: { type: 'boolean' },
TELEGRAM_NICK: {},
TOKEN: {},
chat_id: { type: 'number' }
},
required: [
'debug',
'gui',
'period_storage_ticker',
'interval_ticker_update',
'timeout_buy',
'timeout_sell',
'WATCH_MODE',
'TELEGRAM_ENABLED',
'TELEGRAM_NICK',
'TOKEN',
'chat_id'
]
},
GUI: {
type: 'object',
properties: {
enabled: { type: 'boolean' },
start: { type: 'boolean' },
port: { type: 'number' },
https: { type: 'boolean' },
key: {},
cert: {},
networktraffic: { type: 'boolean' },
authentication: {
type: 'object',
properties: {
login: { type: 'boolean' },
twoFA: { type: 'boolean' }
},
required: [
'login',
'twoFA'
]
},
notifications: {
type: 'object',
properties: {
trade: { type: 'boolean' },
callback: { type: 'boolean' },
error: { type: 'boolean' }
},
required: [
'trade',
'callback',
'error'
]
}
},
required: [
'enabled',
'start',
'port',
'https',
'key',
'cert',
'networktraffic',
'authentication',
'notifications'
]
},
ws: {
type: 'object',
properties: {
port: { type: 'number' },
clientport: { type: 'number' },
hostname: {}
},
required: [
'port',
'clientport',
'hostname'
]
}
createdAt: {},
updatedAt: {}
}
}
子对象B->
let schema = {
title: 'Exchanges',
description: 'Exchanges database.',
// MY CURRENT EDIT (NOT WORKING)
type: 'object',
patternProperties: {
"^[A-Za-z]+$": { type: 'boolean' }
},
additionalProperties: false,
// END
required: [
],
uniqueItemProperties: [
],
properties: {
},
}
如果我正在发出请求并尝试传递任何内容,则该请求有效且未进行验证。在更深层次上设置的相同patternProperties完全没有问题,并且已正确验证。