cuelang.org/go@v0.10.1/cue/testdata/builtins/validators.txtar (about) 1 -- in.cue -- 2 import ( 3 "encoding/json" 4 "list" 5 "struct" 6 ) 7 8 // non-monotonic builtins must fail with an "incomplete" error if there 9 // is a possibility the constraint can get resolved by becoming more specific. 10 incompleteError1: { 11 MyType: { 12 kv: struct.MinFields(1) 13 } 14 15 foo: MyType & { 16 kv: joel: "testing" 17 } 18 } 19 20 incompleteError2: { 21 MyType: { 22 kv: [string]: string 23 kv: struct.MinFields(1) 24 } 25 26 foo: MyType & { 27 kv: joel: "testing" 28 } 29 } 30 31 incompleteError3: { 32 t: string 33 t: json.Validate(string) 34 } 35 36 uniqueConstrains1: { 37 t: string 38 t: json.Validate(string) 39 t: json.Validate(string) 40 } 41 42 uniqueConstrains2: { 43 t: struct.MaxFields(1) 44 t: struct.MaxFields(1) 45 } 46 47 violation: { 48 #MyType: { 49 kv: [string]: string 50 kv: struct.MinFields(1) 51 } 52 53 foo: #MyType & { 54 kv: joel: "testing" 55 kv: tony: "testing" 56 } 57 } 58 59 conjuncts: { 60 kv: struct.MinFields(1) 61 kv: struct.MaxFields(3) 62 } 63 64 // TODO: stripe off conflicting pairs 65 // conflicting: { 66 // kv: struct.MinFields(3) 67 // kv: struct.MaxFields(1) 68 // } 69 70 // Builtins with bool return that can be used as validator. 71 72 bareBuiltin: { 73 a: json.Valid 74 a: json.Valid 75 } 76 77 bareBuiltinCheck: { 78 a: json.Valid 79 a: "3" 80 } 81 82 builtinValidator: { 83 a: json.Valid() 84 a: json.Valid() 85 } 86 87 builtinValidatorCheck: { 88 a: json.Valid() 89 a: "3" 90 } 91 92 callOfCallToValidator: { 93 a: json.Valid 94 b: a() 95 e: b() // not allowed 96 e: "5" 97 } 98 99 validatorAsFunction: { 100 a: json.Valid 101 b: a("3") 102 c: json.Valid("3") 103 } 104 105 issue2098: ok1: { 106 _a: [1, ...] 107 _a: list.MinItems(1) 108 _a[0] 109 } 110 111 issue2098: incomplete1: { 112 _a: [...] 113 _a: list.MinItems(1) 114 _a[0] 115 } 116 117 -- out/eval/stats -- 118 Leaks: 0 119 Freed: 50 120 Reused: 45 121 Allocs: 5 122 Retain: 0 123 124 Unifications: 50 125 Conjuncts: 91 126 Disjuncts: 52 127 -- out/evalalpha -- 128 Errors: 129 callOfCallToValidator.e: cannot call previously called validator b: 130 ./in.cue:94:5 131 132 Result: 133 (_|_){ 134 // [eval] 135 incompleteError1: (struct){ 136 MyType: (struct){ 137 kv: (struct){ struct.MinFields(1) } 138 } 139 foo: (struct){ 140 kv: (struct){ 141 joel: (string){ "testing" } 142 } 143 } 144 } 145 incompleteError2: (struct){ 146 MyType: (struct){ 147 kv: (_|_){ 148 // [incomplete] incompleteError2.MyType.kv: invalid value {} (does not satisfy struct.MinFields(1)): len(fields) < MinFields(1) (0 < 1): 149 // ./in.cue:22:7 150 // ./in.cue:22:24 151 } 152 } 153 foo: (struct){ 154 kv: (struct){ 155 joel: (string){ "testing" } 156 } 157 } 158 } 159 incompleteError3: (struct){ 160 t: (string){ &("encoding/json".Validate(string), string) } 161 } 162 uniqueConstrains1: (struct){ 163 t: (string){ &("encoding/json".Validate(string), string) } 164 } 165 uniqueConstrains2: (struct){ 166 t: (struct){ struct.MaxFields(1) } 167 } 168 violation: (struct){ 169 #MyType: (#struct){ 170 kv: (_|_){ 171 // [incomplete] violation.#MyType.kv: invalid value {} (does not satisfy struct.MinFields(1)): len(fields) < MinFields(1) (0 < 1): 172 // ./in.cue:49:7 173 // ./in.cue:49:24 174 } 175 } 176 foo: (#struct){ 177 kv: (#struct){ 178 joel: (string){ "testing" } 179 tony: (string){ "testing" } 180 } 181 } 182 } 183 conjuncts: (struct){ 184 kv: (struct){ &(struct.MinFields(1), struct.MaxFields(3)) } 185 } 186 bareBuiltin: (struct){ 187 a: ((string|bytes)){ "encoding/json".Valid() } 188 } 189 bareBuiltinCheck: (struct){ 190 a: (string){ "3" } 191 } 192 builtinValidator: (struct){ 193 a: ((string|bytes)){ "encoding/json".Valid() } 194 } 195 builtinValidatorCheck: (struct){ 196 a: (string){ "3" } 197 } 198 callOfCallToValidator: (_|_){ 199 // [eval] 200 a: ((string|bytes)){ "encoding/json".Valid() } 201 b: ((string|bytes)){ "encoding/json".Valid() } 202 e: (_|_){ 203 // [eval] callOfCallToValidator.e: cannot call previously called validator b: 204 // ./in.cue:94:5 205 } 206 } 207 validatorAsFunction: (struct){ 208 a: ((string|bytes)){ "encoding/json".Valid() } 209 b: (bool){ true } 210 c: (bool){ true } 211 } 212 issue2098: (struct){ 213 ok1: (int){ 214 1 215 _a: (list){ 216 0: (int){ 1 } 217 } 218 } 219 incomplete1: (struct){ 220 _a: (_|_){ 221 // [incomplete] issue2098.incomplete1._a: invalid value [] (does not satisfy list.MinItems(1)): len(list) < MinItems(1) (0 < 1): 222 // ./in.cue:112:6 223 // ./in.cue:112:20 224 } 225 } 226 } 227 } 228 -- diff/-out/evalalpha<==>+out/eval -- 229 diff old new 230 --- old 231 +++ new 232 @@ -20,7 +20,6 @@ 233 kv: (_|_){ 234 // [incomplete] incompleteError2.MyType.kv: invalid value {} (does not satisfy struct.MinFields(1)): len(fields) < MinFields(1) (0 < 1): 235 // ./in.cue:22:7 236 - // ./in.cue:21:7 237 // ./in.cue:22:24 238 } 239 } 240 @@ -44,7 +43,6 @@ 241 kv: (_|_){ 242 // [incomplete] violation.#MyType.kv: invalid value {} (does not satisfy struct.MinFields(1)): len(fields) < MinFields(1) (0 < 1): 243 // ./in.cue:49:7 244 - // ./in.cue:48:7 245 // ./in.cue:49:24 246 } 247 } 248 @@ -91,15 +89,10 @@ 249 0: (int){ 1 } 250 } 251 } 252 - incomplete1: (_|_){ 253 - // [incomplete] issue2098.incomplete1._a: invalid value [] (does not satisfy list.MinItems(1)): len(list) < MinItems(1) (0 < 1): 254 - // ./in.cue:112:6 255 - // ./in.cue:111:6 256 - // ./in.cue:112:20 257 + incomplete1: (struct){ 258 _a: (_|_){ 259 // [incomplete] issue2098.incomplete1._a: invalid value [] (does not satisfy list.MinItems(1)): len(list) < MinItems(1) (0 < 1): 260 // ./in.cue:112:6 261 - // ./in.cue:111:6 262 // ./in.cue:112:20 263 } 264 } 265 -- diff/todo/p2 -- 266 Missing error at incomplete1: index 0 is erased. 267 -- out/eval -- 268 Errors: 269 callOfCallToValidator.e: cannot call previously called validator b: 270 ./in.cue:94:5 271 272 Result: 273 (_|_){ 274 // [eval] 275 incompleteError1: (struct){ 276 MyType: (struct){ 277 kv: (struct){ struct.MinFields(1) } 278 } 279 foo: (struct){ 280 kv: (struct){ 281 joel: (string){ "testing" } 282 } 283 } 284 } 285 incompleteError2: (struct){ 286 MyType: (struct){ 287 kv: (_|_){ 288 // [incomplete] incompleteError2.MyType.kv: invalid value {} (does not satisfy struct.MinFields(1)): len(fields) < MinFields(1) (0 < 1): 289 // ./in.cue:22:7 290 // ./in.cue:21:7 291 // ./in.cue:22:24 292 } 293 } 294 foo: (struct){ 295 kv: (struct){ 296 joel: (string){ "testing" } 297 } 298 } 299 } 300 incompleteError3: (struct){ 301 t: (string){ &("encoding/json".Validate(string), string) } 302 } 303 uniqueConstrains1: (struct){ 304 t: (string){ &("encoding/json".Validate(string), string) } 305 } 306 uniqueConstrains2: (struct){ 307 t: (struct){ struct.MaxFields(1) } 308 } 309 violation: (struct){ 310 #MyType: (#struct){ 311 kv: (_|_){ 312 // [incomplete] violation.#MyType.kv: invalid value {} (does not satisfy struct.MinFields(1)): len(fields) < MinFields(1) (0 < 1): 313 // ./in.cue:49:7 314 // ./in.cue:48:7 315 // ./in.cue:49:24 316 } 317 } 318 foo: (#struct){ 319 kv: (#struct){ 320 joel: (string){ "testing" } 321 tony: (string){ "testing" } 322 } 323 } 324 } 325 conjuncts: (struct){ 326 kv: (struct){ &(struct.MinFields(1), struct.MaxFields(3)) } 327 } 328 bareBuiltin: (struct){ 329 a: ((string|bytes)){ "encoding/json".Valid() } 330 } 331 bareBuiltinCheck: (struct){ 332 a: (string){ "3" } 333 } 334 builtinValidator: (struct){ 335 a: ((string|bytes)){ "encoding/json".Valid() } 336 } 337 builtinValidatorCheck: (struct){ 338 a: (string){ "3" } 339 } 340 callOfCallToValidator: (_|_){ 341 // [eval] 342 a: ((string|bytes)){ "encoding/json".Valid() } 343 b: ((string|bytes)){ "encoding/json".Valid() } 344 e: (_|_){ 345 // [eval] callOfCallToValidator.e: cannot call previously called validator b: 346 // ./in.cue:94:5 347 } 348 } 349 validatorAsFunction: (struct){ 350 a: ((string|bytes)){ "encoding/json".Valid() } 351 b: (bool){ true } 352 c: (bool){ true } 353 } 354 issue2098: (struct){ 355 ok1: (int){ 356 1 357 _a: (list){ 358 0: (int){ 1 } 359 } 360 } 361 incomplete1: (_|_){ 362 // [incomplete] issue2098.incomplete1._a: invalid value [] (does not satisfy list.MinItems(1)): len(list) < MinItems(1) (0 < 1): 363 // ./in.cue:112:6 364 // ./in.cue:111:6 365 // ./in.cue:112:20 366 _a: (_|_){ 367 // [incomplete] issue2098.incomplete1._a: invalid value [] (does not satisfy list.MinItems(1)): len(list) < MinItems(1) (0 < 1): 368 // ./in.cue:112:6 369 // ./in.cue:111:6 370 // ./in.cue:112:20 371 } 372 } 373 } 374 } 375 -- out/compile -- 376 --- in.cue 377 { 378 incompleteError1: { 379 MyType: { 380 kv: 〈import;struct〉.MinFields(1) 381 } 382 foo: (〈0;MyType〉 & { 383 kv: { 384 joel: "testing" 385 } 386 }) 387 } 388 incompleteError2: { 389 MyType: { 390 kv: { 391 [string]: string 392 } 393 kv: 〈import;struct〉.MinFields(1) 394 } 395 foo: (〈0;MyType〉 & { 396 kv: { 397 joel: "testing" 398 } 399 }) 400 } 401 incompleteError3: { 402 t: string 403 t: 〈import;"encoding/json"〉.Validate(string) 404 } 405 uniqueConstrains1: { 406 t: string 407 t: 〈import;"encoding/json"〉.Validate(string) 408 t: 〈import;"encoding/json"〉.Validate(string) 409 } 410 uniqueConstrains2: { 411 t: 〈import;struct〉.MaxFields(1) 412 t: 〈import;struct〉.MaxFields(1) 413 } 414 violation: { 415 #MyType: { 416 kv: { 417 [string]: string 418 } 419 kv: 〈import;struct〉.MinFields(1) 420 } 421 foo: (〈0;#MyType〉 & { 422 kv: { 423 joel: "testing" 424 } 425 kv: { 426 tony: "testing" 427 } 428 }) 429 } 430 conjuncts: { 431 kv: 〈import;struct〉.MinFields(1) 432 kv: 〈import;struct〉.MaxFields(3) 433 } 434 bareBuiltin: { 435 a: 〈import;"encoding/json"〉.Valid 436 a: 〈import;"encoding/json"〉.Valid 437 } 438 bareBuiltinCheck: { 439 a: 〈import;"encoding/json"〉.Valid 440 a: "3" 441 } 442 builtinValidator: { 443 a: 〈import;"encoding/json"〉.Valid() 444 a: 〈import;"encoding/json"〉.Valid() 445 } 446 builtinValidatorCheck: { 447 a: 〈import;"encoding/json"〉.Valid() 448 a: "3" 449 } 450 callOfCallToValidator: { 451 a: 〈import;"encoding/json"〉.Valid 452 b: 〈0;a〉() 453 e: 〈0;b〉() 454 e: "5" 455 } 456 validatorAsFunction: { 457 a: 〈import;"encoding/json"〉.Valid 458 b: 〈0;a〉("3") 459 c: 〈import;"encoding/json"〉.Valid("3") 460 } 461 issue2098: { 462 ok1: { 463 _a: [ 464 1, 465 ..., 466 ] 467 _a: 〈import;list〉.MinItems(1) 468 〈0;_a〉[0] 469 } 470 } 471 issue2098: { 472 incomplete1: { 473 _a: [ 474 ..., 475 ] 476 _a: 〈import;list〉.MinItems(1) 477 〈0;_a〉[0] 478 } 479 } 480 }