JSON 模式 'Required' 通过复选框验证 set/removed
JSON Schema 'Required' validation set/removed by checkbox
我正在设计一个使用 JSON 架构建模的 Web 应用程序。我正在尝试创建一个带有文本区域和复选框的页面。文字区是解释我为什么喜欢披萨。如果用户单击该复选框,则表示他们确认他们不喜欢披萨。除非选中复选框,否则文本框为 'required'。复选框作为布尔值有效运行,但无法更改正在使用的组件(因为用户研究员这么说)。目前,我正在使用 AJV 来验证我的架构,并且它被配置为在 属性 为 required
但没有输入为 entered/selected 时抛出 errorMessages.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' : ['']
}
由于未选择任何值,因此验证失败。
{
'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"]
}
}
}
}
您已经完成了大部分内容,但是您错过了一些您可能没有预料到的东西。
您 if-not-checked-then-q-why-i-love-pizza-is-required
的架构。
const
关键字仅适用于 属性,如果存在的话。
您需要向 if
架构添加 required
约束。
显然这是猜测,因为您还没有提供 JSON 实例数据。
我已经简化了您的架构,因此这可能并不完美,但这种通用方法应该可行。
{
"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"] }
}
}
}
我正在设计一个使用 JSON 架构建模的 Web 应用程序。我正在尝试创建一个带有文本区域和复选框的页面。文字区是解释我为什么喜欢披萨。如果用户单击该复选框,则表示他们确认他们不喜欢披萨。除非选中复选框,否则文本框为 'required'。复选框作为布尔值有效运行,但无法更改正在使用的组件(因为用户研究员这么说)。目前,我正在使用 AJV 来验证我的架构,并且它被配置为在 属性 为 required
但没有输入为 entered/selected 时抛出 errorMessages.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' : ['']
}
由于未选择任何值,因此验证失败。
{
'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"]
}
}
}
}
您已经完成了大部分内容,但是您错过了一些您可能没有预料到的东西。
您 if-not-checked-then-q-why-i-love-pizza-is-required
的架构。
const
关键字仅适用于 属性,如果存在的话。
您需要向 if
架构添加 required
约束。
显然这是猜测,因为您还没有提供 JSON 实例数据。
我已经简化了您的架构,因此这可能并不完美,但这种通用方法应该可行。
{
"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"] }
}
}
}