我正在设计一个使用JSON Schema建模的Web应用程序。我正在尝试创建一个带有文本区域和复选框的页面。文本区域是解释我喜欢披萨的原因。如果用户单击复选框,则表示他们确认不喜欢披萨。除非选中该复选框,否则文本框为“必需”。该复选框有效地作为布尔值运行,但所使用的组件不能改变(因为用户研究员这样说)。目前,我正在使用AJV来验证我的架构,并且它被配置为在属性为errorMessages.required
但未输入/选择输入时抛出required
。
不幸的是,在JSON架构方面,我完全缺乏经验。以下是我目前尝试进行验证的尝试。这正确呈现,但它不能正常工作 - 在我的开发环境中它只是验证任何东西,但在jsonschemavalidator.net它不会验证,除非选中复选框。如何实现我想要的功能?
{
$schema: 'http://json-schema.org/draft-07/schema#',
type: 'object',
additionalProperties: false,
propertyNames: {
enum: [
'q-why-i-love-pizza',
'q-i-hate-pizza'
]
},
properties: {
'q-why-i-love-pizza': {
type: 'string',
title: 'If you love pizza, tell us why',
maxLength: 500,
errorMessages: {
required: "Please tell us why you love pizza, or select 'I hate pizza'"
}
},
'q-i-hate-pizza': {
type: 'array',
maxItems: 1,
uniqueItems: true,
items: {
anyOf: [
{
title: 'I hate pizza',
const: 'hate'
}
]
},
errorMessages: {
required: "Please tell us why you love pizza, or select 'I hate pizza'"
}
}
},
allOf: [
{
$ref: '#/definitions/if-not-checked-then-q-why-i-love-pizza-is-required'
}
],
definitions: {
'if-not-checked-then-q-why-i-love-pizza-is-required': {
if: {
not: {
properties: {
'q-i-hate-pizza': {
const: 'hate'
}
}
}
},
then: {
required: ['q-why-i-love-pizza'],
propertyNames: {
enum: [
'q-i-hate-pizza',
'q-why-i-love-pizza'
]
}
}
}
}
}
编辑:
我期待以下内容:
{
'q-why-i-love-pizza' : '',
'q-i-hate-pizza' : ['']
}
这应该是FAIL验证,因为没有选择任何值。
{
'q-why-i-love-pizza' : 'I love pizza because it's amazing',
'q-i-hate-pizza' : ['']
}
这应该通过,因为用户输入了他们喜欢披萨的原因,因此没有必要点击复选框。
{
'q-why-i-love-pizza' : '',
'q-i-hate-pizza' : ['hate']
}
这应该通过,因为虽然用户没有告诉我们为什么他们喜欢披萨,但他们已经检查了盒子,表明他们讨厌披萨。
{
'q-why-i-love-pizza' : 'I am a user, so decided to tell you I hate pizza too',
'q-i-hate-pizza' : ['hate']
}
这也应该通过,因为我需要接受用户勾选方框说他们讨厌比萨的可能性,但无论如何继续告诉我。
解:
{
type: "object",
properties: {
'q-why-i-love-pizza': {
type: 'string',
title: 'If you love pizza, tell us why',
maxLength: 500,
errorMessages: {
required: "Please tell us why you love pizza, or select 'I hate pizza'"
}
},
'q-i-hate-pizza': {
type: 'array',
maxItems: 1,
uniqueItems: true,
items: {
anyOf: [
{
title: 'I hate pizza',
const: 'hate'
}
]
},
errorMessages: {
required: "Please tell us why you love pizza, or select 'I hate pizza'"
}
}
},
allOf: [
{ $ref: "#/definitions/if-not-checked-then-q-offender-contact-description-is-required" }
],
definitions: {
"if-not-checked-then-q-offender-contact-description-is-required": {
if: {
not: {
required: ["q-i-hate-pizza"]
}
},
then: {
required: ["q-why-i-love-pizza"]
}
}
}
}
我已经简化了你的模式,所以这可能不完美,但这种一般方法应该有效。
{
"type": "object",
"properties": {
"q-why-i-love-pizza": { "type": "string", "maxLength": 500 },
"q-i-hate-pizza": { "const": ["hate"] }
},
"allOf": [
{ "$ref": "#/definitions/if-hates-pizza-then-q-why-i-love-pizza-is-required" }
],
"definitions": {
"if-hates-pizza-then-q-why-i-love-pizza-is-required": {
"if": { "not": { "required": ["q-i-hate-pizza"] } },
"then": { "required": ["q-why-i-love-pizza"] }
}
}
}
你大部分都在那里,但你错过了一些你可能没想到的东西。
您的if-not-checked-then-q-why-i-love-pizza-is-required
架构。 const
关键字仅适用于该属性,如果它存在。您需要在required
模式中添加if
约束。
显然这是猜测,因为您还没有提供JSON实例数据。