Skip to content

Commit 00a747c

Browse files
NoamTDmvdan
authored andcommitted
pkg/encoding: add test cases for {json,yaml}.Validate
The json/yaml Validate APIs currently do not correctly handle disjunctions. This CL adds test cases displaying false negatives returned by these functions. For #2741. Signed-off-by: Noam Dolovich <[email protected]> Change-Id: I10de43a09883e770c42d2499cef7ae29b3593b19 Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1194424 Reviewed-by: Daniel Martí <[email protected]> TryBot-Result: CUEcueckoo <[email protected]> Unity-Result: CUE porcuepine <[email protected]>
1 parent bf75699 commit 00a747c

File tree

2 files changed

+58
-7
lines changed

2 files changed

+58
-7
lines changed

pkg/encoding/json/testdata/gen.txtar

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ validate: [string]: {
1111
validate: t1: schema: {b: string}
1212
validate: t2: schema: {a: <3}
1313

14+
validate: disjunctionRequired: schema: {a!: int} | {b!: int}
15+
validate: disjunctionClosed: schema: close({a: int}) | close({b: int})
16+
1417
// Issue #2395
1518
validate: enforceRequired: {
1619
str: "{}"
@@ -93,9 +96,9 @@ validate.t2.result: error in call to encoding/json.Validate: invalid value 10 (o
9396
./in.cue:9:27
9497
json.Validate:1:6
9598
unmarshal.trailingInvalid.#result: error in call to encoding/json.Unmarshal: json: invalid JSON:
96-
./in.cue:39:11
99+
./in.cue:42:11
97100
unmarshal.trailingValid.#result: error in call to encoding/json.Unmarshal: json: invalid JSON:
98-
./in.cue:39:11
101+
./in.cue:42:11
99102

100103
Result:
101104
import "encoding/json"
@@ -115,6 +118,24 @@ validate: {
115118
}
116119
result: _|_ // validate.t2.result: error in call to encoding/json.Validate: validate.t2.schema.a: invalid value 10 (out of bound <3)
117120
}
121+
disjunctionRequired: {
122+
str: *"{\"a\":10}" | string
123+
schema: {
124+
a!: int
125+
} | {
126+
b!: int
127+
}
128+
result: json.Validate(str, schema)
129+
}
130+
disjunctionClosed: {
131+
str: *"{\"a\":10}" | string
132+
schema: {
133+
a: int
134+
} | {
135+
b: int
136+
}
137+
result: json.Validate(str, schema)
138+
}
118139

119140
// Issue #2395
120141
enforceRequired: {
@@ -251,7 +272,7 @@ unmarshalStream: {
251272
diff old new
252273
--- old
253274
+++ new
254-
@@ -75,23 +75,15 @@
275+
@@ -93,23 +93,15 @@
255276
}
256277
marshalStream: {
257278
t1: {
@@ -284,7 +305,7 @@ diff old new
284305
[{
285306
a: 1
286307
}, {
287-
@@ -99,11 +91,7 @@
308+
@@ -117,11 +109,7 @@
288309
}]
289310
}
290311
t3: {
@@ -297,7 +318,7 @@ diff old new
297318
[{
298319
a: "\\ \" & < >"
299320
}, {
300-
@@ -113,31 +101,27 @@
321+
@@ -131,31 +119,27 @@
301322
}
302323
marshal: {
303324
t1: {
@@ -356,9 +377,9 @@ validate.t2.result: error in call to encoding/json.Validate: invalid value 10 (o
356377
./in.cue:9:27
357378
json.Validate:1:6
358379
unmarshal.trailingInvalid.#result: error in call to encoding/json.Unmarshal: json: invalid JSON:
359-
./in.cue:39:11
380+
./in.cue:42:11
360381
unmarshal.trailingValid.#result: error in call to encoding/json.Unmarshal: json: invalid JSON:
361-
./in.cue:39:11
382+
./in.cue:42:11
362383

363384
Result:
364385
import "encoding/json"
@@ -378,6 +399,24 @@ validate: {
378399
}
379400
result: _|_ // validate.t2.result: error in call to encoding/json.Validate: validate.t2.schema.a: invalid value 10 (out of bound <3)
380401
}
402+
disjunctionRequired: {
403+
str: *"{\"a\":10}" | string
404+
schema: {
405+
a!: int
406+
} | {
407+
b!: int
408+
}
409+
result: json.Validate(str, schema)
410+
}
411+
disjunctionClosed: {
412+
str: *"{\"a\":10}" | string
413+
schema: {
414+
a: int
415+
} | {
416+
b: int
417+
}
418+
result: json.Validate(str, schema)
419+
}
381420

382421
// Issue #2395
383422
enforceRequired: {
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
-- in.cue --
2+
import "encoding/yaml"
3+
4+
t1: yaml.Validate("a: 2", {a!: int} | {b!: int})
5+
-- out/yaml --
6+
import "encoding/yaml"
7+
8+
t1: yaml.Validate("a: 2", {
9+
a!: int
10+
} | {
11+
b!: int
12+
})

0 commit comments

Comments
 (0)