Skip to content

Create getMetadata/getTitle/getDescription methods #1154

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
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

EskiMojo14
Copy link

@EskiMojo14 EskiMojo14 commented Apr 12, 2025

Loops through schema (including nested pipes) to shallowly merge metadata objects.

const schema = v.pipe(
  v.string(),
  v.title('title'),
  v.email(),
  v.description('description'),
  v.metadata({ key: 'value' }),
  v.pipe(v.string(), v.metadata({ extra: 'extra' }))
);
expect(v.getTitle(schema)).toBe('title')
expect(v.getDescription(schema)).toBe('description')
expect(v.getMetadata(schema)).toStrictEqual({
  key: 'value',
  extra: 'extra',
});

Copy link

vercel bot commented Apr 12, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
valibot ✅ Ready (Inspect) Visit Preview 💬 Add feedback Apr 17, 2025 11:00pm

import { describe, expect, test } from 'vitest';
import * as v from '../../index.ts';

describe('getMetadata', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps we should have a test, which tests with nested object?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure what you mean by nested object, sorry

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

v.pipe(
  v.object({
    name: v.pipe(v.string(), v.title("The Name")0
  }),
  v.title("An object")
);

Or does this just get metadata of a single field?

Copy link
Author

@EskiMojo14 EskiMojo14 Apr 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this just gets the metadata of the single schema (and any pipeline items), not any nested schemas like object entries - you'd need to do v.getMetadata(schema.pipe[0].entries.name)

@EskiMojo14 EskiMojo14 marked this pull request as ready for review April 17, 2025 22:56
@fabian-hiller fabian-hiller added this to the v1.1 milestone Apr 20, 2025
@fabian-hiller fabian-hiller self-assigned this Apr 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants