Skip to content
Discussion options

You must be logged in to vote

You could use our new getTitle method to check if a schema has a title.

Here is how you can walk your schemas to check for title:

import * as v from 'valibot';

type Schema =
  | v.GenericSchema
  | v.SchemaWithPipe<
      [
        v.GenericSchema,
        ...(v.GenericPipeItem | v.TitleAction<unknown, string>)[],
      ]
    >;

function hasTitle(schema: Schema): boolean {
  if ('pipe' in schema) {
    for (const item of schema.pipe) {
      if (v.isOfKind('schema', item) && 'pipe' in item) {
        if (hasTitle(item)) {
          return true;
        }
      } else if (v.isOfKind('metadata', item) && v.isOfType('title', item)) {
        return true;
      }
    }
  }
  return false;
}

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@josh-hemphill
Comment options

Answer selected by josh-hemphill
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants