fabian-hiller / valibot

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

New fields in existing schema #589

Closed chertik77 closed 3 months ago

chertik77 commented 3 months ago

image

Hey how i can add new field avatar to PartialSignupSchema but not like an object...

type PartialSignupSchema = {
    name?: string | undefined;
    email?: string | undefined;
    password?: string | undefined;
} & Record<string, {
    avatar?: File | undefined;
}>
fabian-hiller commented 3 months ago

By object merging:

const Schema = v.object({
  ...OtherSchema.entries,
  avatar: v.optional(v.instance(File)),
});
chertik77 commented 3 months ago
 avatar: v.optional(v.instance(File)),

Thanks you so much 😀

chertik77 commented 3 months ago

Also, I merge title to card schema

export const TitleSchema = v.object({
  title: v.string([
    v.toTrimmed(),
    v.minLength(3, 'Please enter at least 3 characters.')
  ])
})
export const CardSchema = v.object({
  ...TitleSchema.entries,
  description: v.string([
    v.toTrimmed(),
    v.minLength(3, 'Please enter at least 3 characters.')
  ]),
  priority: v.picklist(priorities),
  deadline: v.date()
})

but description has same properties as title, how i can merge title properties into desc field but keep description as a title.

chertik77 commented 3 months ago

And here i"m getting object instead of actual string

type HelpSchema = {
    email: {
        email: string;
    };
    comment: string;
}
export const HelpSchema = v.object({
  email: v.pick(SigninSchema, ['email']),
  comment: v.string([
    v.toTrimmed(),
    v.minLength(5, 'Please enter at least 5 characters.')
  ])
})
fabian-hiller commented 3 months ago

If you have just started using Valibot, I recommend using the release candidate we just released: https://valibot.dev/guides/migrate-from-v0.30.0/

but description has same properties as title, how i can merge title properties into desc field but keep description as a title.

const StringSchema = v.string([
  v.toTrimmed(),
  v.minLength(3, 'Please enter at least 3 characters.')
]);

export const TitleSchema = v.object({
  title: StringSchema,
});

export const CardSchema = v.object({
  ...TitleSchema.entries,
  description: StringSchema,
  priority: v.picklist(priorities),
  deadline: v.date()
});

And here i"m getting object instead of actual string

pick is similar to Pick in TypeScript. It's the wrong procedure for what you are aiming for.

export const HelpSchema = v.object({
  email: SigninSchema.entries.email, // <--
  comment: v.string([
    v.toTrimmed(),
    v.minLength(5, 'Please enter at least 5 characters.')
  ])
})
chertik77 commented 3 months ago

Thanks, u're a lifesaver, i'm new here, but i like it