![]() |
果断的电池 · 如何在json schema中使用`If` ...· 1 月前 · |
![]() |
完美的馒头 · 如何用命令获取和查看RDS ...· 8 月前 · |
![]() |
慷慨的排球 · 50行代码探索金融数据,使用Python进行 ...· 10 月前 · |
![]() |
失恋的回锅肉 · 如何将ArrayBuffer转换为Base6 ...· 11 月前 · |
![]() |
有爱心的显示器 · Lerna specify run ...· 1 年前 · |
![]() |
爱旅游的荔枝 · SyntaxError: ...· 1 年前 · |
JSON Schema (draft-07)的一个相对较新的添加添加了if、then和else关键字。我不知道如何正确地使用这些新的关键词。以下是我到目前为止的JSON Schema:
{
"type": "object",
"properties": {
"foo": {
"type": "string"
"bar": {
"type": "string"
"if": {
"properties": {
"foo": {
"enum": [
"bar"
"then": {
"required": [
"bar"
}
如果"foo“属性等于"bar",则"bar”属性是必需的。这与预期的一样。
但是,如果"foo“属性不存在或者输入为空,那么我什么都不想要。如何做到这一点呢?
empty input {}.
发现错误:
Required properties are missing from object: bar. Schema path: #/then/required
我使用在线验证工具:
发布于 2018-07-26 21:22:57
if
关键字表示,如果值模式的结果通过验证,则应用
then
模式,否则应用
else
模式。
您的模式不起作用,因为您需要在
if
模式中使用
"foo"
,否则一个空的JSON实例将通过
if
模式的验证,从而应用需要
"bar"
的
then
模式。
其次,您需要
"propertyNames":false
,它可以防止模式中包含任何键,这与设置
"else": false
不同,后者会使任何内容始终无法通过验证。
{
"type": "object",
"properties": {
"foo": {
"type": "string"
"bar": {
"type": "string"
"if": {
"properties": {
"foo": {
"enum": [
"bar"
"required": [
"foo"
"then": {
"required": [
"bar"
"else": false
}
发布于 2018-07-26 21:19:34
不能只使用"else“属性吗?
{
"type": "object",
"properties": {
"foo": { "type": "string" },
"bar": { "type": "string" }
"if": {
"properties": {
"foo": {
"enum": ["bar"]
"then": {