| -- in.cue -- |
| import ( |
| "struct" |
| "encoding/json" |
| ) |
| |
| |
| // non-monotonic builtins must fail with an "incomplete" error if there |
| // is a possibility the constraint can get resolved by becoming more specific. |
| incompleteError1: { |
| MyType: { |
| kv: struct.MinFields(1) |
| } |
| |
| foo: MyType & { |
| kv: joel: "testing" |
| } |
| } |
| |
| incompleteError2: { |
| MyType: { |
| kv: [string]: string |
| kv: struct.MinFields(1) |
| } |
| |
| foo: MyType & { |
| kv: joel: "testing" |
| } |
| } |
| |
| incompleteError3: { |
| t: string |
| t: json.Validate(string) |
| } |
| |
| uniqueConstrains1: { |
| t: string |
| t: json.Validate(string) |
| t: json.Validate(string) |
| } |
| |
| uniqueConstrains2: { |
| t: struct.MaxFields(1) |
| t: struct.MaxFields(1) |
| } |
| |
| violation: { |
| #MyType: { |
| kv: [string]: string |
| kv: struct.MinFields(1) |
| } |
| |
| foo: #MyType & { |
| kv: joel: "testing" |
| kv: tony: "testing" |
| } |
| } |
| |
| conjuncts: { |
| kv: struct.MinFields(1) |
| kv: struct.MaxFields(3) |
| } |
| |
| // TODO: stripe off conflicting pairs |
| // conflicting: { |
| // kv: struct.MinFields(3) |
| // kv: struct.MaxFields(1) |
| // } |
| |
| // Builtins with bool return that can be used as validator. |
| |
| bareBuiltin: { |
| a: json.Valid |
| a: json.Valid |
| } |
| |
| bareBuiltinCheck: { |
| a: json.Valid |
| a: "3" |
| } |
| |
| builtinValidator: { |
| a: json.Valid() |
| a: json.Valid() |
| } |
| |
| builtinValidatorCheck: { |
| a: json.Valid() |
| a: "3" |
| } |
| |
| callOfCallToValidator: { |
| a: json.Valid |
| b: a() |
| e: b() // not allowed |
| e: "5" |
| } |
| |
| validatorAsFunction: { |
| a: json.Valid |
| b: a("3") |
| c: json.Valid("3") |
| } |
| |
| -- out/eval -- |
| Errors: |
| callOfCallToValidator.e: cannot call previously called validator b: |
| ./in.cue:94:8 |
| |
| Result: |
| (_|_){ |
| // [eval] |
| incompleteError1: (struct){ |
| MyType: (struct){ |
| kv: (struct){ struct.MinFields(1) } |
| } |
| foo: (struct){ |
| kv: (struct){ |
| joel: (string){ "testing" } |
| } |
| } |
| } |
| incompleteError2: (struct){ |
| MyType: (struct){ |
| kv: (_|_){ |
| // [incomplete] incompleteError2.MyType.kv: invalid value {} (does not satisfy struct.MinFields(1)): len(fields) < MinFields(1) (0 < 1): |
| // ./in.cue:22:17 |
| // ./in.cue:22:34 |
| } |
| } |
| foo: (struct){ |
| kv: (struct){ |
| joel: (string){ "testing" } |
| } |
| } |
| } |
| incompleteError3: (struct){ |
| t: (string){ &("encoding/json".Validate(string), string) } |
| } |
| uniqueConstrains1: (struct){ |
| t: (string){ &("encoding/json".Validate(string), string) } |
| } |
| uniqueConstrains2: (struct){ |
| t: (struct){ struct.MaxFields(1) } |
| } |
| violation: (struct){ |
| #MyType: (#struct){ |
| kv: (_|_){ |
| // [incomplete] violation.#MyType.kv: invalid value {} (does not satisfy struct.MinFields(1)): len(fields) < MinFields(1) (0 < 1): |
| // ./in.cue:49:17 |
| // ./in.cue:49:34 |
| } |
| } |
| foo: (#struct){ |
| kv: (#struct){ |
| joel: (string){ "testing" } |
| tony: (string){ "testing" } |
| } |
| } |
| } |
| conjuncts: (struct){ |
| kv: (struct){ &(struct.MinFields(1), struct.MaxFields(3)) } |
| } |
| bareBuiltin: (struct){ |
| a: ((string|bytes)){ "encoding/json".Valid() } |
| } |
| bareBuiltinCheck: (struct){ |
| a: (string){ "3" } |
| } |
| builtinValidator: (struct){ |
| a: ((string|bytes)){ "encoding/json".Valid() } |
| } |
| builtinValidatorCheck: (struct){ |
| a: (string){ "3" } |
| } |
| callOfCallToValidator: (_|_){ |
| // [eval] |
| a: ((string|bytes)){ "encoding/json".Valid() } |
| b: ((string|bytes)){ "encoding/json".Valid() } |
| e: (_|_){ |
| // [eval] callOfCallToValidator.e: cannot call previously called validator b: |
| // ./in.cue:94:8 |
| } |
| } |
| validatorAsFunction: (struct){ |
| a: ((string|bytes)){ "encoding/json".Valid() } |
| b: (bool){ true } |
| c: (bool){ true } |
| } |
| } |
| -- out/compile -- |
| --- in.cue |
| { |
| incompleteError1: { |
| MyType: { |
| kv: 〈import;struct〉.MinFields(1) |
| } |
| foo: (〈0;MyType〉 & { |
| kv: { |
| joel: "testing" |
| } |
| }) |
| } |
| incompleteError2: { |
| MyType: { |
| kv: { |
| [string]: string |
| } |
| kv: 〈import;struct〉.MinFields(1) |
| } |
| foo: (〈0;MyType〉 & { |
| kv: { |
| joel: "testing" |
| } |
| }) |
| } |
| incompleteError3: { |
| t: string |
| t: 〈import;"encoding/json"〉.Validate(string) |
| } |
| uniqueConstrains1: { |
| t: string |
| t: 〈import;"encoding/json"〉.Validate(string) |
| t: 〈import;"encoding/json"〉.Validate(string) |
| } |
| uniqueConstrains2: { |
| t: 〈import;struct〉.MaxFields(1) |
| t: 〈import;struct〉.MaxFields(1) |
| } |
| violation: { |
| #MyType: { |
| kv: { |
| [string]: string |
| } |
| kv: 〈import;struct〉.MinFields(1) |
| } |
| foo: (〈0;#MyType〉 & { |
| kv: { |
| joel: "testing" |
| } |
| kv: { |
| tony: "testing" |
| } |
| }) |
| } |
| conjuncts: { |
| kv: 〈import;struct〉.MinFields(1) |
| kv: 〈import;struct〉.MaxFields(3) |
| } |
| bareBuiltin: { |
| a: 〈import;"encoding/json"〉.Valid |
| a: 〈import;"encoding/json"〉.Valid |
| } |
| bareBuiltinCheck: { |
| a: 〈import;"encoding/json"〉.Valid |
| a: "3" |
| } |
| builtinValidator: { |
| a: 〈import;"encoding/json"〉.Valid() |
| a: 〈import;"encoding/json"〉.Valid() |
| } |
| builtinValidatorCheck: { |
| a: 〈import;"encoding/json"〉.Valid() |
| a: "3" |
| } |
| callOfCallToValidator: { |
| a: 〈import;"encoding/json"〉.Valid |
| b: 〈0;a〉() |
| e: 〈0;b〉() |
| e: "5" |
| } |
| validatorAsFunction: { |
| a: 〈import;"encoding/json"〉.Valid |
| b: 〈0;a〉("3") |
| c: 〈import;"encoding/json"〉.Valid("3") |
| } |
| } |