8000 schema/field: validate rune length with `MinRuneLen` / `MaxRuneLen` by liangminhua · Pull Request #4398 · ent/ent · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

schema/field: validate rune length with MinRuneLen / MaxRuneLen #4398

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

Merged
merged 1 commit into from
Jun 20, 2025

Conversation

liangminhua
Copy link
Contributor
  • Issue: The original MaxLen validator used len(v) (byte count), while databases like MySQL/PostgreSQL/SQLite define varchar(size) as character count (not bytes). This caused inconsistent validation for multi-byte characters (e.g., Chinese, Emoji).
  • Fix: Switched to utf8.RuneCountInString(v) to count Unicode characters, ensuring the validation aligns with database constraints.
  • Impact: All supported databases (MySQL/PostgreSQL/SQLite) now have consistent character-length checks at both application and database layers.
  • Future Compatibility:
    • For Oracle/SQL Server (where varchar uses byte counts by default), explicit syntax like varchar(N CHAR) or nvarchar can enforce character limits. This change does not affect existing logic but should be noted for future dialect support.

@a8m
Copy link
Member
a8m commented May 24, 2025

Hey! Thanks for the contribution, but we can’t accept this change, as it will introduce a breaking-change.

Please follow this custom validator example in our doc: https://entgo.io/docs/schema-fields#validators

// MaxRuneCount validates the rune length of a string by using the unicode/utf8 package.
func MaxRuneCount(maxLen int) func(s string) error {
    return func(s string) error {
        if utf8.RuneCountInString(s) > maxLen {
            return errors.New("value is more than the max length")
        }
        return nil
    }
}

field.String("name").
    // If using a SQL-database: change the underlying data type to varchar(10).
    Annotations(entsql.Annotation{
        Size: 10,
    }).
    Validate(MaxRuneCount(10))
field.String("nickname").
    //  If using a SQL-database: change the underlying data type to varchar(20).
    Annotations(entsql.Annotation{
        Size: 20,
    }).
    Validate(MaxRuneCount(20))

@liangminhua
Copy link
Contributor Author

Thanks for the reply! Adding a new method with no disruptive changes.

@a8m
Copy link
Member
a8m commented May 26, 2025

Thanks for the reply! Adding a new method with no disruptive changes.

Thanks for the contribution, but I don't see a reason to add it to the standard ent validators.

@giautm, @masseelch - your thoughts?

@giautm
8000
Copy link
Collaborator
giautm commented May 26, 2025

Thanks for the reply! Adding a new method with no disruptive changes.

Thanks for the contribution, but I don't see a reason to add it to the standard ent validators.

@giautm, @masseelch - your thoughts?

I think it's okay to have this in standard functions, it make life more easy.

@masseelch
Copy link
Collaborator

Thanks for the reply! Adding a new method with no disruptive changes.

Thanks for the contribution, but I don't see a reason to add it to the standard ent validators.
@giautm, @masseelch - your thoughts?

I think it's okay to have this in standard functions, it make life more easy.

I agree with Giau

@a8m
Copy link
Member
a8m commented May 29, 2025

LGTM then.

@liangminhua, just add a small unit-test for it, and one of us will merge this.

@liangminhua liangminhua force-pushed the master branch 2 times, most recently from 1cc0d1c to 666c22f Compare May 31, 2025 18:00
Signed-off-by: liangminhua <ben302010@live.cn>
@liangminhua
Copy link
Contributor Author

Thanks for the review! ✅

Copy link
Collaborator
@masseelch masseelch left a comment

Choose a reason for hiding this comment

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

LGTM

cc @giautm

@giautm giautm changed the title fix(schema/string): validate MaxLen with utf8.RuneCountInString to align with database behaviors schema/field: validate rune length with MinRuneLen / MaxRuneLen Jun 20, 2025
@giautm giautm merged commit 86238d8 into ent:master Jun 20, 2025
6 checks passed
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.

4 participants
0