| 418 | } |
| 419 | |
| 420 | func TestExtendedValidations(t *testing.T) { |
| 421 | env, err := NewEnv( |
| 422 | Variable("x", types.StringType), |
| 423 | ExtendedValidations(), |
| 424 | ) |
| 425 | if err != nil { |
| 426 | t.Fatalf("NewEnv(ExtendedValidations()) failed: %v", err) |
| 427 | } |
| 428 | tests := []struct { |
| 429 | expr string |
| 430 | iss string |
| 431 | }{ |
| 432 | { |
| 433 | expr: `x in ['hello', 0] |
| 434 | && duration(x) < duration('1d') |
| 435 | && timestamp(x) != timestamp('1000-01-00T00:00:00Z') |
| 436 | && x.matches('x++')`, |
| 437 | iss: ` |
| 438 | ERROR: <input>:1:16: expected type 'string' but found 'int' |
| 439 | | x in ['hello', 0] |
| 440 | | ...............^ |
| 441 | ERROR: <input>:2:30: invalid duration argument |
| 442 | | && duration(x) < duration('1d') |
| 443 | | .............................^ |
| 444 | ERROR: <input>:3:33: invalid timestamp argument |
| 445 | | && timestamp(x) != timestamp('1000-01-00T00:00:00Z') |
| 446 | | ................................^ |
| 447 | ERROR: <input>:4:17: invalid matches argument |
| 448 | | && x.matches('x++') |
| 449 | | ................^`, |
| 450 | }, |
| 451 | } |
| 452 | for _, tst := range tests { |
| 453 | tc := tst |
| 454 | t.Run(tc.expr, func(t *testing.T) { |
| 455 | _, iss := env.Compile(tc.expr) |
| 456 | if tc.iss != "" { |
| 457 | if iss.Err() == nil { |
| 458 | t.Fatalf("e.Compile(%v) returned ast, expected error: %v", tc.expr, tc.iss) |
| 459 | } |
| 460 | if !test.Compare(iss.Err().Error(), tc.iss) { |
| 461 | t.Fatalf("e.Compile(%v) returned %v, expected error: %v", tc.expr, iss.Err(), tc.iss) |
| 462 | } |
| 463 | return |
| 464 | } |
| 465 | if iss.Err() != nil { |
| 466 | t.Fatalf("e.Compile(%v) failed: %v", tc.expr, iss.Err()) |
| 467 | } |
| 468 | }) |
| 469 | } |
| 470 | } |
| 471 | |
| 472 | func TestValidatorConfig(t *testing.T) { |
| 473 | config := newValidatorConfig() |