Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Implemented Struct().Pick(), Struct().Omit() and Struct().Extend() #53

Merged
merged 3 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/docs/core-concepts/1-anatomy-of-schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ type Test struct {
type TestFunc = func(val any, ctx ParseCtx) bool
```

You can view all the default tests that come with each [schema type here.](/schema-types)
You can view all the default tests that come with each [schema type here.](/zog-schemas)

##### Creating Custom Tests

Expand Down
32 changes: 30 additions & 2 deletions docs/docs/schema-types.md → docs/docs/zog-schemas.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,27 @@
sidebar_position: 5
---

# Schema types
# Zog Schemas

## Generic Schema Methods

These are methods that can generally be called on any schema type (Some exceptions might exist).

```go

schema.Test(test) // create a custom test
schema.Required() // marks field as required
schema.Optional() // marks field as optional
schema.Default(value) // sets default value for field
schema.Catch(value) // sets catch value for field
schema.PreTransform(fn) // adds a pre-transformation function to the schema
schema.PostTransform(fn) // adds a post-transformation function to the schema

// VALIDATION METHODS
schema.Parse(data, dest) // parses the data into the destination
```

## Schema Types

```go
// Primtives. Calling .Parse() on these will return []ZogError
Expand All @@ -17,7 +37,7 @@ z.Struct(z.Schema{
"name": z.String(),
})
z.Slice(z.String())

z.Ptr(z.String()) // validates pointer to string
```

## Primtive Types
Expand Down Expand Up @@ -90,6 +110,13 @@ s := z.Struct(z.Schema{
"name": String().Required(),
"age": Int().Required(),
})

// UTILITIES
schema.Pick("key1", map[string]bool{"a": true, "b": false}) // creates a new shallow copy of the schema with only the specified fields. It supports both string keys and map[string]bool
schema.Omit("key1", map[string]bool{"a": true, "b": false}) // creates a new shallow copy of the schema omiting the specified fields. It supports both string keys and map[string]bool

schema.Extend(z.Schema{"a": z.String()}) // creates a new shallow copy of the schema with the additional fields
schema.Merge(otherSchema, otherSchema2) // merges two or more schemas into a new schema. Last schema takes precedence for conflicting keys
// Tests / Validators
// None right now
```
Expand All @@ -99,6 +126,7 @@ s := z.Struct(z.Schema{
```go
// usage
schema := z.Slice(String())

// Tests / Validators
z.Slice(Int()).Min(5) // validates slice has at least 5 elements
z.Slice(Float()).Max(5) // validates slice has at most 5 elements
Expand Down
38 changes: 0 additions & 38 deletions struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package zog

import (
"fmt"
"maps"
"reflect"
"strings"

Expand Down Expand Up @@ -140,43 +139,6 @@ func Struct(schema Schema) *structProcessor {
}
}

// WARNING. THIS WILL PROBABLY BE DEPRECATED SOON IN FAVOR OF z.Merge(schema1, schema2)
func (v *structProcessor) Merge(other *structProcessor) *structProcessor {
new := &structProcessor{
// preTransforms: make([]p.PreTransform, len(v.preTransforms)+len(other.preTransforms)),
// postTransforms: make([]p.PostTransform, len(v.postTransforms)+len(other.postTransforms)),
// tests: make([]p.Test, len(v.tests)+len(other.tests)),
preTransforms: make([]p.PreTransform, 0),
postTransforms: make([]p.PostTransform, 0),
tests: make([]p.Test, 0),
}
if v.preTransforms != nil {
new.preTransforms = append(new.preTransforms, v.preTransforms...)
}
if other.preTransforms != nil {
new.preTransforms = append(new.preTransforms, other.preTransforms...)
}

if v.postTransforms != nil {
new.postTransforms = append(new.postTransforms, v.postTransforms...)
}
if other.postTransforms != nil {
new.postTransforms = append(new.postTransforms, other.postTransforms...)
}

if v.tests != nil {
new.tests = append(new.tests, v.tests...)
}
if other.tests != nil {
new.tests = append(new.tests, other.tests...)
}
new.required = v.required
new.schema = Schema{}
maps.Copy(new.schema, v.schema)
maps.Copy(new.schema, other.schema)
return new
}

// Parses val into destPtr and validates each field based on the schema. Only supports val = map[string]any & dest = &struct
func (v *structProcessor) Parse(data any, destPtr any, options ...ParsingOption) p.ZogErrMap {
errs := p.NewErrsMap()
Expand Down
Loading
Loading