Skip to content

Add a new schema function to validate Valibot schemas #1057

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

Open
andrewmd5 opened this issue Feb 16, 2025 · 3 comments
Open

Add a new schema function to validate Valibot schemas #1057

andrewmd5 opened this issue Feb 16, 2025 · 3 comments
Assignees
Labels
question Further information is requested

Comments

@andrewmd5
Copy link

andrewmd5 commented Feb 16, 2025

Description

I have a use-case where I need to create an object that contains a schema as one of its members. Ideally, I would like to define it like this:

schema: v.pipe(v.schema(), v.transform((s) => toJsonSchema(s)))

However, the only current way to do this is:

schema: v.pipe(
  v.custom<v.BaseSchema<unknown, unknown, v.BaseIssue<unknown>>>(() => true),
  v.transform((s) => toJsonSchema(s))
)

Which does work, but not sure what kind of side effects it will have:

const AutoCompleteSchema = object({
  resolver: pipe(
    string(),
    trim(),
    nonEmpty(),
    regex(/^\/(?:[a-z0-9\-._~%!$&'()*+,;=:@]+(?:\/[a-z0-9\-._~%!$&'()*+,;=:@]+)*)\/?$/)
  ),
  method: picklist(['GET', 'POST']),
  schema: pipe(custom<BaseSchema<unknown, unknown, BaseIssue<unknown>>>(() => true), transform(toJsonSchema)),
});

const data: InferInput<typeof AutoCompleteSchema> = {
  resolver: "/yes/",
  method: "GET",
  schema: object({
    test: string(),
  })
}

const ff = safeParse(AutoCompleteSchema, data);
if (ff.success) {
  console.log(JSON.stringify(ff.output));
}
{
   "resolver":"/yes/",
   "method":"GET",
   "schema":{
      "$schema":"http://json-schema.org/draft-07/schema#",
      "type":"object",
      "properties":{
         "test":{
            "type":"string"
         }
      },
      "required":[
         "test"
      ]
   }
}
@andrewmd5 andrewmd5 changed the title Allow Using v.schema() Directly in Pipe Chains for Nested Schemas Add v.schema() Feb 16, 2025
@fabian-hiller
Copy link
Owner

Thank you for creating this issue. Should schema be a JSON schema or a Valibot schema?

@fabian-hiller fabian-hiller self-assigned this Feb 16, 2025
@fabian-hiller fabian-hiller added the question Further information is requested label Feb 16, 2025
@andrewmd5
Copy link
Author

andrewmd5 commented Feb 17, 2025

Thank you for creating this issue. Should schema be a JSON schema or a Valibot schema?

Both, preferably. Currently I have to do this to silence incompatibility issues:

schema: union([
    pipe(
      custom<BaseSchema<unknown, unknown, BaseIssue<unknown>>>((s) => {
        if (
          s != undefined &&
          typeof s === 'object' &&
          'kind' in s &&
          s.kind === 'schema'
        ) {
          return true;
        }
        return false;
      }),
      transform(toJsonSchema)
    ),
    custom<JSONSchema7>((s) => {
      if (s != undefined && typeof s === 'object' && '$schema' in s) {
        return true;
      }
      return false;
    }),
  ]),

@fabian-hiller
Copy link
Owner

I would recommend to define a Valibot schema that matches the structure of a the Valibot BaseSchema or a JSON Schema.

@fabian-hiller fabian-hiller changed the title Add v.schema() Add a new schema function to validate Valibot schemas Mar 18, 2025
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