Skip to content

Commit

Permalink
stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
aacebo committed Oct 7, 2024
1 parent 9971ffe commit d07ac62
Show file tree
Hide file tree
Showing 14 changed files with 240 additions and 6 deletions.
4 changes: 2 additions & 2 deletions any.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (self AnySchema) Validate(value any) error {
}

func (self AnySchema) validate(key string, value reflect.Value) error {
err := newErrorGroup(key)
err := NewErrorGroup(key)

for _, rule := range self.rules {
if rule.Resolve == nil {
Expand All @@ -85,7 +85,7 @@ func (self AnySchema) validate(key string, value reflect.Value) error {
v, e := rule.Resolve(value)

if e != nil {
err = err.Add(newError(rule.Key, key, e.Error()))
err = err.Add(NewError(rule.Key, key, e.Error()))
continue
}

Expand Down
20 changes: 20 additions & 0 deletions any_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package owl_test

import (
"encoding/json"
"testing"

"github.com/aacebo/owl"
Expand Down Expand Up @@ -42,6 +43,25 @@ func Test_Any(t *testing.T) {
}
})
})

t.Run("json", func(t *testing.T) {
t.Run("serialize", func(t *testing.T) {
schema := owl.Any().Enum(1, true, "hi").Required()
b, err := json.Marshal(schema)

if err != nil {
t.Error(err)
}

if string(b) != `{"enum":[1,true,"hi"],"required":true,"type":"any"}` {
t.Errorf(
"expected `%s`, received `%s`",
`{"enum":[1,true,"hi"],"required":true,"type":"any"}`,
string(b),
)
}
})
})
}

func ExampleAny() {
Expand Down
16 changes: 16 additions & 0 deletions bool_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package owl_test

import (
"encoding/json"
"testing"

"github.com/aacebo/owl"
Expand Down Expand Up @@ -42,6 +43,21 @@ func Test_Bool(t *testing.T) {
}
})
})

t.Run("json", func(t *testing.T) {
t.Run("serialize", func(t *testing.T) {
schema := owl.Bool()
b, err := json.Marshal(schema)

if err != nil {
t.Error(err)
}

if string(b) != `{"type":"bool"}` {
t.Errorf("expected `%s`, received `%s`", `{"type":"bool"}`, string(b))
}
})
})
}

func ExampleBool() {
Expand Down
2 changes: 1 addition & 1 deletion error.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type Error struct {
Message string `json:"message,omitempty"`
}

func newError(rule string, key string, message string) Error {
func NewError(rule string, key string, message string) Error {
return Error{
Rule: rule,
Key: key,
Expand Down
2 changes: 1 addition & 1 deletion error_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ type ErrorGroup struct {
Errors []error `json:"errors,omitempty"`
}

func newErrorGroup(key string) ErrorGroup {
func NewErrorGroup(key string) ErrorGroup {
return ErrorGroup{
Key: key,
Errors: []error{},
Expand Down
34 changes: 34 additions & 0 deletions error_group_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package owl_test

import (
"testing"

"github.com/aacebo/owl"
)

func Test_ErrorGroup(t *testing.T) {
t.Run("should serialize", func(t *testing.T) {
schema := owl.Object().Fields(map[string]owl.Schema{
"a": owl.String().Required(),
"b": owl.Bool(),
"c": owl.Int(),
})

err := schema.Validate(map[string]any{
"b": 1.0,
"c": true,
})

if err == nil {
t.FailNow()
}

if len(err.Error()) != 507 {
t.Errorf(
"expected `%d`, received `%d`",
507,
len(err.Error()),
)
}
})
}
26 changes: 26 additions & 0 deletions error_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package owl_test

import (
"testing"

"github.com/aacebo/owl"
)

func Test_Error(t *testing.T) {
t.Run("should serialize", func(t *testing.T) {
schema := owl.String()
err := schema.Validate(1)

if err == nil {
t.FailNow()
}

if len(err.Error()) != 91 {
t.Errorf(
"expected `%d`, received `%d`",
91,
len(err.Error()),
)
}
})
}
20 changes: 20 additions & 0 deletions float_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package owl_test

import (
"encoding/json"
"testing"

"github.com/aacebo/owl"
Expand Down Expand Up @@ -78,6 +79,25 @@ func Test_Float(t *testing.T) {
}
})
})

t.Run("json", func(t *testing.T) {
t.Run("serialize", func(t *testing.T) {
schema := owl.Float().Min(1).Max(5)
b, err := json.Marshal(schema)

if err != nil {
t.Error(err)
}

if string(b) != `{"max":5,"min":1,"type":"float"}` {
t.Errorf(
"expected `%s`, received `%s`",
`{"max":5,"min":1,"type":"float"}`,
string(b),
)
}
})
})
}

func ExampleFloat() {
Expand Down
20 changes: 20 additions & 0 deletions int_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package owl_test

import (
"encoding/json"
"testing"

"github.com/aacebo/owl"
Expand Down Expand Up @@ -78,6 +79,25 @@ func Test_Int(t *testing.T) {
}
})
})

t.Run("json", func(t *testing.T) {
t.Run("serialize", func(t *testing.T) {
schema := owl.Int().Min(1).Max(5)
b, err := json.Marshal(schema)

if err != nil {
t.Error(err)
}

if string(b) != `{"max":5,"min":1,"type":"int"}` {
t.Errorf(
"expected `%s`, received `%s`",
`{"max":5,"min":1,"type":"int"}`,
string(b),
)
}
})
})
}

func ExampleInt() {
Expand Down
12 changes: 10 additions & 2 deletions object.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ func (self *ObjectSchema) Field(key string, schema Schema) *ObjectSchema {
return self
}

func (self *ObjectSchema) Fields(fields map[string]Schema) *ObjectSchema {
for key, schema := range fields {
self.fields[key] = schema
}

return self
}

func (self ObjectSchema) MarshalJSON() ([]byte, error) {
return json.Marshal(self.schema)
}
Expand Down Expand Up @@ -77,7 +85,7 @@ func (self ObjectSchema) validate(key string, value reflect.Value) error {
}

func (self ObjectSchema) validateMap(key string, value reflect.Value) error {
err := newErrorGroup(key)
err := NewErrorGroup(key)

for name, schema := range self.fields {
k := reflect.ValueOf(name)
Expand All @@ -100,7 +108,7 @@ func (self ObjectSchema) validateMap(key string, value reflect.Value) error {
}

func (self ObjectSchema) validateStruct(key string, value reflect.Value) error {
err := newErrorGroup(key)
err := NewErrorGroup(key)

for name, schema := range self.fields {
fieldName, exists := self.getStructFieldByName(name, value)
Expand Down
26 changes: 26 additions & 0 deletions object_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package owl_test

import (
"encoding/json"
"regexp"
"testing"

"github.com/aacebo/owl"
Expand Down Expand Up @@ -126,6 +128,30 @@ func Test_Object(t *testing.T) {
}
})
})

t.Run("json", func(t *testing.T) {
t.Run("serialize", func(t *testing.T) {
schema := owl.Object().Fields(map[string]owl.Schema{
"username": owl.String().Regex(regexp.MustCompile("^[0-9a-zA-Z_-]+$")).Required(),
"password": owl.String().Min(5).Max(20).Required(),
"staySignedIn": owl.Bool(),
})

b, err := json.Marshal(schema)

if err != nil {
t.Error(err)
}

if string(b) != `{"fields":{"password":{"max":20,"min":5,"required":true,"type":"string"},"staySignedIn":{"type":"bool"},"username":{"regex":"^[0-9a-zA-Z_-]+$","required":true,"type":"string"}},"type":"object"}` {
t.Errorf(
"expected `%s`, received `%s`",
`{"fields":{"password":{"max":20,"min":5,"required":true,"type":"string"},"staySignedIn":{"type":"bool"},"username":{"regex":"^[0-9a-zA-Z_-]+$","required":true,"type":"string"}},"type":"object"}`,
string(b),
)
}
})
})
}

func ExampleObject() {
Expand Down
20 changes: 20 additions & 0 deletions string_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package owl_test

import (
"encoding/json"
"regexp"
"testing"

Expand Down Expand Up @@ -151,6 +152,25 @@ func Test_String(t *testing.T) {
}
})
})

t.Run("json", func(t *testing.T) {
t.Run("serialize", func(t *testing.T) {
schema := owl.String().Min(1).Max(5).Email()
b, err := json.Marshal(schema)

if err != nil {
t.Error(err)
}

if string(b) != `{"email":true,"max":5,"min":1,"type":"string"}` {
t.Errorf(
"expected `%s`, received `%s`",
`{"email":true,"max":5,"min":1,"type":"string"}`,
string(b),
)
}
})
})
}

func ExampleString() {
Expand Down
20 changes: 20 additions & 0 deletions time_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package owl_test

import (
"encoding/json"
"testing"
"time"

Expand Down Expand Up @@ -61,6 +62,25 @@ func Test_Time(t *testing.T) {
}
})
})

t.Run("json", func(t *testing.T) {
t.Run("serialize", func(t *testing.T) {
schema := owl.Time()
b, err := json.Marshal(schema)

if err != nil {
t.Error(err)
}

if string(b) != `{"type":"time"}` {
t.Errorf(
"expected `%s`, received `%s`",
`{"type":"time"}`,
string(b),
)
}
})
})
}

func ExampleTime() {
Expand Down
Loading

0 comments on commit d07ac62

Please sign in to comment.