-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathvalidation.go
90 lines (87 loc) · 3.13 KB
/
validation.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Package gollm provides validation functionality for Language Learning Model interactions.
// This file contains utilities for validating structured data and generating JSON schemas,
// which are essential for ensuring proper data formats in LLM communications.
package gollm
import (
"github.com/teilomillet/gollm/llm"
)
// Validate checks if the given struct is valid according to its validation rules.
// It uses struct tags to define validation rules and performs comprehensive validation
// of the input structure.
//
// The function supports various validation rules through struct tags, including:
// - required: Field must be present and non-zero
// - min/max: Numeric range validation
// - len: Exact length requirement
// - email: Email format validation
// - url: URL format validation
// - regex: Pattern matching
// - contains/excludes: String content validation
// - unique: Array unique items validation
// - minItems/maxItems: Array length validation
// - password: Password strength validation
//
// Example usage:
//
// type Config struct {
// Model string `validate:"required,model"`
// MaxTokens int `validate:"min=1,max=4096"`
// Email string `validate:"required,email"`
// Password string `validate:"required,password=strong"`
// }
//
// config := Config{
// Model: "gpt-4",
// MaxTokens: 2048,
// Email: "user@example.com",
// Password: "SecureP@ss123",
// }
// err := Validate(&config)
//
// Parameters:
// - s: The struct to validate. Must be a pointer to a struct.
//
// Returns:
// - error: nil if validation passes, otherwise returns detailed validation errors
func Validate(s interface{}) error {
return llm.Validate(s)
}
// GenerateJSONSchema generates a JSON schema for the given struct.
// The schema is generated based on struct fields and their tags, providing
// a complete JSON Schema that can be used for validation or documentation.
//
// The function analyzes struct fields and generates a schema that includes:
// - Field types and formats
// - Required fields
// - Validation rules from struct tags
// - Nested object structures
// - Array specifications
// - Custom validation rules
// - Format constraints
//
// Example usage:
//
// type Message struct {
// Role string `json:"role" validate:"required,oneof=system user assistant"`
// Content string `json:"content" validate:"required,min=1"`
// Tokens int `json:"tokens,omitempty" validate:"min=0"`
// Tags []string `json:"tags,omitempty" validate:"unique"`
// }
//
// type Conversation struct {
// ID string `json:"id" validate:"required,uuid"`
// Messages []Message `json:"messages" validate:"required,min=1"`
// Model string `json:"model" validate:"required,model"`
// }
//
// schema, err := GenerateJSONSchema(&Conversation{})
//
// Parameters:
// - v: The struct to generate schema for. Must be a pointer to a struct.
//
// Returns:
// - []byte: The generated JSON schema as a byte slice
// - error: Any error encountered during schema generation
func GenerateJSONSchema(v interface{}) ([]byte, error) {
return llm.GenerateJSONSchema(v)
}