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: new test option to set the issue path for issues generated from that test #87

Merged
merged 2 commits into from
Feb 11, 2025

Conversation

Oudwins
Copy link
Owner

@Oudwins Oudwins commented Feb 11, 2025

Summary by CodeRabbit

  • Documentation

    • Updated guides with clearer examples for schema validation.
    • Added a new section on creating custom tests.
    • Refined navigation with improved document order for easier discovery.
  • New Features

    • Enhanced schema validation with new options for configuring custom error messages and improved error reporting.
    • Adopted more idiomatic naming conventions to better align with Golang practices.

Copy link
Contributor

coderabbitai bot commented Feb 11, 2025

Walkthrough

This update revises both documentation and internal code to align Zog with idiomatic Golang. Several API methods have been renamed (e.g., from z.Array() to z.Slice(), and refine to schema.Test()), while some types (like z.Enum()) have been removed in favor of alternatives. The documentation now includes new sections and updated examples—including guidance on custom tests—and numerous sidebar/position values have been adjusted. In the internals and utilities, error handling has been enhanced by incorporating an IssuePath field along with corresponding helper functions and tests.

Changes

Files Change Summary
docs/.../changes-from-zod.md, docs/.../core-concepts/1-anatomy-of-schema.md, docs/.../zog-schemas.md, docs/.../custom-tests.md Updated documentation content: renamed and replaced methods (e.g., z.Array()z.Slice(), refineschema.Test()), removed deprecated types, added new custom test options (schema.TestFunc), and provided expanded examples and sections for creating custom tests.
docs/.../configuration.md, docs/.../context.mdx, docs/.../core-design-decisions.md, docs/.../errors.md, docs/.../faq.md, docs/.../examples-of-use/_category_.json, docs/.../packages/_category_.json Adjusted sidebar and JSON "position" values to change the ordering of documentation sections. Several files had their sidebar_position or position updated (e.g., from 10 to 202, 7 to 8, and similar shifts) to improve navigation.
internals/contexts.go, internals/tests.go Enhanced internal error handling by updating IssueFromTest to assign EPath when available and adding a new IssuePath field to the Test struct for better issue specificity.
utilsOptions.go, utilsOptions_test.go Introduced a new IssuePath function to set custom issue paths, along with corresponding test functions (TestIssueCode and TestIssuePath) to verify the functionality.

Sequence Diagram(s)

sequenceDiagram
    participant U as User
    participant S as Schema
    participant T as TestFunc
    participant E as Error Handler
    U->>S: Provide input for validation
    S->>T: Execute custom test (TestFunc)
    T-->>S: Return validation result
    alt Validation fails
        S->>E: Invoke IssueFromTest (using IssuePath)
        E-->>S: Return error with detailed IssuePath
    else Validation succeeds
        S->>U: Return validated data
    end
Loading

Possibly related PRs

Poem

I’m a rabbit in code, hopping through the night,
With tests refined and methods shining bright.
From slices to prefixes, our paths now align,
Each error and issue elegantly defined.
I nibble on changes with joy in my heart—
A playful commit, a true work of art!
🐇💻 Hop on, dear coder, let’s celebrate smart!

✨ Finishing Touches
  • 📝 Generate Docstrings (Beta)

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

Deploying zog with  Cloudflare Pages  Cloudflare Pages

Latest commit: edd1d0b
Status: ✅  Deploy successful!
Preview URL: https://88924554.zog-3a0.pages.dev
Branch Preview URL: https://feat-test-issue-path.zog-3a0.pages.dev

View logs

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (6)
utilsOptions_test.go (1)

56-79: Consider adding edge cases to the test.

While the test covers the basic functionality well, consider adding these edge cases:

  • Empty string for issue path
  • Special characters in the issue path
  • Nested struct scenarios
 func TestIssuePath(t *testing.T) {
+	t.Run("empty_path", func(t *testing.T) {
+		var out User
+		schema := Struct(Schema{
+			"name": String().Min(5, IssuePath(""), Message("foo msg")),
+		})
+		err := schema.Parse(map[string]any{"name": "1234"}, &out)
+		assert.Equal(t, "name", err["name"][0].Path())
+	})
+
+	t.Run("special_chars", func(t *testing.T) {
+		var out User
+		schema := Struct(Schema{
+			"name": String().Min(5, IssuePath("foo/bar@123"), Message("foo msg")),
+		})
+		err := schema.Parse(map[string]any{"name": "1234"}, &out)
+		assert.Equal(t, "foo/bar@123", err["foo/bar@123"][0].Path())
+	})
+
+	t.Run("nested_struct", func(t *testing.T) {
+		type Address struct {
+			Street string
+		}
+		type User struct {
+			Name    string
+			Address Address
+		}
+		var out User
+		schema := Struct(Schema{
+			"address": Struct(Schema{
+				"street": String().Min(5, IssuePath("user.address.street")),
+			}),
+		})
+		err := schema.Parse(map[string]any{
+			"address": map[string]any{"street": "123"},
+		}, &out)
+		assert.Equal(t, "user.address.street", err["user.address.street"][0].Path())
+	})
docs/docs/custom-tests.md (2)

13-14: Improve readability of the introduction.

Consider rephrasing for better clarity and grammar:

-All schemas contain the `TestFunc()` method which can be used to create a simple custom test in a similar way to Zod's `refine` method. The `TestFunc()` method takes a `ValidateFunc` as an argument. This is a function that takes the data as input and returns a boolean indicating if it is valid or not. If you return `false` from the function Zog will create a [ZogIssue](/errors). For example:
+All schemas contain the `TestFunc()` method, which can be used to create a simple custom test similarly to Zod's `refine` method. The `TestFunc()` method takes a `ValidateFunc` as an argument. This is a function that takes the data as input and returns a boolean indicating whether it is valid. If you return `false` from the function, Zog will create a [ZogIssue](/errors). For example:
🧰 Tools
🪛 LanguageTool

[style] ~14-~14: Consider replacing this phrase with the adverb “similarly” to avoid wordiness.
Context: ... be used to create a simple custom test in a similar way to Zod's refine method. The `TestFunc...

(IN_A_X_MANNER)


[style] ~14-~14: In contexts where ‘if’ is followed by ‘or’, using ‘whether’ may be more appropriate (and formal).
Context: ... input and returns a boolean indicating if it is valid or not. If you return `fals...

(IF_WHETHER)


[uncategorized] ~14-~14: A comma might be missing here.
Context: ... or not. If you return false from the function Zog will create a ZogIssue. ...

(AI_EN_LECTOR_MISSING_PUNCTUATION_COMMA)


22-23: Improve readability of the pro tip.

Add missing comma and improve readability:

-> **Pro tip**
-> It is very likely that you may want to set custom messages or paths, you can do that like with any other tests with `TestOptions`. For more on this checkout the [Anatomy of a Schema](/core-concepts/anatomy-of-schema#test-options) page.
+> **Pro tip**
+> It is very likely that you may want to set custom messages or paths. You can do that like any other tests with `TestOptions`. For more on this, check out the [Anatomy of a Schema](/core-concepts/anatomy-of-schema#test-options) page.
🧰 Tools
🪛 LanguageTool

[uncategorized] ~23-~23: Possible missing comma found.
Context: ...r tests with TestOptions. For more on this checkout the [Anatomy of a Schema](/cor...

(AI_HYDRA_LEO_MISSING_COMMA)

docs/docs/core-concepts/1-anatomy-of-schema.md (1)

59-59: Improve readability.

Add missing comma:

-> A test is what zod calls a "validator". It is a struct that represents an individual validation. For example for the String schema `z.String()` the method `Min(3)` generates a test that checks if the string is at least 3 characters long. You can view all the default tests that come with each [schema type here.](/zog-schemas)
+> A test is what zod calls a "validator". It is a struct that represents an individual validation. For example, for the String schema `z.String()`, the method `Min(3)` generates a test that checks if the string is at least 3 characters long. You can view all the default tests that come with each [schema type here.](/zog-schemas)
🧰 Tools
🪛 LanguageTool

[typographical] ~59-~59: After the expression ‘for example’ a comma is usually used.
Context: ...epresents an individual validation. For example for the String schema z.String() the ...

(COMMA_FOR_EXAMPLE)

docs/docs/zog-schemas.md (2)

33-34: Improve readability.

Add missing comma:

-These are options that can be passed to any test. For more on this checkout the [Anatomy of a Schema](/core-concepts/anatomy-of-schema#test-options) page.
+These are options that can be passed to any test. For more on this, check out the [Anatomy of a Schema](/core-concepts/anatomy-of-schema#test-options) page.
🧰 Tools
🪛 LanguageTool

[uncategorized] ~33-~33: Possible missing comma found.
Context: ... can be passed to any test. For more on this checkout the [Anatomy of a Schema](/cor...

(AI_HYDRA_LEO_MISSING_COMMA)


53-54: Improve readability.

Rephrase for better clarity:

-These are options that can be passed to schema.Parse() & schema.Validate(). They configure the execution behaviour of the validation.
+These are options that can be passed to the `Parse()` and `Validate()` methods. They configure the execution behavior of the validation.
🧰 Tools
🪛 LanguageTool

[grammar] ~53-~53: The word ‘schema’ is not correct in this context. Use one of the suggestions or replace it with an appropriate verb.
Context: ...s These are options that can be passed to schema.Parse() & schema.Validate(). They confi...

(VB_TO_NN_DT)

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 49f01e3 and edd1d0b.

📒 Files selected for processing (15)
  • docs/docs/changes-from-zod.md (1 hunks)
  • docs/docs/configuration.md (1 hunks)
  • docs/docs/context.mdx (1 hunks)
  • docs/docs/core-concepts/1-anatomy-of-schema.md (1 hunks)
  • docs/docs/core-design-decisions.md (1 hunks)
  • docs/docs/custom-tests.md (1 hunks)
  • docs/docs/errors.md (1 hunks)
  • docs/docs/examples-of-use/_category_.json (1 hunks)
  • docs/docs/faq.md (1 hunks)
  • docs/docs/packages/_category_.json (1 hunks)
  • docs/docs/zog-schemas.md (3 hunks)
  • internals/contexts.go (1 hunks)
  • internals/tests.go (1 hunks)
  • utilsOptions.go (1 hunks)
  • utilsOptions_test.go (2 hunks)
✅ Files skipped from review due to trivial changes (7)
  • docs/docs/packages/category.json
  • docs/docs/examples-of-use/category.json
  • docs/docs/configuration.md
  • docs/docs/context.mdx
  • docs/docs/faq.md
  • docs/docs/core-design-decisions.md
  • docs/docs/errors.md
🧰 Additional context used
🪛 LanguageTool
docs/docs/custom-tests.md

[style] ~14-~14: Consider replacing this phrase with the adverb “similarly” to avoid wordiness.
Context: ... be used to create a simple custom test in a similar way to Zod's refine method. The `TestFunc...

(IN_A_X_MANNER)


[style] ~14-~14: In contexts where ‘if’ is followed by ‘or’, using ‘whether’ may be more appropriate (and formal).
Context: ... input and returns a boolean indicating if it is valid or not. If you return `fals...

(IF_WHETHER)


[uncategorized] ~14-~14: A comma might be missing here.
Context: ... or not. If you return false from the function Zog will create a ZogIssue. ...

(AI_EN_LECTOR_MISSING_PUNCTUATION_COMMA)


[uncategorized] ~23-~23: Possible missing comma found.
Context: ...r tests with TestOptions. For more on this checkout the [Anatomy of a Schema](/cor...

(AI_HYDRA_LEO_MISSING_COMMA)

docs/docs/core-concepts/1-anatomy-of-schema.md

[typographical] ~59-~59: After the expression ‘for example’ a comma is usually used.
Context: ...epresents an individual validation. For example for the String schema z.String() the ...

(COMMA_FOR_EXAMPLE)

docs/docs/zog-schemas.md

[uncategorized] ~33-~33: Possible missing comma found.
Context: ... can be passed to any test. For more on this checkout the [Anatomy of a Schema](/cor...

(AI_HYDRA_LEO_MISSING_COMMA)


[grammar] ~53-~53: The word ‘schema’ is not correct in this context. Use one of the suggestions or replace it with an appropriate verb.
Context: ...s These are options that can be passed to schema.Parse() & schema.Validate(). They confi...

(VB_TO_NN_DT)

⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: Cloudflare Pages
🔇 Additional comments (6)
utilsOptions_test.go (1)

40-54: LGTM!

The test function thoroughly verifies the IssueCode feature by testing both Parse and Validate scenarios, ensuring that the issue code is correctly set and default messages are preserved.

utilsOptions.go (1)

38-53: LGTM!

The IssuePath function is well-documented with clear usage examples and a warning about potential type safety issues.

internals/contexts.go (1)

124-126: LGTM!

The modification to IssueFromTest correctly sets the custom issue path when provided.

internals/tests.go (1)

16-16: LGTM!

The IssuePath field is appropriately added to the Test struct.

docs/docs/changes-from-zod.md (1)

1-19: LGTM!

The documentation clearly explains the differences between Zog and Zod, including the rationale for method name changes and design decisions.

🧰 Tools
🪛 LanguageTool

[uncategorized] ~10-~10: Use a comma before ‘but’ if it connects two independent clauses (unless they are closely connected and short).
Context: ... adhere to the Zod API whenever possible but there are significant differences becau...

(COMMA_COMPOUND_SENTENCE_2)


[uncategorized] ~13-~13: Possible missing comma found.
Context: ...thod name would be confusing for Golang devs I changed it - Some other changes: - ...

(AI_HYDRA_LEO_MISSING_COMMA)


[uncategorized] ~14-~14: Possible missing preposition found.
Context: ...onfusing for Golang devs I changed it - Some other changes: - The refine method fo...

(AI_HYDRA_LEO_MISSING_TO)

docs/docs/core-concepts/1-anatomy-of-schema.md (1)

65-69: LGTM!

The code examples effectively demonstrate the usage of test options, including the new IssuePath functionality.

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