Skip to content

feat: default and catch funcs - #215

Merged
Oudwins merged 1 commit into
masterfrom
feat/catch-and-default-funcs
Feb 7, 2026
Merged

feat: default and catch funcs#215
Oudwins merged 1 commit into
masterfrom
feat/catch-and-default-funcs

Conversation

@Oudwins

@Oudwins Oudwins commented Feb 7, 2026

Copy link
Copy Markdown
Owner

Summary by CodeRabbit

Release Notes

  • New Features

    • Added DefaultFunc() and CatchFunc() methods across all schema types, enabling defaults and catch values to be supplied via functions for dynamic value generation.
  • Documentation

    • Updated API reference to document new DefaultFunc() and CatchFunc() methods.

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your free trial has ended. If you'd like to continue receiving code reviews, you can add a payment method here.

@cloudflare-workers-and-pages

Copy link
Copy Markdown

Deploying zog with  Cloudflare Pages  Cloudflare Pages

Latest commit: 9af7101
Status: ✅  Deploy successful!
Preview URL: https://f0665d38.zog-3a0.pages.dev
Branch Preview URL: https://feat-catch-and-default-funcs.zog-3a0.pages.dev

View logs

@coderabbitai

coderabbitai Bot commented Feb 7, 2026

Copy link
Copy Markdown
Contributor

Walkthrough

This pull request refactors default and catch value handling across all schema types (Any, Boolean, Number, String, Time, Map, Slice) from concrete pointers to lazy function-based providers. Core parsing/validation functions are updated to accept and invoke these function callbacks instead of dereferencing values directly. Tests are added for both parsing and validation flows.

Changes

Cohort / File(s) Summary
Schema Type Refactoring: Any
any.go, any_test.go, any_validate_test.go
Replaced defaultVal *any and catch *any fields with defaultFunc func() any and catchFunc func() any. Updated Default() and Catch() methods to delegate to new DefaultFunc() and CatchFunc() methods. Added comprehensive test coverage for parsing and validation flows with function-based defaults/catches.
Schema Type Refactoring: Boolean
boolean.go, boolean_test.go, boolean_validate_test.go
Converted boolean schema defaults/catches from concrete pointers to function-based providers. Default() and Catch() now delegate to DefaultFunc() and CatchFunc(). Tests verify correct invocation counts and value propagation for nil and invalid inputs.
Schema Type Refactoring: Number
numbers.go, numbers_test.go, numbers_validate_test.go
Applied function-based default/catch pattern to NumberSchema. Methods Default() and Catch() wrap literal values and delegate to DefaultFunc() and CatchFunc(). Tests validate invocation order and counts across default and catch scenarios.
Schema Type Refactoring: String
string.go, string_test.go, string_validate_test.go
Replaced StringSchema pointer fields with defaultFunc func() T and catchFunc func() T. Preserved API surface with delegation pattern. Tests ensure proper sequencing of DefaultFunc/CatchFunc with constraint validation (Min, Required).
Schema Type Refactoring: Time
time.go, time_test.go, time_validate_test.go
TimeSchema converted to function-based defaults/catches. Updated primitiveParsing/primitiveValidation calls. Tests verify DefaultFunc invoked on nil and CatchFunc invoked on constraint violations.
Schema Type Refactoring: Map & Slice
maps.go, maps_test.go, maps_validate_test.go, slices.go, slices_test.go, slices_validate_test.go
MapSchema replaced defaultVal map[K]V with defaultFunc func() map[K]V; SliceSchema replaced defaultVal any with defaultFunc func() any. Both add DefaultFunc() methods with delegation from Default(). Tests validate lazy initialization and invocation counts.
Core Infrastructure Changes
zogSchema.go, toZSS.go
Updated primitiveParsing() and primitiveValidation() function signatures to accept defaultFunc func() T and catchFunc func() T instead of defaultVal *T and catch *T. All internal logic updated to invoke functions instead of dereferencing. Helper functions added to toZSS.go for deriving default values from functions.
Documentation
docs/docs/reference.md
Added documentation for new public methods DefaultFunc(fn) and CatchFunc(fn) alongside existing Default(value) and Catch(value) methods.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~60 minutes

Possibly related PRs

Poem

🐰 With functions now we lazily provide,
No pointers stored, just closures deep inside.
Defaults and catches bloom on demand,
Each schema transformed by a refactoring hand! ✨

🚥 Pre-merge checks | ✅ 2 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 10.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main change: introducing default and catch functions across multiple schema types, which is the primary refactoring throughout the codebase.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch feat/catch-and-default-funcs

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Fix all issues with AI agents
In `@toZSS.go`:
- Around line 397-402: The function defaultValueFromAnyFunc should guard against
the case where defaultFunc returns nil before calling deepCopyPrimitivePtr;
modify defaultValueFromAnyFunc (the function named defaultValueFromAnyFunc) to
check if val == nil (or reflect.ValueOf(val).IsValid()/IsNil for
interface-contained nil) and return nil immediately when the returned interface
is nil, otherwise call deepCopyPrimitivePtr(&val) as before to avoid
deepCopyPrimitivePtr invoking rv.Elem() on an invalid reflect.Value and causing
a panic.
🧹 Nitpick comments (1)
slices.go (1)

214-224: Default delegates to DefaultFunc — note shared reference for mutable types.

Default(val any) closes over val. For reference types like slices, every invocation of defaultFunc returns the same underlying reference. If a Validate/Transform later mutates the slice in-place, the "default" is permanently changed. DefaultFunc gives users an escape hatch (return a fresh slice each call), but Default silently shares.

This is a pre-existing concern (same semantics as the old pointer-based approach) and not a regression, but worth a doc note or comment for future maintainers.

Comment thread toZSS.go
@Oudwins
Oudwins merged commit f27b872 into master Feb 7, 2026
10 checks passed
@Oudwins
Oudwins deleted the feat/catch-and-default-funcs branch February 7, 2026 15:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant