-- in.cue --
import "encoding/yaml"

#test: {
	fn: _

	data1: "a: 2"
	t1: [string]: data1
	t1: ok1: fn({a!: int} | {b!: int}) // TODO: filter unsatisfied required fields.
	t1: ok2: fn(close({a: int}) | close({b: int}))

	#A: {a: int}
	#B: {b: int}
	t1: ok3: fn(#A | #B)

	data2: "'foo'"
	t2: [string]: data2
	t2: ok1: fn(*int | string)
	t2: ok2: fn(string)
}

validate: #test & {
	fn: yaml.Validate

	// TODO: fix this test: the second disjunct should be eliminated, so there
	// should not be a concreteness error.
	t1: _
}

validatePartial: #test & {
	fn: yaml.ValidatePartial
}

invalidDisjuntion: yaml.Validate("a: 3", {a: 1 | 2})

// Both Validate and ValidatePartial must report every conflict, not just the
// first one. Regression test for https://cuelang.org/issue/2079.
multiConflict:        yaml.Validate(#"{field1: "bad1", field2: "bad2"}"#, {field1: int, field2: int})
multiConflictPartial: yaml.ValidatePartial(#"{field1: "bad1", field2: "bad2"}"#, {field1: int, field2: int})
-- out/yaml --
Errors:
validate.t1.ok1: invalid value "a: 2" (does not satisfy encoding/yaml.Validate): incomplete value {a:2} | {a:2,b!:int}:
    ./in.cue:8:11
    ./in.cue:6:9
    ./in.cue:7:16
invalidDisjuntion: error in call to encoding/yaml.Validate: 2 errors in empty disjunction::
    ./in.cue:33:20
invalidDisjuntion: error in call to encoding/yaml.Validate: conflicting values 1 and 3:
    ./in.cue:33:20
    ./in.cue:33:46
    yaml.Validate:1:4
invalidDisjuntion: error in call to encoding/yaml.Validate: conflicting values 2 and 3:
    ./in.cue:33:20
    ./in.cue:33:50
    yaml.Validate:1:4
multiConflict: error in call to encoding/yaml.Validate: conflicting values "bad1" and int (mismatched types string and int):
    ./in.cue:37:23
    ./in.cue:37:84
    yaml.Validate:1:10
multiConflict: error in call to encoding/yaml.Validate: conflicting values "bad2" and int (mismatched types string and int):
    ./in.cue:37:23
    ./in.cue:37:97
    yaml.Validate:1:26
multiConflictPartial: error in call to encoding/yaml.ValidatePartial: conflicting values "bad1" and int (mismatched types string and int):
    ./in.cue:38:23
    ./in.cue:38:91
    yaml.ValidatePartial:1:10
multiConflictPartial: error in call to encoding/yaml.ValidatePartial: conflicting values "bad2" and int (mismatched types string and int):
    ./in.cue:38:23
    ./in.cue:38:104
    yaml.ValidatePartial:1:26

Result:
import "encoding/yaml"

#test: {
	fn:    _
	data1: "a: 2"
	t1: {
		ok1: data1 & fn({
			a!: int
		} | {
			b!: int
		})
		ok2: data1 & fn(close({
			a: int
		}) | close({
			b: int
		}))
		ok3: data1 & fn(#A | #B)
	}
	#A: {
		a: int
	}
	#B: {
		b: int
	}
	data2: "'foo'"
	t2: {
		ok1: data2 & fn(*int | string)
		ok2: data2 & fn(string)
	}
}
validate: {
	fn:    yaml.Validate
	data1: "a: 2"

	// TODO: fix this test: the second disjunct should be eliminated, so there
	// should not be a concreteness error.
	t1: {
		ok1: _|_ // validate.t1.ok1: invalid value "a: 2" (does not satisfy encoding/yaml.Validate): validate.t1.ok1: incomplete value {a:2} | {a:2,b!:int}
		ok2: "a: 2"
		ok3: "a: 2"
	}
	#A: {
		a: int
	}
	#B: {
		b: int
	}
	data2: "'foo'"
	t2: {
		ok1: "'foo'"
		ok2: "'foo'"
	}
}
validatePartial: {
	fn:    yaml.ValidatePartial
	data1: "a: 2"
	t1: {
		ok1: "a: 2"
		ok2: "a: 2"
		ok3: "a: 2"
	}
	#A: {
		a: int
	}
	#B: {
		b: int
	}
	data2: "'foo'"
	t2: {
		ok1: "'foo'"
		ok2: "'foo'"
	}
}
invalidDisjuntion: _|_ // invalidDisjuntion: error in call to encoding/yaml.Validate: invalidDisjuntion.a: 2 errors in empty disjunction: (and 2 more errors)

// Both Validate and ValidatePartial must report every conflict, not just the
// first one. Regression test for https://cuelang.org/issue/2079.
multiConflict:        _|_ // multiConflict: error in call to encoding/yaml.Validate: multiConflict.field1: conflicting values "bad1" and int (mismatched types string and int) (and 1 more errors)
multiConflictPartial: _|_ // multiConflictPartial: error in call to encoding/yaml.ValidatePartial: multiConflictPartial.field1: conflicting values "bad1" and int (mismatched types string and int) (and 1 more errors)
