|
1 | 1 | package validator
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "reflect" |
4 | 5 | "testing"
|
5 | 6 |
|
6 | 7 | "github.com/yannh/kubeconform/pkg/registry"
|
@@ -379,3 +380,58 @@ lastName: bar
|
379 | 380 | }
|
380 | 381 | }
|
381 | 382 | }
|
| 383 | + |
| 384 | +func TestValidationErrors(t *testing.T) { |
| 385 | + rawResource := []byte(` |
| 386 | +kind: name |
| 387 | +apiVersion: v1 |
| 388 | +firstName: foo |
| 389 | +age: not a number |
| 390 | +`) |
| 391 | + |
| 392 | + schema := []byte(`{ |
| 393 | + "title": "Example Schema", |
| 394 | + "type": "object", |
| 395 | + "properties": { |
| 396 | + "kind": { |
| 397 | + "type": "string" |
| 398 | + }, |
| 399 | + "firstName": { |
| 400 | + "type": "string" |
| 401 | + }, |
| 402 | + "lastName": { |
| 403 | + "type": "string" |
| 404 | + }, |
| 405 | + "age": { |
| 406 | + "description": "Age in years", |
| 407 | + "type": "integer", |
| 408 | + "minimum": 0 |
| 409 | + } |
| 410 | + }, |
| 411 | + "required": ["firstName", "lastName"] |
| 412 | +}`) |
| 413 | + |
| 414 | + expectedErrors := []ValidationError{ |
| 415 | + {Path: "", Msg: "missing properties: 'lastName'"}, |
| 416 | + {Path: "/age", Msg: "expected integer, but got string"}, |
| 417 | + } |
| 418 | + |
| 419 | + val := v{ |
| 420 | + opts: Opts{ |
| 421 | + SkipKinds: map[string]struct{}{}, |
| 422 | + RejectKinds: map[string]struct{}{}, |
| 423 | + }, |
| 424 | + schemaCache: nil, |
| 425 | + schemaDownload: downloadSchema, |
| 426 | + regs: []registry.Registry{ |
| 427 | + newMockRegistry(func() (string, []byte, error) { |
| 428 | + return "", schema, nil |
| 429 | + }), |
| 430 | + }, |
| 431 | + } |
| 432 | + |
| 433 | + got := val.ValidateResource(resource.Resource{Bytes: rawResource}) |
| 434 | + if !reflect.DeepEqual(expectedErrors, got.ValidationErrors) { |
| 435 | + t.Errorf("Expected %+v, got %+v", expectedErrors, got.ValidationErrors) |
| 436 | + } |
| 437 | +} |
0 commit comments