aboutsummaryrefslogtreecommitdiff
path: root/valid
diff options
context:
space:
mode:
authorXe Iaso <me@xeiaso.net>2024-03-02 22:02:59 -0500
committerXe Iaso <me@xeiaso.net>2024-03-02 22:03:58 -0500
commit590a15a0c3658abf13db39bd3c52021a7ec6ba82 (patch)
treec9db32f7e5da32f0f1a09bd26e096b7963ca2477 /valid
parent7854f16ef4071ef3bd2f7574a45462f53291a4be (diff)
downloadx-590a15a0c3658abf13db39bd3c52021a7ec6ba82.tar.xz
x-590a15a0c3658abf13db39bd3c52021a7ec6ba82.zip
web/ollama: add Hallucinate function
Signed-off-by: Xe Iaso <me@xeiaso.net>
Diffstat (limited to 'valid')
-rw-r--r--valid/example_test.go39
-rw-r--r--valid/valid.go31
2 files changed, 70 insertions, 0 deletions
diff --git a/valid/example_test.go b/valid/example_test.go
new file mode 100644
index 0000000..e321ad9
--- /dev/null
+++ b/valid/example_test.go
@@ -0,0 +1,39 @@
+package valid
+
+import (
+ "errors"
+ "log/slog"
+)
+
+type Example struct {
+ Name string
+}
+
+func (e *Example) Valid() error {
+ var errs []error
+ if e.Name == "" {
+ errs = append(errs, errors.New("name is empty"))
+ }
+ if len(errs) == 0 {
+ return nil
+ }
+ return errors.Join(errs...)
+}
+
+func ExampleValider_Valid() {
+ e := Example{Name: ""}
+
+ if err := e.Valid(); err != nil {
+ slog.Error("validation failed", "err", err)
+ }
+
+ // Output:
+ // validation failed err=name is empty
+
+ e.Name = "ollama"
+ if err := e.Valid(); err != nil {
+ slog.Error("validation failed", "err", err)
+ }
+ // Output:
+ //
+}
diff --git a/valid/valid.go b/valid/valid.go
new file mode 100644
index 0000000..24f8884
--- /dev/null
+++ b/valid/valid.go
@@ -0,0 +1,31 @@
+// Package valid contains the Valider interface.
+//
+// The Valider interface is used to verify the "validity" of a value, where "validity" is defined by the implementing type.
+//
+// All examples are assuming the following struct:
+//
+// type Example struct {
+// Name string
+// }
+//
+// Where possible, implementors should use errors.Join to allow for multiple validation errors to be returned:
+//
+// func (e *Example) Valid() error {
+// var errs []error
+// if e.Name == "" {
+// errs = append(errs, errors.New("name is empty"))
+// }
+// if len(errs) == 0 {
+// return nil
+// }
+// return errors.Join(errs...)
+// }
+package valid
+
+// Interface is an interface for types that can be validated.
+type Interface interface {
+ // Valid returns any validation errors for the value. If the value is valid, nil is returned.
+ //
+ // Where possible, implementors should use errors.Join to allow for multiple validation errors to be returned.
+ Valid() error
+}