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

Empty string Required check fails leading to unexpected error reporting #111

Open
jm-positron opened this issue Feb 23, 2025 · 1 comment

Comments

@jm-positron
Copy link

Before you begin

  • [ x] I have read the FAQ and my issue was not answered there

What version of Zog are you using

0.17

Describe the bug
A clear and concise description of what the bug is.

The Required check fails on empty string values, even if they are present.

To Reproduce
A small, self-container, complete reproduction, uploaded to a Github repo, containing the minimum amount of files required to reproduce the behaviour, along with a list of commands that need to be run. Keep it simple.

type Example struct {
	Value string
}

func TestSchema(t *testing.T) {
	schema := z.Struct(z.Schema{
		"value": z.String().Trim().Required(),
	})
	example := &Example{}
	issues := schema.Parse(map[string]any{
		"value": "",
	}, example)
	if len(issues) > 0 {
		t.Errorf("Got issuess")
	}
}

Expected behavior
A clear and concise description of what you expected to happen.

I expect the Required check to pass, since they key is present and it is a string.
I further expect I can manage the length with the Min check, which is more intuitive.

This is important for error reporting, since the required check fails with the error "is required".

It doesn't really matter where I add Min:

  • If I add it after (Required().Min()) the check will fail on the empty string
  • If I add it before (Min().Required()) the min check passes (presumably because the value isn't required at that point, so min does not feel it needs to be enforced?) This is especially counterintuitive.
@Oudwins
Copy link
Owner

Oudwins commented Feb 23, 2025

You might be right to think that this is bad behavior. Zod works how you describe. I'll need to think about it. Because for Validate it should still create an error as we don't know if an empty string was provided or if the user didn't specify anything

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

No branches or pull requests

2 participants