Skip to content

Commit

Permalink
add examples
Browse files Browse the repository at this point in the history
  • Loading branch information
aacebo committed Oct 6, 2024
1 parent 33cace7 commit cd1ff81
Show file tree
Hide file tree
Showing 10 changed files with 287 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ run:
fmt:
gofmt -w ./

make doc:
godoc -http=:6060

test:
go clean -testcache
go test ./... -cover -coverprofile=coverage.out
Expand Down
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,19 @@
<img src="https://codecov.io/gh/aacebo/owl/graph/badge.svg?token=9XETRUUQUY" />
</a>
</p>

# Install

```bash
go get github.com/aacebo/owl
```

# Usage

```go
schema := owl.String().Required()

if err := schema.Validate("..."); err != nil { // nil
panic(err)
}
```
16 changes: 16 additions & 0 deletions any_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,19 @@ func Test_Any(t *testing.T) {
})
})
}

func ExampleAny() {
schema := owl.Any()

if err := schema.Validate("..."); err != nil { // nil
panic(err)
}

if err := schema.Validate(1); err != nil { // nil
panic(err)
}

if err := schema.Validate(true); err != nil { // nil
panic(err)
}
}
16 changes: 16 additions & 0 deletions bool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,19 @@ func Test_Bool(t *testing.T) {
})
})
}

func ExampleBool() {
schema := owl.Bool()

if err := schema.Validate(true); err != nil { // nil
panic(err)
}

if err := schema.Validate(false); err != nil { // nil
panic(err)
}

if err := schema.Validate("test"); err != nil { // error
panic(err)
}
}
40 changes: 40 additions & 0 deletions float_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,43 @@ func Test_Float(t *testing.T) {
})
})
}

func ExampleFloat() {
schema := owl.Float()

if err := schema.Validate(1.0); err != nil { // nil
panic(err)
}

if err := schema.Validate(1); err != nil { // nil
panic(err)
}

if err := schema.Validate("test"); err != nil { // error
panic(err)
}
}

func ExampleFloatSchema_Min() {
schema := owl.Float().Min(5.0)

if err := schema.Validate(5); err != nil { // nil
panic(err)
}

if err := schema.Validate(4.5); err != nil { // error
panic(err)
}
}

func ExampleFloatSchema_Max() {
schema := owl.Float().Max(5.0)

if err := schema.Validate(5); err != nil { // nil
panic(err)
}

if err := schema.Validate(5.5); err != nil { // error
panic(err)
}
}
40 changes: 40 additions & 0 deletions int_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,43 @@ func Test_Int(t *testing.T) {
})
})
}

func ExampleInt() {
schema := owl.Int()

if err := schema.Validate(1); err != nil { // nil
panic(err)
}

if err := schema.Validate(1); err != nil { // nil
panic(err)
}

if err := schema.Validate("test"); err != nil { // error
panic(err)
}
}

func ExampleIntSchema_Min() {
schema := owl.Int().Min(5)

if err := schema.Validate(5); err != nil { // nil
panic(err)
}

if err := schema.Validate(4); err != nil { // error
panic(err)
}
}

func ExampleIntSchema_Max() {
schema := owl.Int().Max(5)

if err := schema.Validate(5); err != nil { // nil
panic(err)
}

if err := schema.Validate(6); err != nil { // error
panic(err)
}
}
21 changes: 21 additions & 0 deletions object_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,24 @@ func Test_Object(t *testing.T) {
})
})
}

func ExampleObject() {
schema := owl.Object().Field(
"email", owl.String().Email().Required(),
).Field(
"password", owl.String().Min(5).Max(20).Required(),
)

if err := schema.Validate(map[string]any{
"email": "[email protected]",
"password": "mytestpassword",
}); err != nil { // nil
panic(err)
}

if err := schema.Validate(map[string]any{
"email": "[email protected]",
}); err != nil { // error
panic(err)
}
}
84 changes: 84 additions & 0 deletions string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,87 @@ func Test_String(t *testing.T) {
})
})
}

func ExampleString() {
schema := owl.String()

if err := schema.Validate("test"); err != nil { // nil
panic(err)
}

if err := schema.Validate(true); err != nil { // error
panic(err)
}
}

func ExampleStringSchema_Min() {
schema := owl.String().Min(5)

if err := schema.Validate("tester"); err != nil { // nil
panic(err)
}

if err := schema.Validate("test"); err != nil { // error
panic(err)
}
}

func ExampleStringSchema_Max() {
schema := owl.String().Max(5)

if err := schema.Validate("test"); err != nil { // nil
panic(err)
}

if err := schema.Validate("tester"); err != nil { // error
panic(err)
}
}

func ExampleStringSchema_Regex() {
schema := owl.String().Regex(regexp.MustCompile("^[0-9a-zA-Z_-]+$"))

if err := schema.Validate("test"); err != nil { // nil
panic(err)
}

if err := schema.Validate("hello world"); err != nil { // error
panic(err)
}
}

func ExampleStringSchema_Email() {
schema := owl.String().Email()

if err := schema.Validate("[email protected]"); err != nil { // nil
panic(err)
}

if err := schema.Validate("test"); err != nil { // error
panic(err)
}
}

func ExampleStringSchema_UUID() {
schema := owl.String().UUID()

if err := schema.Validate("afefc1ab-b8f2-4803-8e9a-60515854141a"); err != nil { // nil
panic(err)
}

if err := schema.Validate("test"); err != nil { // error
panic(err)
}
}

func ExampleStringSchema_URL() {
schema := owl.String().URL()

if err := schema.Validate("https://www.google.com"); err != nil { // nil
panic(err)
}

if err := schema.Validate("test"); err != nil { // error
panic(err)
}
}
32 changes: 32 additions & 0 deletions time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,35 @@ func Test_Time(t *testing.T) {
})
})
}

func ExampleTime() {
schema := owl.Time()

if err := schema.Validate(time.Now()); err != nil { // nil
panic(err)
}

if err := schema.Validate(time.Now().Format(time.RFC3339)); err != nil { // nil
panic(err)
}

if err := schema.Validate("test"); err != nil { // error
panic(err)
}
}

func ExampleTimeSchema_Min() {
schema := owl.Time().Min(time.Now())

if err := schema.Validate(time.Now().AddDate(-1, 0, 0)); err != nil { // error
panic(err)
}
}

func ExampleTimeSchema_Max() {
schema := owl.Time().Max(time.Now())

if err := schema.Validate(time.Now().AddDate(1, 0, 0)); err != nil { // error
panic(err)
}
}
19 changes: 19 additions & 0 deletions union_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,22 @@ func Test_Union(t *testing.T) {
})
})
}

func ExampleUnion() {
schema := owl.Union(
owl.String().Required(),
owl.Int().Required(),
)

if err := schema.Validate("test"); err != nil { // nil
panic(err)
}

if err := schema.Validate(1); err != nil { // nil
panic(err)
}

if err := schema.Validate(true); err != nil { // error
panic(err)
}
}

0 comments on commit cd1ff81

Please sign in to comment.