-
-
Notifications
You must be signed in to change notification settings - Fork 204
Valibot: correctly support format: int64
#2344
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
Conversation
|
🦋 Changeset detectedLatest commit: fb7b724 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
TODO
|
@mrlubos I got it to work and have a first draft, but I have a few questions:
|
Hey!
|
Hey @mrlubos, thanks again for your feedback! A few follow-ups:
const expression = ts.factory.createArrayLiteralExpression(
elements
.map((value) => (isTsNode(value) ? value : toExpression({ value })))
.filter(isType<ts.Expression>),
// multiline if array contains objects
multiLine ||
(!Array.isArray(elements[0]) && typeof elements[0] === 'object'),
); The key issue is in the condition: multiLine || (!Array.isArray(elements[0]) && typeof elements[0] === 'object') This forces const unionExpression = compiler.callExpression({
functionName: compiler.propertyAccessExpression({
expression: identifiers.v,
name: identifiers.schemas.union,
}),
parameters: [
compiler.arrayLiteralExpression({
elements: [
compiler.callExpression({
functionName: compiler.propertyAccessExpression({
expression: identifiers.v,
name: identifiers.schemas.number,
}),
}),
compiler.callExpression({
functionName: compiler.propertyAccessExpression({
expression: identifiers.v,
name: identifiers.schemas.string,
}),
}),
compiler.callExpression({
functionName: compiler.propertyAccessExpression({
expression: identifiers.v,
name: identifiers.schemas.bigint,
}),
}),
],
multiLine: false,
}),
],
}); This is why
export const vFoo = v.object({
foo: v.optional(
v.pipe(
v.union([
v.number(),
v.string(),
v.bigint()
]),
v.transform(x => BigInt(x)),
v.minValue(-9223372036854775808n, 'Invalid value: Expected int64 to be >= -2^63'),
v.maxValue(9223372036854775807n, 'Invalid value: Expected int64 to be <= 2^63-1'),
v.minValue(BigInt(0)),
v.maxValue(BigInt(100))
)
)
}); You can see my logic for format validation, then the additional schema min/max limitation. I want to ask if one style is preferred over the other, if a mix is acceptable, or if we should standardize on one.
|
|
commit: |
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #2344 +/- ##
==========================================
+ Coverage 22.47% 22.52% +0.05%
==========================================
Files 323 324 +1
Lines 31662 31857 +195
Branches 1232 1232
==========================================
+ Hits 7115 7177 +62
- Misses 24538 24671 +133
Partials 9 9
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
@Daschi1 I moved tests around to accommodate both Zod 4 and Zod 3. Whatever was in |
I just finished implementing the tests, thank you for the heads up! I'll resolve the merge conflicts later today and let you know once it's ready for review. |
Hi @mrlubos, I tried to integrate the recent changes over the last few hours, but honestly I struggled a lot, mainly due to my limited Git experience with large merges. I made some updates and now there are no merge conflicts with The PR now shows over 3,000 changed files because of the directory refactor, and I don't know if that is expected. Also, some tests that pull schemas are failing with 404 errors. I suspect it's because those files were moved, but I'm not sure if that is on my end or if the tests need adjustment. I'm out of ideas and would really appreciate any guidance or assistance you can offer. I apologize if I overcomplicated this merge. My last option might be to close this PR and open a new one based on the latest code changes where I manually reimplement all my changes, so I can be sure everything is in the right place. Thank you for your patience and any help you can provide! |
Sorry for that! If resolving conflicts is a pain, I'd roll back your branch to commit From there, I'd start with a new branch based off The only files you won't be able to move that way will probably be the changes to tests and specs. However, you should be able to re-apply those changes manually, I think it was just a few files. Do NOT move snapshots over, they'll only be getting in the way. Instead, re-run |
You also added a commit |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you generate this file with an LLM?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, for the final implementation I used Sonnet 4. I first wrote a few tests myself to establish the style and specified the conditions I wanted it to cover, then had it generate the rest based on that. I reviewed the result afterwards and it looked good to me. Did you notice anything that seems off?
@@ -253,6 +253,74 @@ const nullTypeToValibotSchema = (_props: { | |||
return expression; | |||
}; | |||
|
|||
// Integer format ranges and properties |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you think about moving these helpers and constants into their own file?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, that seems reasonable. plugin.ts
is already quite large. Where would you prefer I put them? In the same directory or somewhere more central?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same directory is fine, thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
d573e06
I extracted them into their own file number-helpers.ts
. Does this look good to you?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you think I should update the formats.test.ts
do use the helper as well?
@Daschi1 CI tests are failing but your pull request might be okay. Stuff has moved around yesterday and I didn't fix the CI yet |
Yeah I noticed, some tests are failing because they attempt to download specs that no longer exist, which causes a 404. For example:
|
Thank you so much for that! That helped tremendously and the |
.../openapi-ts-tests/main/test/plugins/valibot/spec/numberTypeToValibotSchema/const-values.json
Outdated
Show resolved
Hide resolved
packages/openapi-ts-tests/main/test/plugins/valibot/spec/numberTypeToValibotSchema/formats.json
Outdated
Show resolved
Hide resolved
...i-ts-tests/main/test/plugins/valibot/spec/numberTypeToValibotSchema/min-max-constraints.json
Outdated
Show resolved
Hide resolved
it('should accept exact const value', () => { | ||
const result = v.safeParse(generatedSchemas.vNumberNoFormat, 42.5); | ||
expect(result.success).toBe(true); | ||
if (result.success) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is a redundant condition in these test files. If the previous line fails, this should never execute. And even if it does, it doesn't matter since the previous one is already failing
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes you are right. I am validating something that would essentially only test if Valibot itself works correctly rather than our generated plugin output. I will remove the redundant checks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
a7df1f2
I removed the redundant checks from const-values.test.ts
and also min-max-constraints.test.ts
. In formats.test.ts
I am specifically checking for the error message defined in plugin.ts
and also verifying the result type to ensure the BigInt
conversions are happening as expected. I think this is fine to keep, but let me know if you would prefer me to change that as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The assertions themselves are fine, but the if
conditions are redundant for the same reason I mentioned above. It makes the file unnecessarily large and harder to read
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, I see now. I misunderstood earlier and thought you were referring to the assertions rather than the if
clauses themselves. I have removed the if
conditions and kept the assertions: 4147808
format: int64
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good! There are a few remaining if conditions, can be merged after those are cleaned up
...es/openapi-ts-tests/main/test/plugins/valibot/test/numberTypeToValibotSchema/formats.test.ts
Outdated
Show resolved
Hide resolved
Oops, I had to undo some changes locally earlier and it looks like I was a bit too generous with my ctrl‑z :D |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks good @Daschi1, thank you! Let's see what the people say once they use it 🛳️
Resolves #2194