Skip to content
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

Force typeing on object schema #571

Closed
kenhuang opened this issue May 9, 2024 · 2 comments
Closed

Force typeing on object schema #571

kenhuang opened this issue May 9, 2024 · 2 comments
Assignees
Labels
question Further information is requested

Comments

@kenhuang
Copy link

kenhuang commented May 9, 2024

In zod we can do this:

type Test {
  field1?: string,
  field2?: string,
}

const TestSchema: z.ZodType<Test> = z.object({
  field1: z.string().optional(),
  field2: z.string().optional(),
});

Is anything similar in valibot? Thanks for the awsome library!

@fabian-hiller
Copy link
Owner

Yes, please try out BaseSchema:

import * as v from 'valibot';

type Test {
  field1?: string,
  field2?: string,
}

const TestSchema: v.BaseSchema<Test> = v.object({
  field1: v.optional(v.string()),
  field2: v.optional(v.string()),
});

@fabian-hiller
Copy link
Owner

fabian-hiller commented May 9, 2024

Hint: Sometimes satisfies is better to not lose type safety for specific object related information:

import * as v from 'valibot';

type Test {
  field1?: string,
  field2?: string,
}

const TestSchema = v.object({
  field1: v.optional(v.string()),
  field2: v.optional(v.string()),
}) satisfies v.BaseSchema<Test>;

@fabian-hiller fabian-hiller self-assigned this May 9, 2024
@fabian-hiller fabian-hiller added the question Further information is requested label May 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants