fabian-hiller / valibot

The modular and type safe schema library for validating structural data 🤖
https://valibot.dev
MIT License
5.67k stars 170 forks source link

Automatic upgrade to v0.31.0 with Grit only works for certain patterns #621

Closed morinokami closed 1 month ago

morinokami commented 1 month ago

Hi, I tried the automatic upgrade command for v0.31.0 as described here, but it seems to only work well with specific patterns when using Grit. When I run the command npx @getgrit/cli apply github.com/fabian-hiller/valibot#migrate_to_v0_31_0 on the following code, only the first schema is converted, and the rest are not.

import * as v from 'valibot';

const Schema = v.pipe(v.string(), v.email());

const ObjectSchema = v.object({ key: v.string() }, v.null_());
const TupleSchema = v.tuple([v.string()], v.null_());

const LooseObjectSchema = v.object({ key: v.string() }, v.unknown());
const LooseTupleSchema = v.tuple([v.string()], v.unknown());
const StrictObjectSchema = v.object({ key: v.string() }, v.never());
const StrictTupleSchema = v.tuple([v.string()], v.never());

The command output is as follows:

$ npx @getgrit/cli apply github.com/fabian-hiller/valibot#migrate_to_v0_31_0
./index.js
     import * as v from 'valibot';

    -const Schema = v.string([v.email()]);
    +const Schema = v.pipe(v.string(), v.email());

     const ObjectSchema = v.object({ key: v.string() }, v.null_());
     const TupleSchema = v.tuple([v.string()], v.null_());
fabian-hiller commented 1 month ago

Thank you for creating this issue. Can you send me the before, after and expected code? Here is a template:

// Before
...

// After
...

// Expected
...
morinokami commented 1 month ago

@fabian-hiller Sure! The schemas in my code are excerpts from the migration document, so the expected code is the same as described there. Here is the filled-in template:

// Before
import * as v from 'valibot';

const Schema = v.string([v.email()]);

const ObjectSchema = v.object({ key: v.string() }, v.null_());
const TupleSchema = v.tuple([v.string()], v.null_());

const LooseObjectSchema = v.object({ key: v.string() }, v.unknown());
const LooseTupleSchema = v.tuple([v.string()], v.unknown());
const StrictObjectSchema = v.object({ key: v.string() }, v.never());
const StrictTupleSchema = v.tuple([v.string()], v.never());

// After
import * as v from 'valibot';

const Schema = v.pipe(v.string(), v.email());

const ObjectSchema = v.object({ key: v.string() }, v.null_());
const TupleSchema = v.tuple([v.string()], v.null_());

const LooseObjectSchema = v.object({ key: v.string() }, v.unknown());
const LooseTupleSchema = v.tuple([v.string()], v.unknown());
const StrictObjectSchema = v.object({ key: v.string() }, v.never());
const StrictTupleSchema = v.tuple([v.string()], v.never());

// Expected
import * as v from 'valibot';

const Schema = v.pipe(v.string(), v.email());

const ObjectSchema = v.objectWithRest({ key: v.string() }, v.null_());
const TupleSchema = v.tupleWithRest([v.string()], v.null_());

const LooseObjectSchema = v.looseObject({ key: v.string() });
const LooseTupleSchema = v.looseTuple([v.string()]);
const StrictObjectSchema = v.strictObject({ key: v.string() });
const StrictTupleSchema = v.strictTuple([v.string()]);
fabian-hiller commented 1 month ago

Thank you very much! I am aware of these missing parts, but unfortunately I do not have time to fix them at the moment. Are you familiar with GritQL?

morinokami commented 1 month ago

Unfortunately, I am not familiar with GritQL, so I think I am unable to help with the fixes. Sorry for not being able to assist 🥲

fabian-hiller commented 1 month ago

I ping @morgante. Maybe he has time and is motivated to fix it.

morgante commented 1 month ago

Hi @morinokami - sorry for the mistake! Did you try the other codemod?

seren5240 commented 1 month ago

I took a crack at it: https://github.com/fabian-hiller/valibot/pull/625/files