MCPcopy
hub / github.com/danielgtaylor/huma / Validate

Function Validate

validate.go:392–589  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

390// fmt.Println(err.Error())
391// }
392func 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)

Callers 8

validateOneOfFunction · 0.85
validateAnyOfFunction · 0.85
validateDiscriminatorFunction · 0.85
handleArrayFunction · 0.85
handleMapStringFunction · 0.85
handleMapAnyFunction · 0.85
ValidateMethod · 0.85
RegisterFunction · 0.85

Calls 9

validateDiscriminatorFunction · 0.85
validateOneOfFunction · 0.85
validateAnyOfFunction · 0.85
validateFormatFunction · 0.85
handleArrayFunction · 0.85
handleMapStringFunction · 0.85
handleMapAnyFunction · 0.85
SchemaFromRefMethod · 0.65
AddMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…