Validate an input value against a schema, collecting errors in the validation result object. If successful, `res.Errors` will be empty. It is suggested to use a `sync.Pool` to reuse the PathBuffer and ValidateResult objects, making sure to call `Reset()` on them before returning them to the pool.
(r Registry, s *Schema, path *PathBuffer, mode ValidateMode, v any, res *ValidateResult)
| 390 | // fmt.Println(err.Error()) |
| 391 | // } |
| 392 | func Validate(r Registry, s *Schema, path *PathBuffer, mode ValidateMode, v any, res *ValidateResult) { |
| 393 | // Get the actual schema if this is a reference. |
| 394 | for s.Ref != "" { |
| 395 | s = r.SchemaFromRef(s.Ref) |
| 396 | } |
| 397 | |
| 398 | if s.OneOf != nil { |
| 399 | if s.Discriminator != nil { |
| 400 | validateDiscriminator(r, s, path, mode, v, res) |
| 401 | } else { |
| 402 | validateOneOf(r, s, path, mode, v, res) |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | if s.AnyOf != nil { |
| 407 | validateAnyOf(r, s, path, mode, v, res) |
| 408 | } |
| 409 | |
| 410 | if s.AllOf != nil { |
| 411 | for _, sub := range s.AllOf { |
| 412 | Validate(r, sub, path, mode, v, res) |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | if s.Not != nil { |
| 417 | subRes := &ValidateResult{} |
| 418 | Validate(r, s.Not, path, mode, v, subRes) |
| 419 | if len(subRes.Errors) == 0 { |
| 420 | res.Add(path, v, validation.MsgExpectedNotMatchSchema) |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | if s.Nullable && v == nil { |
| 425 | return |
| 426 | } |
| 427 | |
| 428 | switch s.Type { |
| 429 | case TypeBoolean: |
| 430 | if _, ok := v.(bool); !ok { |
| 431 | res.Add(path, v, validation.MsgExpectedBoolean) |
| 432 | return |
| 433 | } |
| 434 | case TypeNumber, TypeInteger: |
| 435 | var num float64 |
| 436 | |
| 437 | switch v := v.(type) { |
| 438 | case float64: |
| 439 | num = v |
| 440 | case float32: |
| 441 | num = float64(v) |
| 442 | case int: |
| 443 | num = float64(v) |
| 444 | case int8: |
| 445 | num = float64(v) |
| 446 | case int16: |
| 447 | num = float64(v) |
| 448 | case int32: |
| 449 | num = float64(v) |
no test coverage detected
searching dependent graphs…