| 346 | } |
| 347 | |
| 348 | func TestExtendedValidations(t *testing.T) { |
| 349 | env, err := NewEnv( |
| 350 | Variable("x", types.StringType), |
| 351 | ExtendedValidations(), |
| 352 | ) |
| 353 | if err != nil { |
| 354 | t.Fatalf("NewEnv(ExtendedValidations()) failed: %v", err) |
| 355 | } |
| 356 | tests := []struct { |
| 357 | expr string |
| 358 | iss string |
| 359 | }{ |
| 360 | { |
| 361 | expr: `x in ['hello', 0] |
| 362 | && duration(x) < duration('1d') |
| 363 | && timestamp(x) != timestamp('1000-01-00T00:00:00Z') |
| 364 | && x.matches('x++')`, |
| 365 | iss: ` |
| 366 | ERROR: <input>:1:16: expected type 'string' but found 'int' |
| 367 | | x in ['hello', 0] |
| 368 | | ...............^ |
| 369 | ERROR: <input>:2:30: invalid duration argument |
| 370 | | && duration(x) < duration('1d') |
| 371 | | .............................^ |
| 372 | ERROR: <input>:3:33: invalid timestamp argument |
| 373 | | && timestamp(x) != timestamp('1000-01-00T00:00:00Z') |
| 374 | | ................................^ |
| 375 | ERROR: <input>:4:17: invalid matches argument |
| 376 | | && x.matches('x++') |
| 377 | | ................^`, |
| 378 | }, |
| 379 | } |
| 380 | for _, tst := range tests { |
| 381 | tc := tst |
| 382 | t.Run(tc.expr, func(t *testing.T) { |
| 383 | _, iss := env.Compile(tc.expr) |
| 384 | if tc.iss != "" { |
| 385 | if iss.Err() == nil { |
| 386 | t.Fatalf("e.Compile(%v) returned ast, expected error: %v", tc.expr, tc.iss) |
| 387 | } |
| 388 | if !test.Compare(iss.Err().Error(), tc.iss) { |
| 389 | t.Fatalf("e.Compile(%v) returned %v, expected error: %v", tc.expr, iss.Err(), tc.iss) |
| 390 | } |
| 391 | return |
| 392 | } |
| 393 | if iss.Err() != nil { |
| 394 | t.Fatalf("e.Compile(%v) failed: %v", tc.expr, iss.Err()) |
| 395 | } |
| 396 | }) |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | func TestValidatorConfig(t *testing.T) { |
| 401 | config := newValidatorConfig() |