diff --git a/boolean.go b/boolean.go index a727230..db9ae4c 100644 --- a/boolean.go +++ b/boolean.go @@ -9,13 +9,13 @@ import ( var _ PrimitiveZogSchema[bool] = &BoolSchema[bool]{} type BoolSchema[T ~bool] struct { - preTransforms []p.PreTransform - tests []p.Test - postTransforms []p.PostTransform + preTransforms []PreTransform + tests []Test + postTransforms []PostTransform defaultVal *T - required *p.Test + required *Test catch *T - coercer conf.CoercerFunc + coercer CoercerFunc } // ! INTERNALS @@ -26,7 +26,7 @@ func (v *BoolSchema[T]) getType() zconst.ZogType { } // Sets the coercer for the schema -func (v *BoolSchema[T]) setCoercer(c conf.CoercerFunc) { +func (v *BoolSchema[T]) setCoercer(c CoercerFunc) { v.coercer = c } @@ -44,7 +44,7 @@ func Bool(opts ...SchemaOption) *BoolSchema[bool] { } // Parse data into destination pointer -func (v *BoolSchema[T]) Parse(data any, dest *T, options ...ExecOption) p.ZogIssueList { +func (v *BoolSchema[T]) Parse(data any, dest *T, options ...ExecOption) ZogIssueList { errs := p.NewErrsList() defer errs.Free() ctx := p.NewExecCtx(errs, conf.IssueFormatter) @@ -66,7 +66,7 @@ func (v *BoolSchema[T]) process(ctx *p.SchemaCtx) { } // Validate data against schema -func (v *BoolSchema[T]) Validate(val *T, options ...ExecOption) p.ZogIssueList { +func (v *BoolSchema[T]) Validate(val *T, options ...ExecOption) ZogIssueList { errs := p.NewErrsList() defer errs.Free() ctx := p.NewExecCtx(errs, conf.IssueFormatter) @@ -90,7 +90,7 @@ func (v *BoolSchema[T]) validate(ctx *p.SchemaCtx) { // GLOBAL METHODS -func (v *BoolSchema[T]) Test(t p.Test, options ...TestOption) *BoolSchema[T] { +func (v *BoolSchema[T]) Test(t Test, options ...TestOption) *BoolSchema[T] { for _, opt := range options { opt(&t) } @@ -107,18 +107,18 @@ func (v *BoolSchema[T]) TestFunc(testFunc p.TestFunc, options ...TestOption) *Bo } // Adds pretransform function to schema -func (v *BoolSchema[T]) PreTransform(transform p.PreTransform) *BoolSchema[T] { +func (v *BoolSchema[T]) PreTransform(transform PreTransform) *BoolSchema[T] { if v.preTransforms == nil { - v.preTransforms = []p.PreTransform{} + v.preTransforms = []PreTransform{} } v.preTransforms = append(v.preTransforms, transform) return v } // Adds posttransform function to schema -func (v *BoolSchema[T]) PostTransform(transform p.PostTransform) *BoolSchema[T] { +func (v *BoolSchema[T]) PostTransform(transform PostTransform) *BoolSchema[T] { if v.postTransforms == nil { - v.postTransforms = []p.PostTransform{} + v.postTransforms = []PostTransform{} } v.postTransforms = append(v.postTransforms, transform) return v diff --git a/numbers.go b/numbers.go index 684d2f7..71b6e39 100644 --- a/numbers.go +++ b/numbers.go @@ -12,11 +12,11 @@ type Numeric = constraints.Ordered var _ PrimitiveZogSchema[int] = &NumberSchema[int]{} type NumberSchema[T Numeric] struct { - preTransforms []p.PreTransform - tests []p.Test - postTransforms []p.PostTransform + preTransforms []PreTransform + tests []Test + postTransforms []PostTransform defaultVal *T - required *p.Test + required *Test catch *T coercer conf.CoercerFunc } @@ -29,7 +29,7 @@ func (v *NumberSchema[T]) getType() zconst.ZogType { } // Sets the coercer for the schema -func (v *NumberSchema[T]) setCoercer(c conf.CoercerFunc) { +func (v *NumberSchema[T]) setCoercer(c CoercerFunc) { v.coercer = c } @@ -120,7 +120,7 @@ func Int32(opts ...SchemaOption) *NumberSchema[int32] { } // parses the value and stores it in the destination -func (v *NumberSchema[T]) Parse(data any, dest *T, options ...ExecOption) p.ZogIssueList { +func (v *NumberSchema[T]) Parse(data any, dest *T, options ...ExecOption) ZogIssueList { errs := p.NewErrsList() defer errs.Free() ctx := p.NewExecCtx(errs, conf.IssueFormatter) @@ -144,7 +144,7 @@ func (v *NumberSchema[T]) process(ctx *p.SchemaCtx) { } // Validates a number pointer -func (v *NumberSchema[T]) Validate(data *T, options ...ExecOption) p.ZogIssueList { +func (v *NumberSchema[T]) Validate(data *T, options ...ExecOption) ZogIssueList { errs := p.NewErrsList() defer errs.Free() ctx := p.NewExecCtx(errs, conf.IssueFormatter) @@ -167,18 +167,18 @@ func (v *NumberSchema[T]) validate(ctx *p.SchemaCtx) { // GLOBAL METHODS -func (v *NumberSchema[T]) PreTransform(transform p.PreTransform) *NumberSchema[T] { +func (v *NumberSchema[T]) PreTransform(transform PreTransform) *NumberSchema[T] { if v.preTransforms == nil { - v.preTransforms = []p.PreTransform{} + v.preTransforms = []PreTransform{} } v.preTransforms = append(v.preTransforms, transform) return v } // Adds posttransform function to schema -func (v *NumberSchema[T]) PostTransform(transform p.PostTransform) *NumberSchema[T] { +func (v *NumberSchema[T]) PostTransform(transform PostTransform) *NumberSchema[T] { if v.postTransforms == nil { - v.postTransforms = []p.PostTransform{} + v.postTransforms = []PostTransform{} } v.postTransforms = append(v.postTransforms, transform) return v @@ -215,7 +215,7 @@ func (v *NumberSchema[T]) Catch(val T) *NumberSchema[T] { } // custom test function call it -> schema.Test(test, options) -func (v *NumberSchema[T]) Test(t p.Test, opts ...TestOption) *NumberSchema[T] { +func (v *NumberSchema[T]) Test(t Test, opts ...TestOption) *NumberSchema[T] { for _, opt := range opts { opt(&t) } diff --git a/pointers.go b/pointers.go index 304b9da..594d85e 100644 --- a/pointers.go +++ b/pointers.go @@ -11,11 +11,11 @@ import ( var _ ComplexZogSchema = &PointerSchema{} type PointerSchema struct { - // preTransforms []p.PreTransform - tests []p.Test + // preTransforms []PreTransform + tests []Test schema ZogSchema - required *p.Test - // postTransforms []p.PostTransform + required *Test + // postTransforms []PostTransform // defaultVal *any // catch *any } @@ -32,13 +32,13 @@ func (v *PointerSchema) setCoercer(c conf.CoercerFunc) { // Ptr creates a pointer ZogSchema func Ptr(schema ZogSchema) *PointerSchema { return &PointerSchema{ - tests: []p.Test{}, + tests: []Test{}, schema: schema, } } // Parse the data into the destination pointer -func (v *PointerSchema) Parse(data any, dest any, options ...ExecOption) p.ZogIssueMap { +func (v *PointerSchema) Parse(data any, dest any, options ...ExecOption) ZogIssueMap { errs := p.NewErrsMap() defer errs.Free() ctx := p.NewExecCtx(errs, conf.IssueFormatter) @@ -94,7 +94,7 @@ func (v *PointerSchema) process(ctx *p.SchemaCtx) { } // Validates a pointer pointer -func (v *PointerSchema) Validate(data any, options ...ExecOption) p.ZogIssueMap { +func (v *PointerSchema) Validate(data any, options ...ExecOption) ZogIssueMap { errs := p.NewErrsMap() defer errs.Free() ctx := p.NewExecCtx(errs, conf.IssueFormatter) @@ -126,7 +126,7 @@ func (v *PointerSchema) validate(ctx *p.SchemaCtx) { // Validate Existing Pointer func (v *PointerSchema) NotNil(options ...TestOption) *PointerSchema { - r := p.Test{ + r := Test{ IssueCode: zconst.IssueCodeNotNil, } for _, opt := range options { diff --git a/slices.go b/slices.go index efd648d..e11a991 100644 --- a/slices.go +++ b/slices.go @@ -13,11 +13,11 @@ import ( var _ ComplexZogSchema = &SliceSchema{} type SliceSchema struct { - preTransforms []p.PreTransform - tests []p.Test + preTransforms []PreTransform + tests []Test schema ZogSchema - postTransforms []p.PostTransform - required *p.Test + postTransforms []PostTransform + required *Test defaultVal any // catch any coercer conf.CoercerFunc @@ -49,7 +49,7 @@ func Slice(schema ZogSchema, opts ...SchemaOption) *SliceSchema { } // Validates a slice -func (v *SliceSchema) Validate(data any, options ...ExecOption) p.ZogIssueMap { +func (v *SliceSchema) Validate(data any, options ...ExecOption) ZogIssueMap { errs := p.NewErrsMap() defer errs.Free() @@ -138,7 +138,7 @@ func (v *SliceSchema) validate(ctx *p.SchemaCtx) { } // Only supports parsing from data=slice[any] to a dest =&slice[] (this can be typed. Doesn't have to be any) -func (v *SliceSchema) Parse(data any, dest any, options ...ExecOption) p.ZogIssueMap { +func (v *SliceSchema) Parse(data any, dest any, options ...ExecOption) ZogIssueMap { errs := p.NewErrsMap() defer errs.Free() ctx := p.NewExecCtx(errs, conf.IssueFormatter) @@ -233,18 +233,18 @@ func (v *SliceSchema) process(ctx *p.SchemaCtx) { } // Adds pretransform function to schema -func (v *SliceSchema) PreTransform(transform p.PreTransform) *SliceSchema { +func (v *SliceSchema) PreTransform(transform PreTransform) *SliceSchema { if v.preTransforms == nil { - v.preTransforms = []p.PreTransform{} + v.preTransforms = []PreTransform{} } v.preTransforms = append(v.preTransforms, transform) return v } // Adds posttransform function to schema -func (v *SliceSchema) PostTransform(transform p.PostTransform) *SliceSchema { +func (v *SliceSchema) PostTransform(transform PostTransform) *SliceSchema { if v.postTransforms == nil { - v.postTransforms = []p.PostTransform{} + v.postTransforms = []PostTransform{} } v.postTransforms = append(v.postTransforms, transform) return v @@ -284,7 +284,7 @@ func (v *SliceSchema) Default(val any) *SliceSchema { // !TESTS // custom test function call it -> schema.Test(t z.Test, opts ...TestOption) -func (v *SliceSchema) Test(t p.Test, opts ...TestOption) *SliceSchema { +func (v *SliceSchema) Test(t Test, opts ...TestOption) *SliceSchema { for _, opt := range opts { opt(&t) } @@ -336,7 +336,7 @@ func (v *SliceSchema) Len(n int, options ...TestOption) *SliceSchema { // Slice contains a specific value func (v *SliceSchema) Contains(value any, options ...TestOption) *SliceSchema { v.tests = append(v.tests, - p.Test{ + Test{ IssueCode: zconst.IssueCodeContains, Params: make(map[string]any, 1), ValidateFunc: func(val any, ctx Ctx) bool { @@ -363,8 +363,8 @@ func (v *SliceSchema) Contains(value any, options ...TestOption) *SliceSchema { return v } -func sliceMin(n int) p.Test { - t := p.Test{ +func sliceMin(n int) Test { + t := Test{ IssueCode: zconst.IssueCodeMin, Params: make(map[string]any, 1), ValidateFunc: func(val any, ctx Ctx) bool { @@ -378,8 +378,8 @@ func sliceMin(n int) p.Test { t.Params[zconst.IssueCodeMin] = n return t } -func sliceMax(n int) p.Test { - t := p.Test{ +func sliceMax(n int) Test { + t := Test{ IssueCode: zconst.IssueCodeMax, Params: make(map[string]any, 1), ValidateFunc: func(val any, ctx Ctx) bool { @@ -393,8 +393,8 @@ func sliceMax(n int) p.Test { t.Params[zconst.IssueCodeMax] = n return t } -func sliceLength(n int) p.Test { - t := p.Test{ +func sliceLength(n int) Test { + t := Test{ IssueCode: zconst.IssueCodeLen, Params: make(map[string]any, 1), ValidateFunc: func(val any, ctx Ctx) bool { diff --git a/string.go b/string.go index 35ca1fb..fbb561a 100644 --- a/string.go +++ b/string.go @@ -18,11 +18,11 @@ var ( ) type StringSchema[T ~string] struct { - preTransforms []p.PreTransform - tests []p.Test - postTransforms []p.PostTransform + preTransforms []PreTransform + tests []Test + postTransforms []PostTransform defaultVal *T - required *p.Test + required *Test catch *T coercer conf.CoercerFunc } @@ -53,7 +53,7 @@ func String(opts ...SchemaOption) *StringSchema[string] { } // Parses the data into the destination string. Returns a list of ZogIssues -func (v *StringSchema[T]) Parse(data any, dest *T, options ...ExecOption) p.ZogIssueList { +func (v *StringSchema[T]) Parse(data any, dest *T, options ...ExecOption) ZogIssueList { errs := p.NewErrsList() defer errs.Free() @@ -78,7 +78,7 @@ func (v *StringSchema[T]) process(ctx *p.SchemaCtx) { } // Validate Given string -func (v *StringSchema[T]) Validate(data *T, options ...ExecOption) p.ZogIssueList { +func (v *StringSchema[T]) Validate(data *T, options ...ExecOption) ZogIssueList { errs := p.NewErrsList() defer errs.Free() ctx := p.NewExecCtx(errs, conf.IssueFormatter) @@ -101,9 +101,9 @@ func (v *StringSchema[T]) validate(ctx *p.SchemaCtx) { } // Adds pretransform function to schema -func (v *StringSchema[T]) PreTransform(transform p.PreTransform) *StringSchema[T] { +func (v *StringSchema[T]) PreTransform(transform PreTransform) *StringSchema[T] { if v.preTransforms == nil { - v.preTransforms = []p.PreTransform{} + v.preTransforms = []PreTransform{} } v.preTransforms = append(v.preTransforms, transform) return v @@ -123,9 +123,9 @@ func (v *StringSchema[T]) Trim() *StringSchema[T] { } // Adds posttransform function to schema -func (v *StringSchema[T]) PostTransform(transform p.PostTransform) *StringSchema[T] { +func (v *StringSchema[T]) PostTransform(transform PostTransform) *StringSchema[T] { if v.postTransforms == nil { - v.postTransforms = []p.PostTransform{} + v.postTransforms = []PostTransform{} } v.postTransforms = append(v.postTransforms, transform) return v @@ -165,7 +165,7 @@ func (v *StringSchema[T]) Catch(val T) *StringSchema[T] { // ! Tests // custom test function call it -> schema.Test(t z.Test, opts ...TestOption) -func (v *StringSchema[T]) Test(t p.Test, opts ...TestOption) *StringSchema[T] { +func (v *StringSchema[T]) Test(t Test, opts ...TestOption) *StringSchema[T] { for _, opt := range opts { opt(&t) } @@ -223,7 +223,7 @@ func (v *StringSchema[T]) Len(n int, options ...TestOption) *StringSchema[T] { // Test: checks that the value is a valid email address func (v *StringSchema[T]) Email(options ...TestOption) *StringSchema[T] { - t := p.Test{ + t := Test{ IssueCode: zconst.IssueCodeEmail, ValidateFunc: func(v any, ctx Ctx) bool { email, ok := v.(*T) @@ -242,7 +242,7 @@ func (v *StringSchema[T]) Email(options ...TestOption) *StringSchema[T] { // Test: checks that the value is a valid URL func (v *StringSchema[T]) URL(options ...TestOption) *StringSchema[T] { - t := p.Test{ + t := Test{ IssueCode: zconst.IssueCodeURL, ValidateFunc: func(v any, ctx Ctx) bool { s, ok := v.(*T) @@ -262,7 +262,7 @@ func (v *StringSchema[T]) URL(options ...TestOption) *StringSchema[T] { // Test: checks that the value has the prefix func (v *StringSchema[T]) HasPrefix(s T, options ...TestOption) *StringSchema[T] { - t := p.Test{ + t := Test{ IssueCode: zconst.IssueCodeHasPrefix, Params: make(map[string]any, 1), ValidateFunc: func(v any, ctx Ctx) bool { @@ -283,7 +283,7 @@ func (v *StringSchema[T]) HasPrefix(s T, options ...TestOption) *StringSchema[T] // Test: checks that the value has the suffix func (v *StringSchema[T]) HasSuffix(s T, options ...TestOption) *StringSchema[T] { - t := p.Test{ + t := Test{ IssueCode: zconst.IssueCodeHasSuffix, Params: make(map[string]any, 1), ValidateFunc: func(v any, ctx Ctx) bool { @@ -304,7 +304,7 @@ func (v *StringSchema[T]) HasSuffix(s T, options ...TestOption) *StringSchema[T] // Test: checks that the value contains the substring func (v *StringSchema[T]) Contains(sub T, options ...TestOption) *StringSchema[T] { - t := p.Test{ + t := Test{ IssueCode: zconst.IssueCodeContains, Params: make(map[string]any, 1), ValidateFunc: func(v any, ctx Ctx) bool { @@ -325,7 +325,7 @@ func (v *StringSchema[T]) Contains(sub T, options ...TestOption) *StringSchema[T // Test: checks that the value contains an uppercase letter func (v *StringSchema[T]) ContainsUpper(options ...TestOption) *StringSchema[T] { - t := p.Test{ + t := Test{ IssueCode: zconst.IssueCodeContainsUpper, ValidateFunc: func(v any, ctx Ctx) bool { val, ok := v.(*T) @@ -349,7 +349,7 @@ func (v *StringSchema[T]) ContainsUpper(options ...TestOption) *StringSchema[T] // Test: checks that the value contains a digit func (v *StringSchema[T]) ContainsDigit(options ...TestOption) *StringSchema[T] { - t := p.Test{ + t := Test{ IssueCode: zconst.IssueCodeContainsDigit, ValidateFunc: func(v any, ctx Ctx) bool { val, ok := v.(*T) @@ -376,7 +376,7 @@ func (v *StringSchema[T]) ContainsDigit(options ...TestOption) *StringSchema[T] // Test: checks that the value contains a special character func (v *StringSchema[T]) ContainsSpecial(options ...TestOption) *StringSchema[T] { t := - p.Test{ + Test{ IssueCode: zconst.IssueCodeContainsSpecial, ValidateFunc: func(v any, ctx Ctx) bool { val, ok := v.(*T) @@ -403,7 +403,7 @@ func (v *StringSchema[T]) ContainsSpecial(options ...TestOption) *StringSchema[T // Test: checks that the value is a valid uuid func (v *StringSchema[T]) UUID(options ...TestOption) *StringSchema[T] { - t := p.Test{ + t := Test{ IssueCode: zconst.IssueCodeUUID, ValidateFunc: func(v any, ctx Ctx) bool { uuid, ok := v.(*T) @@ -422,7 +422,7 @@ func (v *StringSchema[T]) UUID(options ...TestOption) *StringSchema[T] { // Test: checks that value matches to regex func (v *StringSchema[T]) Match(regex *regexp.Regexp, options ...TestOption) *StringSchema[T] { - t := p.Test{ + t := Test{ IssueCode: zconst.IssueCodeMatch, Params: make(map[string]any, 1), ValidateFunc: func(v any, ctx Ctx) bool { diff --git a/struct.go b/struct.go index 21dc6bf..328aa12 100644 --- a/struct.go +++ b/struct.go @@ -12,12 +12,12 @@ import ( var _ ComplexZogSchema = &StructSchema{} type StructSchema struct { - preTransforms []p.PreTransform + preTransforms []PreTransform schema Schema - postTransforms []p.PostTransform - tests []p.Test + postTransforms []PostTransform + tests []Test // defaultVal any - required *p.Test + required *Test // catch any } @@ -44,7 +44,7 @@ func Struct(schema Schema) *StructSchema { } // Parses val into destPtr and validates each field based on the schema. Only supports val = map[string]any & dest = &struct -func (v *StructSchema) Parse(data any, destPtr any, options ...ExecOption) p.ZogIssueMap { +func (v *StructSchema) Parse(data any, destPtr any, options ...ExecOption) ZogIssueMap { errs := p.NewErrsMap() defer errs.Free() ctx := p.NewExecCtx(errs, conf.IssueFormatter) @@ -152,7 +152,7 @@ func (v *StructSchema) process(ctx *p.SchemaCtx) { // Validate a struct pointer given the struct schema. Usage: // userSchema.Validate(&User, ...options) -func (v *StructSchema) Validate(dataPtr any, options ...ExecOption) p.ZogIssueMap { +func (v *StructSchema) Validate(dataPtr any, options ...ExecOption) ZogIssueMap { errs := p.NewErrsMap() defer errs.Free() ctx := p.NewExecCtx(errs, conf.IssueFormatter) @@ -240,18 +240,18 @@ func (v *StructSchema) validate(ctx *p.SchemaCtx) { } // Add a pretransform step to the schema -func (v *StructSchema) PreTransform(transform p.PreTransform) *StructSchema { +func (v *StructSchema) PreTransform(transform PreTransform) *StructSchema { if v.preTransforms == nil { - v.preTransforms = []p.PreTransform{} + v.preTransforms = []PreTransform{} } v.preTransforms = append(v.preTransforms, transform) return v } // Adds posttransform function to schema -func (v *StructSchema) PostTransform(transform p.PostTransform) *StructSchema { +func (v *StructSchema) PostTransform(transform PostTransform) *StructSchema { if v.postTransforms == nil { - v.postTransforms = []p.PostTransform{} + v.postTransforms = []PostTransform{} } v.postTransforms = append(v.postTransforms, transform) return v @@ -286,7 +286,7 @@ func (v *StructSchema) Optional() *StructSchema { // ! VALIDATORS // custom test function call it -> schema.Test(t z.Test, opts ...TestOption) -func (v *StructSchema) Test(t p.Test, opts ...TestOption) *StructSchema { +func (v *StructSchema) Test(t Test, opts ...TestOption) *StructSchema { for _, opt := range opts { opt(&t) } diff --git a/time.go b/time.go index cf4a556..3ba1e0d 100644 --- a/time.go +++ b/time.go @@ -12,11 +12,11 @@ import ( var _ PrimitiveZogSchema[time.Time] = &TimeSchema{} type TimeSchema struct { - preTransforms []p.PreTransform - tests []p.Test - postTransforms []p.PostTransform + preTransforms []PreTransform + tests []Test + postTransforms []PostTransform defaultVal *time.Time - required *p.Test + required *Test catch *time.Time coercer conf.CoercerFunc } @@ -70,7 +70,7 @@ func (t TimeFunc) Format(format string) SchemaOption { } // Parses the data into the destination time.Time. Returns a list of errors -func (v *TimeSchema) Parse(data any, dest *time.Time, options ...ExecOption) p.ZogIssueList { +func (v *TimeSchema) Parse(data any, dest *time.Time, options ...ExecOption) ZogIssueList { errs := p.NewErrsList() defer errs.Free() ctx := p.NewExecCtx(errs, conf.IssueFormatter) @@ -93,7 +93,7 @@ func (v *TimeSchema) process(ctx *p.SchemaCtx) { } // Validates an existing time.Time -func (v *TimeSchema) Validate(data *time.Time, options ...ExecOption) p.ZogIssueList { +func (v *TimeSchema) Validate(data *time.Time, options ...ExecOption) ZogIssueList { errs := p.NewErrsList() defer errs.Free() ctx := p.NewExecCtx(errs, conf.IssueFormatter) @@ -115,18 +115,18 @@ func (v *TimeSchema) validate(ctx *p.SchemaCtx) { } // Adds pretransform function to schema -func (v *TimeSchema) PreTransform(transform p.PreTransform) *TimeSchema { +func (v *TimeSchema) PreTransform(transform PreTransform) *TimeSchema { if v.preTransforms == nil { - v.preTransforms = []p.PreTransform{} + v.preTransforms = []PreTransform{} } v.preTransforms = append(v.preTransforms, transform) return v } // Adds posttransform function to schema -func (v *TimeSchema) PostTransform(transform p.PostTransform) *TimeSchema { +func (v *TimeSchema) PostTransform(transform PostTransform) *TimeSchema { if v.postTransforms == nil { - v.postTransforms = []p.PostTransform{} + v.postTransforms = []PostTransform{} } v.postTransforms = append(v.postTransforms, transform) return v @@ -165,7 +165,7 @@ func (v *TimeSchema) Catch(val time.Time) *TimeSchema { // GLOBAL METHODS // custom test function call it -> schema.Test("error_code", func(val any, ctx Ctx) bool {return true}) -func (v *TimeSchema) Test(t p.Test, opts ...TestOption) *TimeSchema { +func (v *TimeSchema) Test(t Test, opts ...TestOption) *TimeSchema { for _, opt := range opts { opt(&t) } @@ -185,7 +185,7 @@ func (v *TimeSchema) TestFunc(testFunc p.TestFunc, options ...TestOption) *TimeS // Checks that the value is after the given time func (v *TimeSchema) After(t time.Time, opts ...TestOption) *TimeSchema { - r := p.Test{ + r := Test{ IssueCode: zconst.IssueCodeAfter, Params: make(map[string]any, 1), ValidateFunc: func(v any, ctx Ctx) bool { @@ -207,7 +207,7 @@ func (v *TimeSchema) After(t time.Time, opts ...TestOption) *TimeSchema { // Checks that the value is before the given time func (v *TimeSchema) Before(t time.Time, opts ...TestOption) *TimeSchema { r := - p.Test{ + Test{ IssueCode: zconst.IssueCodeBefore, Params: make(map[string]any, 1), ValidateFunc: func(v any, ctx Ctx) bool { @@ -229,7 +229,7 @@ func (v *TimeSchema) Before(t time.Time, opts ...TestOption) *TimeSchema { // Checks that the value is equal to the given time func (v *TimeSchema) EQ(t time.Time, opts ...TestOption) *TimeSchema { - r := p.Test{ + r := Test{ IssueCode: zconst.IssueCodeEQ, Params: make(map[string]any, 1), ValidateFunc: func(v any, ctx Ctx) bool { diff --git a/utilsOptions.go b/utilsOptions.go index 4d52a4e..86592d8 100644 --- a/utilsOptions.go +++ b/utilsOptions.go @@ -7,7 +7,7 @@ import ( ) // Options that can be passed to a test -type TestOption = func(test *p.Test) +type TestOption = func(test *Test) // Message is a function that allows you to set a custom message for the test. func Message(msg string) TestOption { @@ -19,8 +19,8 @@ func Message(msg string) TestOption { } // MessageFunc is a function that allows you to set a custom message formatter for the test. -func MessageFunc(fn p.IssueFmtFunc) TestOption { - return func(test *p.Test) { +func MessageFunc(fn IssueFmtFunc) TestOption { + return func(test *Test) { test.IssueFmtFunc = fn } } @@ -30,7 +30,7 @@ func MessageFunc(fn p.IssueFmtFunc) TestOption { z.String().TestFunc(..., z.IssueCode("just_provide_a_string" or use values in zconst)) */ func IssueCode(code zconst.ZogIssueCode) TestOption { - return func(test *p.Test) { + return func(test *Test) { test.IssueCode = code } } @@ -47,7 +47,7 @@ z.Schema { ) */ func IssuePath(path string) TestOption { - return func(test *p.Test) { + return func(test *Test) { test.IssuePath = path } } @@ -55,7 +55,7 @@ func IssuePath(path string) TestOption { // Params is a function that allows you to set a custom params for the test. // You may then access these values when formatting test errors in the IssueFmtFunc func Params(params map[string]any) TestOption { - return func(test *p.Test) { + return func(test *Test) { test.Params = params } } @@ -77,13 +77,13 @@ type ParsingOption = ExecOption // Deprecated: use WithIssueFormatter instead // Deprecated for naming consistency -func WithErrFormatter(fmter p.IssueFmtFunc) ExecOption { +func WithErrFormatter(fmter IssueFmtFunc) ExecOption { return WithIssueFormatter(fmter) } // Sets the issue formatter for the execution context. This is used to format the issues messages during execution. // This follows principle of most specific wins. So default formatter < execution formatter < test specific formatter (i.e MessageFunc) -func WithIssueFormatter(fmter p.IssueFmtFunc) ExecOption { +func WithIssueFormatter(fmter IssueFmtFunc) ExecOption { return func(p *p.ExecCtx) { p.SetIssueFormatter(fmter) } diff --git a/zogSchema.go b/zogSchema.go index 64f8969..2974cd4 100644 --- a/zogSchema.go +++ b/zogSchema.go @@ -1,7 +1,6 @@ package zog import ( - "github.com/Oudwins/zog/conf" p "github.com/Oudwins/zog/internals" "github.com/Oudwins/zog/zconst" ) @@ -11,7 +10,7 @@ import ( type ZogSchema interface { process(ctx *p.SchemaCtx) validate(ctx *p.SchemaCtx) - setCoercer(c conf.CoercerFunc) + setCoercer(c CoercerFunc) getType() zconst.ZogType } @@ -37,9 +36,11 @@ type PreTransform = p.PreTransform // Function signature for postTransforms. Takes the value pointer and the context and returns an error. type PostTransform = p.PostTransform +type IssueFmtFunc = p.IssueFmtFunc + // ! PRIMITIVE PROCESSING -func primitiveProcessor[T p.ZogPrimitive](ctx *p.SchemaCtx, preTransforms []p.PreTransform, tests []p.Test, postTransforms []p.PostTransform, defaultVal *T, required *p.Test, catch *T, coercer conf.CoercerFunc, isZeroFunc p.IsZeroValueFunc) { +func primitiveProcessor[T p.ZogPrimitive](ctx *p.SchemaCtx, preTransforms []PreTransform, tests []Test, postTransforms []PostTransform, defaultVal *T, required *Test, catch *T, coercer CoercerFunc, isZeroFunc p.IsZeroValueFunc) { ctx.CanCatch = catch != nil destPtr := ctx.DestPtr.(*T) @@ -122,7 +123,7 @@ func primitiveProcessor[T p.ZogPrimitive](ctx *p.SchemaCtx, preTransforms []p.Pr // 4. postTransforms -> Done above on defer } -func primitiveValidator[T p.ZogPrimitive](ctx *p.SchemaCtx, preTransforms []p.PreTransform, tests []p.Test, postTransforms []p.PostTransform, defaultVal *T, required *p.Test, catch *T) { +func primitiveValidator[T p.ZogPrimitive](ctx *p.SchemaCtx, preTransforms []PreTransform, tests []Test, postTransforms []PostTransform, defaultVal *T, required *Test, catch *T) { ctx.CanCatch = catch != nil valPtr := ctx.Val.(*T)