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

nonEmpty validator #171

Open
IlyaSemenov opened this issue Sep 19, 2023 · 7 comments
Open

nonEmpty validator #171

IlyaSemenov opened this issue Sep 19, 2023 · 7 comments
Assignees
Labels
enhancement New feature or request priority This has priority

Comments

@IlyaSemenov
Copy link
Contributor

As a developer migrating from zod, I am missing the nonEmpty() guard, which would work like:

const usernameSchema = v.string([v.toTrimmed(), v.nonEmpty()])

I realise I can achieve that with minLength(1), and that I can write own plugin to reduce boilerplate. I actually did:

import { type ErrorMessage, minLength } from "valibot"

/**
 * Creates a validation functions that ensures a string or an array is not empty.
 *
 * @param error The error message.
 *
 * @returns A validation function.
 */
export function nonEmpty<TInput extends string | any[]>(error?: ErrorMessage) {
  return minLength<TInput>(1, error)
}

My question is, is that something you believe could be added to the valibot distribution? I can create a PR, just wanted to ask beforehand if that makes sense.

@fabian-hiller
Copy link
Owner

Currently I don't want to add it, as the same can be achieved with minLength and this is not really more cumbersome than nonEmpty. However, I can leave this issue open so that other people can give us feedback on this.

@IlyaSemenov
Copy link
Contributor Author

FWIW, published this at https://www.npmjs.com/package/valibotx

@fabian-hiller
Copy link
Owner

That's great. Feel free to add it on our ecosystem page to the utilities.

@daku10
Copy link

daku10 commented Jan 14, 2024

Hello @fabian-hiller,

Firstly, thank you for your efforts in developing this great product.

I would like to discuss the potential for a Type guard for NonEmptyArray. I noticed that the nonEmpty feature in zod is capable of inferring a Type guard for NonEmptyArray, which I find quite useful in some scenarios.

To my understanding, a similar result can be achieved with the following TypeScript code:

type NonEmptyArray<T> = [T, ...T[]];

export function nonEmptyArray<TItem extends BaseSchema>(item: TItem) {
  return transform(array(item, [minLength(1)]), (value) => {
    return value as NonEmptyArray<Output<TItem>>;
  });
}

However, this implementation lacks flexibility, particularly in terms of configuring additional pipe functions. I believe integrating this functionality into the core features might address this issue.

What are your thoughts on this approach? Any feedback or suggestions would be greatly appreciated.

Thank you for your time and consideration.

@fabian-hiller
Copy link
Owner

I think that the same can be achieved with tuple. Here is a guide describing the differences to array: https://valibot.dev/guides/arrays/

@fabian-hiller
Copy link
Owner

Also I plan to add a required or nonEmpty validation action. I will investigate this in the next fee weeks.

@fabian-hiller fabian-hiller added the priority This has priority label Jan 14, 2024
@daku10
Copy link

daku10 commented Jan 15, 2024

Oh, I missed the tuple function.
Your suggestion is very helpful.
Thank you for your quick and kind reply!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request priority This has priority
Projects
None yet
Development

No branches or pull requests

3 participants