|
1 | 1 | package validate_test
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "encoding/json" |
4 | 5 | "fmt"
|
5 | 6 | "testing"
|
6 | 7 | "time"
|
@@ -297,6 +298,100 @@ func TestPtrFieldValidation(t *testing.T) {
|
297 | 298 | assert.False(t, valid.Validate())
|
298 | 299 | }
|
299 | 300 |
|
| 301 | +type Pet struct { |
| 302 | + Breed string `json:"breed" validate:"min_len:3"` |
| 303 | + Color string `json:"color" validate:"in:orange,black,brown"` |
| 304 | +} |
| 305 | +type Human struct { |
| 306 | + Age int `json:"age" validate:"gt:13"` |
| 307 | + Name string `json:"name" validate:"min_len:3"` |
| 308 | +} |
| 309 | +type Settings struct { |
| 310 | + *Pet `json:",omitempty"` |
| 311 | + *Human `json:",omitempty"` |
| 312 | +} |
| 313 | + |
| 314 | +type Entity struct { |
| 315 | + ID string `json:"id" validate:"uuid4"` |
| 316 | + Kind string `json:"kind" validate:"in:pet,human"` |
| 317 | + Settings Settings `json:"settings"` |
| 318 | +} |
| 319 | + |
| 320 | +func (Entity) ConfigValidation(v *validate.Validation) { |
| 321 | + v.WithScenes(validate.SValues{ |
| 322 | + "required": []string{"ID", "Kind"}, |
| 323 | + "pet": []string{"Settings.Pet"}, |
| 324 | + "human": []string{"Settings.Human"}, |
| 325 | + }) |
| 326 | + |
| 327 | +} |
| 328 | + |
| 329 | +func TestPtrFieldEmbeddedValid(t *testing.T) { |
| 330 | + // Valid case :) |
| 331 | + input := []byte(`{ |
| 332 | + "id": "59fcf270-8646-4250-b4ff-d50f6121bc9d", |
| 333 | + "kind": "pet", |
| 334 | + "settings": { |
| 335 | + "breed": "dog", |
| 336 | + "color": "brown" |
| 337 | + } |
| 338 | + }`) |
| 339 | + var entity Entity |
| 340 | + err := json.Unmarshal(input, &entity) |
| 341 | + fmt.Println(entity) |
| 342 | + assert.NoError(t, err) |
| 343 | + |
| 344 | + validate.Config(func(opt *validate.GlobalOption) { |
| 345 | + opt.SkipOnEmpty = false |
| 346 | + opt.StopOnError = false |
| 347 | + }) |
| 348 | + |
| 349 | + vld := validate.Struct(&entity) |
| 350 | + vld.SkipOnEmpty = false |
| 351 | + err = vld.ValidateE("required") |
| 352 | + assert.Empty(t, err) |
| 353 | + vld.ResetResult() |
| 354 | + err = vld.ValidateE(entity.Kind) |
| 355 | + assert.Empty(t, err) |
| 356 | + |
| 357 | + validate.ResetOption() |
| 358 | +} |
| 359 | + |
| 360 | +func TestPtrFieldEmbeddedInvalid(t *testing.T) { |
| 361 | + // Invalid case |
| 362 | + input := []byte(`{ |
| 363 | + "id": "59fcf270-8646-4250-b4ff-d50f6121bc9d", |
| 364 | + "kind": "pet", |
| 365 | + "settings": { |
| 366 | + } |
| 367 | + }`) |
| 368 | + var entity Entity |
| 369 | + err := json.Unmarshal(input, &entity) |
| 370 | + fmt.Println(entity) |
| 371 | + assert.NoError(t, err) |
| 372 | + |
| 373 | + validate.Config(func(opt *validate.GlobalOption) { |
| 374 | + opt.SkipOnEmpty = false |
| 375 | + opt.StopOnError = false |
| 376 | + }) |
| 377 | + vld := validate.Struct(&entity) |
| 378 | + err = vld.ValidateE("required") |
| 379 | + assert.Empty(t, err) |
| 380 | + vld.ResetResult() |
| 381 | + err = vld.ValidateE(entity.Kind) |
| 382 | + expected := validate.Errors{ |
| 383 | + "breed": validate.MS{ |
| 384 | + "min_len": "breed min length is 3", |
| 385 | + }, |
| 386 | + "color": validate.MS{ |
| 387 | + "in": "color value must be in the enum [orange black brown]", |
| 388 | + }, |
| 389 | + } |
| 390 | + |
| 391 | + assert.Equal(t, expected, err) |
| 392 | + validate.ResetOption() |
| 393 | +} |
| 394 | + |
300 | 395 | // ----- test case structs
|
301 | 396 |
|
302 | 397 | type Org struct {
|
|
0 commit comments