fabian-hiller / valibot

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

Codemod using `unknown()` schema #633

Open hyunbinseo opened 4 weeks ago

hyunbinseo commented 4 weeks ago

Codemod: valibot/migrate-to-v0.31.0@0.2.1

// Before
const Schema = optional(coerce(number([integer(), minValue(0)]), Number));

// After -  integer(), minValue() are lost
const Schema = optional(pipe(unknown(), transform(Number)));

// Expected
const Schema = optional(pipe(unknown(), transform(Number), number(), integer(), minValue(0)));
// Before
const Schema = coerce(picklist(readonlyConstArray), Number);

// After - picklist() is lost
const Schema = pipe(unknown(), transform(Number));

// Expected
const Schema = pipe(picklist(readonlyConstArray), transform(Number));
throrin19 commented 4 weeks ago

same problem with nullable, nullish and optional. How it should convert it to 0.31 version ?

// Before
const orgId = coerce(nullish(number([integer()])), toNumber),

// After
const orgId = pipe(unknown(), transform(toNumber)),

// Expected - for nullish, optional, nullable, I can't find example with pipe : 
nullish(pipe(unknown(), transform(toNumber), number(), integer()));
// OR 
pipe(nullish(), unknown(), transform(toNumber), number(), integer());
hyunbinseo commented 4 weeks ago

@throrin19 I believe this is the correct usage,

import { integer, nullish, number, pipe, transform, unknown } from 'valibot';

const Schema = nullish(pipe(unknown(), transform(Number), number(), integer()));

since pipe() results in a schema. It can be placed in the v.string() place.

// Example from https://valibot.dev/api/nullable/
const NullableStringSchema = v.nullable(v.string(), "I'm the default!");
fabian-hiller commented 3 weeks ago

Thank you for your feedback! I will look into it!