Replies: 3 comments 13 replies
-
Hmm, maybe brand is what I'm looking for? |
Beta Was this translation helpful? Give feedback.
-
You can extend the validation of an existing schema using our import * as v from 'valibot';
const EmailSchema = v.pipe(v.string(), v.email());
const GmailSchema = v.pipe(EmailSchema, v.endsWith('@gmail.com')); And here is an example that shows how you can only allow specific schemas as arguments when using TypeScript: import * as v from 'valibot';
const EmailSchema = v.pipe(v.string(), v.email());
type EmailSchema = typeof EmailSchema;
const LoginSchema = v.object({
email: EmailSchema,
password: v.pipe(v.string(), v.minLength(8)),
});
type LoginSchema = typeof LoginSchema;
function foo(schema: EmailSchema | LoginSchema) {
console.log(schema);
}
foo(EmailSchema); // Works
foo(v.string()); // Error |
Beta Was this translation helpful? Give feedback.
-
@fabian-hiller For my use case, I decided to go a different route where I modify the schema based on the value in the metadata action I find. This removes the need for context based runtime validation, and instead I want to modify the schema based on the context, which should mean better performance (since the additional context-based logic isn't executed at runtime). I am following a similar pattern used in getDefaults and getFallbacks to iterate over all the schemas in the schema. When I find a metadata value, I decide to either keep the schema or delete it from the schema object. This appears to be working, when I parse some data using the modified schema, the dropped fields don't get returned. However I'm not sure if this is a supported pattern that could lead to unexpected behaviors, or if there's a different approach I should be taking here. For example, something I would like to do is add additional actions as well which I think I can achieve by creating a |
Beta Was this translation helpful? Give feedback.
-
Hey! 👋🏾 I'm working on a project that uses valibot for validating input and output data based on a schema. I'm able to specify the type as
GenericSchema
for the schema which allows any valibot schema to be used. However for my project, I want to add additional custom checks/actions to the regular valibot schemas. For example, validating that strings don't contain a specific substring, or numbers follow an expected pattern, etc. Even for schemas that don't need any additional checks (yet), I want to create a wrapping schema for them and require the use of the project schemas instead of the regular valibot schemas. That is, using schemas (object()
,string()
,number()
, etc) from valibot should show a typescript error and a runtime error and require the use of equivalent schemas with the additional validations from the project instead.Is there a way to achieve this without having to re-implement the schemas and parsers already implemented in valibot?
Beta Was this translation helpful? Give feedback.
All reactions