這部分是針對數據驗證,但如果可以使用json模式驗證來完成,那麼該模式可以更好地滿足我的需求。我輸入的是:二叉樹邏輯運算的Json模式驗證
(1 or (1 and 0)) => true
成爲一個JSON對象:
[1, [1, 0]]
所以驗證應該給沒有錯誤,但目前我的架構不能嵌套數據之間的手柄或情況正常。
{
"allOf": [
{"$ref": "#/definitions/_items"},
{
"allOf": [
{"$ref": "#/definitions/_or"},
{
"items": {
"$ref": "#/definitions/_and"
}
}
]
}
],
"definitions": {
"_items": {
"minItems": 1,
"maxItems": 2,
"type": "array",
"items": {
"anyOf": [
{"enum": [1,0]},
{"$ref": "#/definitions/_items"}
]
}
},
"_or": {
"not": {
"type": "array",
"items": {
"not": {
"anyOf": [
{"enum": [1]},
{"$ref": "#/definitions/_items"}
]
}
}
}
},
"_and": {
"items": {
"anyOf": [
{"enum": [1]},
{"$ref": "#/definitions/_items"}
]
}
}
}
}
驗證下一個數據對模式:
[1,0] is valid, ok
[1,1] is valid, ok
[0,1] is valid, ok
[0,0] not valid, ok
[[1,1],1] valid, ok
[[1,1],0] valid, ok
但:
[[1,0],1] not valid, not ok!
[[0,0],1] not valid, not ok!
因爲無論開啓或操作有1,如果只是滿足_items定義,以便左側不應該的問題。
所以我的問題是我應該如何改變模式,以便任何OR運算符都足夠有效的輸入?
更多的例子請通過@esp:通過邏輯表達式和相應的JSON數據
我輸入例子:
1 or (1 and 0)) => [1, [1, 0]] => true
1 or (0 and 1)) => [1, [0, 1]] => true
1 or (1 and 1)) => [1, [0, 0]] => true
1 or (0 and 0)) => [1, [0, 0]] => true
1 or (1)) => [1, [1]] => true
1 or (0)) => [1, [0]] => true
1 or 1 => [1, 1 => true
1 or 0 => [1, 0] => true
0 or 1 => [0, 1] => true
0 or 0 => [0, 0] => false
我也給左側的幾個例子嵌套,但不是全部,因爲組合很快就會變得很混亂:
(1 and 0) or (1 and 0)) => [[1, 0], [1, 0]] => false
(0 and 0) or (0 and o)) => [[0, 0], [0, 0]] => false
(1 and 1) or (1 and 0)) => [[1, 1], [1, 0]] => true
(1 and 0) or (1 and 1)) => [[1, 0], [1, 1]] => true
(1 and 1) or (1 and 1)) => [[1, 1], [1, 1]] => true
您能定義所有可能的輸入和預期輸出嗎?有些示例不匹配x或(y和z)模式。 – esp
另外似乎有兩個問題 - 表達式是否匹配模式以及它是否爲真,因此您可能需要兩個模式而不是一個,否則如何區分不匹配模式和false? – esp
@esp:新增示例。因此,輸入可以是:[p]或[q]或[p,q]或[p,[q]]或[[p],q]或[[p],[q]]'二進制項目的邊可以是數字1,0或包含至少一個相似項目的集合,但最多兩個。這應該已經用_items定義完成。 – MarkokraM