-
Notifications
You must be signed in to change notification settings - Fork 175
Description
The current validator crate provides built-in validators for various use cases, but it lacks a validator for checking the length of a string based on its UTF-16 code units. This feature request proposes the addition of a UTF-16 code units length validator to the crate.
The motivation behind this request stems from the need to match the behavior of the HTML textarea maxlength attribute, which counts UTF-16 code units. To provide better consistency between frontend and backend validation, it would be useful to have a validator that directly checks the length of a string based on its UTF-16 code units.
The new validator could be used as follows:
use validator::Validate;
#[derive(Debug, Validate)]
struct MyStruct {
#[validate(utf16_length(min = 1, max = "N"))]
field: String,
}Replace N with the desired UTF-16 code unit count. Use the same N for the HTML textarea.
N would be max bytes / 2, as UTF-16 code units are 2 bytes long.
This new validator would ensure that the code unit count limits are consistent between the HTML textarea and Rust, despite the different character encodings used, and avoid false negatives.