-
-
Notifications
You must be signed in to change notification settings - Fork 201
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
Add Grit migration script #591
Conversation
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.
Thank you very much! This looks really nice if it works as expected!
.grit/patterns/valibot_v03.md
Outdated
|
||
pattern vb_pipe() { | ||
or { | ||
`v.string([$args])` => `v.pipe(v.string(), $args)`, |
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 should work for any schema function, not just string
. You can find a list of all schema functions in our API reference. Also, in the previous version of Valibot, the pipe
argument is always the last optional argument of a schema function. Here is another example:
// Before
const Schema = v.map(
v.number(),
v.string([v.url(), v.endsWith('@example.com')]), // <-- nested pipe
[v.maxSize(10)] // <-- outer pipe
);
// After
const Schema = v.pipe(
v.map(
v.number(),
v.pipe(v.string(), v.url(), v.endsWith('@example.com')), // <-- nested pipe
),
v.maxSize(10), // <-- outer pipe
);
But of course there are special cases like union
where you can pass an array of options
. This is not a pipe
. The codemod should just detect an array of actions as the last optional argument of a schema as a "pipe" and refactor only this part. Here is another example:
// Before
const Schema = v.object({
email: v.string([v.email(), v.endsWith('@gmail.com')]),
password: v.string([v.minLength(8)]),
other: v.union([v.string([v.decimal()]), v.number()]), // <--
});
// After
const Schema = v.object({
email: v.pipe(v.string(), v.email(), v.endsWith('@gmail.com')),
password: v.pipe(v.string(), v.minLength(8)),
other: v.union([v.pipe(v.string(), v.decimal()), v.number()]), // <--
});
It is also important that it works with any import wildcard alias as well as with direct imports (same applies to the other codemod steps).
// With "v" alias
import * as v from 'valibot';
// With "foo" alias
import * as foo from 'valibot';
// With direct imports
import { string, minLength, … } from 'valibot';
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.
Sorry I don't really understand the distinction in the first argument complete - can you take a look at the latest and confirm I interpreted it correctly?
Other cases should work correctly, including non-foo aliases.
To set expectations, no codemod will cover 100% of cases but this should cover 90% of what's required from my reading of the docs - feel free to tweak, I hope the syntax is clear enough.
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.
Thank you! I will try to have a look later today or in the next few days.
You can always copy the output to our playground to see if the transformed code is valid. |
If I run |
Once I can confirm that the codemod works, I will add instructions on how to run it to our migration guide. Do people need to manually add the |
Hmm, what steps did you take to install it / can you provide reproduction instructions? This isn't expected.
Yes, currently they will need to download it. I'm planning to add a feature for applying remote patterns directly too.
We also have Windows releases available. |
I followed the Quickstart guide and executed: I got it to work with
👍 |
Agreed, sudo definitely shouldn't be required. Maybe you can try running |
I get the same error when I type
// Before
const Schema = v.transform(
v.brand(v.string(), "Name"),
(input) => input.length
);
// After
const Schema = v.pipe(
v.string(),
v.brand('Name'),
v.transform((input) => input.length)
); |
This now handles |
You can try this out by running
Grit also includes built-in testing for the markdown file. Just run
grit patterns test
in this repo.