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

Add length validators based on the Intl.Segmenter API #784

Closed
remonke opened this issue Aug 15, 2024 · 3 comments
Closed

Add length validators based on the Intl.Segmenter API #784

remonke opened this issue Aug 15, 2024 · 3 comments
Assignees
Labels
enhancement New feature or request

Comments

@remonke
Copy link
Contributor

remonke commented Aug 15, 2024

Actions minLength and maxLength use String.prototype.length, which is not very reliable as it relies on the number of character codes. This approach is not ideal for checking the number of characters the way humans perceive it, especially with emojis. For example, most emojis have a length of 2 (like 🙃), but some have a length of 7 (like 🧑🏻‍💻).

I suggest adding new actions like minGraphemeCount or maxGraphemeCount, which would use the Intl.Segmenter API instead of String.prototype.length. This would be particularly useful when dealing with user-generated content. As of April 2024, the API is supported in all major browsers.

The code would look something like this:

import * as v from 'valibot';

const PostSchema = v.object({
  title: v.pipe(v.string(), v.maxGraphemeCount(300, /* optional language parameter */ 'en')),
});
@fabian-hiller fabian-hiller self-assigned this Aug 15, 2024
@fabian-hiller fabian-hiller added the enhancement New feature or request label Aug 15, 2024
@fabian-hiller
Copy link
Owner

Thank you for your contribution. We have already discussed this in PR #666 (comment). Feel free to create a PR and copy the source code of length, notLength, minLength and maxLength and implement graphemes, notGraphemes, minGraphemes and minGraphemes based on it.

@2-NOW
Copy link

2-NOW commented Sep 29, 2024

Fixes: #853

@fabian-hiller
Copy link
Owner

This has been implemented and will be available in the next release.

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

No branches or pull requests

3 participants