1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
use serde_json::Value;
use crate::error::{ValidationIssue, VldError};
use crate::schema::VldSchema;
mod checks;
mod formats;
#[cfg(any(feature = "ts", feature = "fake"))]
pub(crate) use formats::{COUNTRIES_ALPHA2, COUNTRIES_ALPHA3, IBAN_LENGTHS};
#[cfg(feature = "openapi")]
use checks::regex_escape;
use checks::{Kind, PasswordRule, StringCheck};
#[cfg(feature = "string-advanced")]
mod typed;
#[cfg(feature = "string-advanced")]
pub use typed::{ZParsedString, ZUrl, ZUuid};
#[derive(Clone)]
enum StringTransform {
Trim,
ToLowerCase,
ToUpperCase,
/// Keep the first `n` characters.
Truncate(usize),
#[cfg(feature = "unicode")]
Nfc,
#[cfg(feature = "unicode")]
Nfkc,
}
/// Schema for string validation. Created via [`vld::string()`](crate::string).
///
/// # Example
/// ```
/// use vld::prelude::*;
///
/// let schema = vld::string().min(3).max(20).email();
/// ```
#[derive(Clone)]
pub struct ZString {
checks: Vec<StringCheck>,
transforms: Vec<StringTransform>,
coerce: bool,
/// `(true, false)` texts of coerced booleans; `None` = `"true"` / `"false"`.
bool_text: Option<(String, String)>,
custom_type_error: Option<String>,
messages: crate::primitives::Messages,
}
impl ZString {
/// Create the schema (the same as its `vld::` constructor function).
pub fn new() -> Self {
Self {
checks: vec![],
transforms: vec![],
coerce: false,
bool_text: None,
custom_type_error: None,
messages: None,
}
}
fn push(mut self, kind: Kind, msg: Option<String>) -> Self {
self.checks.push(StringCheck { kind, msg });
self
}
/// Set a custom error message for type mismatch (when the input is not a string).
///
/// # Example
/// ```
/// use vld::prelude::*;
/// let schema = vld::string().type_error("Must be text!");
/// let err = schema.parse("42").unwrap_err();
/// assert!(err.issues[0].message.contains("Must be text!"));
/// ```
pub fn type_error(mut self, msg: impl Into<String>) -> Self {
self.custom_type_error = Some(msg.into());
self
}
/// Override error messages in bulk by check key.
///
/// The closure receives the check key (e.g. `"too_small"`, `"invalid_email"`)
/// and should return `Some(new_message)` to replace, or `None` to keep the original.
///
/// Keys: `"too_small"`, `"too_big"`, `"invalid_length"`, `"non_empty"`,
/// `"invalid_regex"`, `"invalid_starts_with"`, `"invalid_ends_with"`,
/// `"invalid_contains"`, and `"invalid_<format>"` for every format check
/// (`"invalid_email"`, `"invalid_url"`, `"invalid_uuid"`, `"invalid_ip"`,
/// `"invalid_currency"`, `"invalid_phone_e164"`, …).
///
/// The closure is consulted when an issue is built, so it covers checks
/// added before **and after** this call; a per-check message
/// (`*_msg(...)`) takes precedence over it. A later call replaces it.
///
/// # Example
/// ```
/// use vld::prelude::*;
/// let schema = vld::string().min(3).email()
/// .with_messages(|key| match key {
/// "too_small" => Some("Too short!".into()),
/// "invalid_email" => Some("Bad email!".into()),
/// _ => None,
/// });
/// ```
pub fn with_messages<F>(mut self, f: F) -> Self
where
F: Fn(&str) -> Option<String> + Send + Sync + 'static,
{
self.messages = Some(std::sync::Arc::new(f));
self
}
/// Minimum string length in characters (inclusive).
pub fn min(self, len: usize) -> Self {
self.push(Kind::Min(len), None)
}
/// Minimum UTF-8 length in **bytes** (`too_small`, origin `bytes`):
/// for byte-limited storage. [`min`](Self::min) counts characters.
pub fn min_bytes(self, n: usize) -> Self {
self.push(Kind::MinBytes(n), None)
}
/// Maximum UTF-8 length in **bytes** (`too_big`, origin `bytes`).
/// Not expressible in JSON Schema (`maxLength` counts characters), so it
/// is left out of it.
///
/// ```
/// use vld::prelude::*;
/// let name = vld::string().max_bytes(4);
/// assert!(name.parse(r#""abcd""#).is_ok());
/// assert!(name.parse(r#""абв""#).is_err()); // 3 characters, 6 bytes
/// ```
pub fn max_bytes(self, n: usize) -> Self {
self.push(Kind::MaxBytes(n), None)
}
/// Minimum string length with custom message.
pub fn min_msg(self, len: usize, msg: impl Into<String>) -> Self {
self.push(Kind::Min(len), Some(msg.into()))
}
/// Maximum string length in characters (inclusive).
pub fn max(self, len: usize) -> Self {
self.push(Kind::Max(len), None)
}
/// Maximum string length with custom message.
pub fn max_msg(self, len: usize, msg: impl Into<String>) -> Self {
self.push(Kind::Max(len), Some(msg.into()))
}
/// Exact string length in characters.
pub fn len(self, len: usize) -> Self {
self.push(Kind::Len(len), None)
}
/// Exact string length with custom message.
pub fn len_msg(self, len: usize, msg: impl Into<String>) -> Self {
self.push(Kind::Len(len), Some(msg.into()))
}
/// Must match a regular expression (emitted as JSON Schema `pattern`).
///
/// Takes the source (`r"^[A-Z]{3}$"`, compiled here) or a prebuilt
/// [`Pattern`](crate::rules::Pattern). Requires the `regex` feature.
///
/// # Panics
///
/// When the source is not a valid pattern (as `Regex::new(..).unwrap()`
/// would); build a [`Pattern`](crate::rules::Pattern) with
/// [`Pattern::new`](crate::rules::Pattern::new) to handle the error.
///
/// ```
/// use vld::prelude::*;
/// let sku = vld::string().pattern(r"^[A-Z]{3}-\d{4}$");
/// assert!(sku.parse(r#""ABC-1234""#).is_ok());
/// ```
#[cfg(feature = "regex")]
#[cfg_attr(docsrs, doc(cfg(feature = "regex")))]
pub fn pattern(self, pattern: impl crate::rules::IntoPattern) -> Self {
self.push(Kind::Pattern(pattern.into_pattern()), None)
}
/// [`pattern`](Self::pattern) with a custom message.
#[cfg(feature = "regex")]
#[cfg_attr(docsrs, doc(cfg(feature = "regex")))]
pub fn pattern_msg(
self,
pattern: impl crate::rules::IntoPattern,
msg: impl Into<String>,
) -> Self {
self.push(Kind::Pattern(pattern.into_pattern()), Some(msg.into()))
}
/// Check with a reusable [`Rule`](crate::rules::Rule) on the string
/// (keeps the `ZString` builder; see [`vld::rules`](crate::rules)).
pub fn rule(self, rule: impl crate::rules::Rule<str>) -> Self {
self.push(Kind::Rule(std::sync::Arc::new(rule)), None)
}
/// [`rule`](Self::rule) with a custom message.
pub fn rule_msg(self, rule: impl crate::rules::Rule<str>, msg: impl Into<String>) -> Self {
self.push(Kind::Rule(std::sync::Arc::new(rule)), Some(msg.into()))
}
/// Must start with the given prefix.
pub fn starts_with(self, prefix: impl Into<String>) -> Self {
self.push(Kind::StartsWith(prefix.into()), None)
}
/// Must start with the given prefix, with custom message.
pub fn starts_with_msg(self, prefix: impl Into<String>, msg: impl Into<String>) -> Self {
self.push(Kind::StartsWith(prefix.into()), Some(msg.into()))
}
/// Must end with the given suffix.
pub fn ends_with(self, suffix: impl Into<String>) -> Self {
self.push(Kind::EndsWith(suffix.into()), None)
}
/// Must end with the given suffix, with custom message.
pub fn ends_with_msg(self, suffix: impl Into<String>, msg: impl Into<String>) -> Self {
self.push(Kind::EndsWith(suffix.into()), Some(msg.into()))
}
/// Must contain the given substring.
pub fn contains(self, sub: impl Into<String>) -> Self {
self.push(Kind::Contains(sub.into()), None)
}
/// Must contain the given substring, with custom message.
pub fn contains_msg(self, sub: impl Into<String>, msg: impl Into<String>) -> Self {
self.push(Kind::Contains(sub.into()), Some(msg.into()))
}
/// Must not be empty.
pub fn non_empty(self) -> Self {
self.push(Kind::NonEmpty, None)
}
/// Must not be empty, with custom message.
pub fn non_empty_msg(self, msg: impl Into<String>) -> Self {
self.push(Kind::NonEmpty, Some(msg.into()))
}
/// Trim whitespace before validation.
pub fn trim(mut self) -> Self {
self.transforms.push(StringTransform::Trim);
self
}
/// Convert to lowercase before validation.
pub fn to_lowercase(mut self) -> Self {
self.transforms.push(StringTransform::ToLowerCase);
self
}
/// Keep at most the first `n` characters (before the checks, in the
/// order transforms were added): for storage limits that cut instead of
/// rejecting.
///
/// ```
/// use vld::prelude::*;
/// let title = vld::string().trim().truncate(5);
/// assert_eq!(title.parse(r#"" Hello, world ""#).unwrap(), "Hello");
/// ```
pub fn truncate(mut self, n: usize) -> Self {
self.transforms.push(StringTransform::Truncate(n));
self
}
/// Convert to uppercase before validation.
pub fn to_uppercase(mut self) -> Self {
self.transforms.push(StringTransform::ToUpperCase);
self
}
/// Normalize to Unicode NFC (canonical composition) before the checks:
/// `e` + U+0301 becomes `é`, so equal-looking input compares and
/// counts equal. Requires the `unicode` feature.
///
/// ```
/// use vld::prelude::*;
/// let name = vld::string().normalize_nfc().max(4);
/// assert_eq!(name.parse("\"Cafe\u{301}\"").unwrap(), "Caf\u{e9}");
/// ```
#[cfg(feature = "unicode")]
#[cfg_attr(docsrs, doc(cfg(feature = "unicode")))]
pub fn normalize_nfc(mut self) -> Self {
self.transforms.push(StringTransform::Nfc);
self
}
/// Normalize to Unicode NFKC (compatibility composition) before the
/// checks: also folds full-width forms and ligatures (`fi` → `fi`,
/// `fi` → `fi`) — the form to compare identifiers / usernames in.
/// Requires the `unicode` feature.
#[cfg(feature = "unicode")]
#[cfg_attr(docsrs, doc(cfg(feature = "unicode")))]
pub fn normalize_nfkc(mut self) -> Self {
self.transforms.push(StringTransform::Nfkc);
self
}
/// Password rule: at least one lowercase letter (Unicode `Lowercase`).
///
/// Every password rule reports its own issue
/// (`invalid_string` / `password_lowercase`, `password_uppercase`,
/// `password_digit`, `password_symbol`, `password_repeat`,
/// `password_strength`), so a form can list every unmet rule. Combine
/// with [`min`](Self::min) and [`redact`](crate::schema::VldSchema::redact)
/// so the password never appears in errors:
///
/// ```
/// use vld::prelude::*;
/// let password = vld::string()
/// .min(12)
/// .require_lowercase()
/// .require_uppercase()
/// .require_digit()
/// .require_symbol()
/// .max_repeat(2)
/// .redact();
/// let err = password.parse(r#""aaapassword""#).unwrap_err();
/// let keys: Vec<_> = err.issues.iter().map(|i| i.code.message_keys()[0].clone()).collect();
/// assert_eq!(keys, [
/// "too_small.string",
/// "invalid_string.password_uppercase",
/// "invalid_string.password_digit",
/// "invalid_string.password_symbol",
/// "invalid_string.password_repeat",
/// ]);
/// assert!(!err.to_string().contains("aaapassword"));
/// ```
pub fn require_lowercase(self) -> Self {
self.push(Kind::Password(PasswordRule::Lowercase), None)
}
/// [`require_lowercase`](Self::require_lowercase) with a custom message.
pub fn require_lowercase_msg(self, msg: impl Into<String>) -> Self {
self.push(Kind::Password(PasswordRule::Lowercase), Some(msg.into()))
}
/// Password rule: at least one uppercase letter (Unicode `Uppercase`).
pub fn require_uppercase(self) -> Self {
self.push(Kind::Password(PasswordRule::Uppercase), None)
}
/// [`require_uppercase`](Self::require_uppercase) with a custom message.
pub fn require_uppercase_msg(self, msg: impl Into<String>) -> Self {
self.push(Kind::Password(PasswordRule::Uppercase), Some(msg.into()))
}
/// Password rule: at least one ASCII digit.
pub fn require_digit(self) -> Self {
self.push(Kind::Password(PasswordRule::Digit), None)
}
/// [`require_digit`](Self::require_digit) with a custom message.
pub fn require_digit_msg(self, msg: impl Into<String>) -> Self {
self.push(Kind::Password(PasswordRule::Digit), Some(msg.into()))
}
/// Password rule: at least one symbol — a character that is neither
/// alphanumeric nor whitespace (`!`, `_`, `€`, …).
pub fn require_symbol(self) -> Self {
self.push(Kind::Password(PasswordRule::Symbol), None)
}
/// [`require_symbol`](Self::require_symbol) with a custom message.
pub fn require_symbol_msg(self, msg: impl Into<String>) -> Self {
self.push(Kind::Password(PasswordRule::Symbol), Some(msg.into()))
}
/// Password rule: no character repeated more than `n` times in a row
/// (`max_repeat(2)` rejects `aaa`).
pub fn max_repeat(self, n: usize) -> Self {
self.push(Kind::Password(PasswordRule::MaxRepeat(n)), None)
}
/// [`max_repeat`](Self::max_repeat) with a custom message.
pub fn max_repeat_msg(self, n: usize, msg: impl Into<String>) -> Self {
self.push(Kind::Password(PasswordRule::MaxRepeat(n)), Some(msg.into()))
}
/// Password rule: the [zxcvbn](https://docs.rs/zxcvbn) strength
/// estimate is at least `min_score` (0 = too guessable … 4 = very
/// unguessable; 3 is a common minimum). Requires the
/// `password-strength` feature.
#[cfg(feature = "password-strength")]
#[cfg_attr(docsrs, doc(cfg(feature = "password-strength")))]
pub fn password_strength(self, min_score: u8) -> Self {
self.push(Kind::Password(PasswordRule::Strength(min_score)), None)
}
/// [`password_strength`](Self::password_strength) with a custom message.
#[cfg(feature = "password-strength")]
#[cfg_attr(docsrs, doc(cfg(feature = "password-strength")))]
pub fn password_strength_msg(self, min_score: u8, msg: impl Into<String>) -> Self {
self.push(
Kind::Password(PasswordRule::Strength(min_score)),
Some(msg.into()),
)
}
/// Coerce non-string values (numbers, booleans) to string.
pub fn coerce(mut self) -> Self {
self.coerce = true;
self
}
/// Coerce like [`coerce`](Self::coerce), writing booleans as `on` / `off`
/// instead of `"true"` / `"false"` (legacy stores often keep `"1"` / `"0"`).
///
/// ```
/// use vld::prelude::*;
/// let flag = vld::string().coerce_bools("1", "0");
/// assert_eq!(flag.parse("true").unwrap(), "1");
/// assert_eq!(flag.parse("false").unwrap(), "0");
/// assert_eq!(flag.parse("7").unwrap(), "7");
/// ```
pub fn coerce_bools(mut self, on: impl Into<String>, off: impl Into<String>) -> Self {
self.coerce = true;
self.bool_text = Some((on.into(), off.into()));
self
}
}
impl Default for ZString {
fn default() -> Self {
Self::new()
}
}
impl VldSchema for ZString {
type Output = String;
fn parse_value(&self, value: &Value) -> Result<String, VldError> {
let type_err = || crate::primitives::type_error(&self.custom_type_error, "string", value);
let mut s = match value {
Value::String(s) => s.clone(),
Value::Number(n) if self.coerce => n.to_string(),
Value::Bool(b) if self.coerce => match &self.bool_text {
Some((on, off)) => if *b { on } else { off }.clone(),
None => b.to_string(),
},
_ => return Err(type_err()),
};
for t in &self.transforms {
s = match t {
StringTransform::Trim => s.trim().to_string(),
StringTransform::ToLowerCase => s.to_lowercase(),
StringTransform::ToUpperCase => s.to_uppercase(),
StringTransform::Truncate(n) => match s.char_indices().nth(*n) {
Some((at, _)) => s[..at].to_string(),
None => s,
},
#[cfg(feature = "unicode")]
StringTransform::Nfc => {
unicode_normalization::UnicodeNormalization::nfc(s.as_str()).collect()
}
#[cfg(feature = "unicode")]
StringTransform::Nfkc => {
unicode_normalization::UnicodeNormalization::nfkc(s.as_str()).collect()
}
};
}
// The (transformed) string is reported as the received value; built
// only once something fails.
let mut received: Option<Value> = None;
let mut errors = VldError::new();
for check in &self.checks {
if let Some(code) = check.kind.failure(&s) {
let received = received.get_or_insert_with(|| Value::String(s.clone()));
let bulk = || self.messages.as_ref().and_then(|f| f(check.kind.key()));
let msg = check
.msg
.clone()
.or_else(bulk)
.or_else(|| check.kind.rule_message());
errors.issues.push(
ValidationIssue::with_override(code, msg.as_ref()).with_received(received),
);
}
}
if errors.is_empty() {
Ok(s)
} else {
Err(errors)
}
}
}
/// Generate a JSON Schema representation of this string schema.
///
/// - Length bounds: the strictest `min`/`max`/`len`/`non_empty` wins.
/// - Formats: the first goes into `format`, further ones into `allOf`.
/// - `pattern`/`starts_with`/`ends_with`/`contains` become `pattern`s
/// (first in `pattern`, further ones in `allOf`).
/// - Password rules add `format: "password"` and an `x-password` object
/// (`lowercase`, `uppercase`, `digit`, `symbol`: `true`; `maxRepeat`,
/// `minScore`: numbers).
#[cfg(feature = "openapi")]
impl crate::openapi::JsonSchema for ZString {
fn json_schema(&self) -> serde_json::Value {
let mut schema = serde_json::json!({"type": "string"});
let (mut mins, mut maxs) = (vec![], vec![]);
let mut formats: Vec<&str> = vec![];
let mut patterns: Vec<String> = vec![];
let mut extra = vec![];
let mut password = serde_json::Map::new();
for check in &self.checks {
match &check.kind {
Kind::Min(n) => mins.push(*n),
Kind::Max(n) => maxs.push(*n),
Kind::Len(n) => {
mins.push(*n);
maxs.push(*n);
}
Kind::NonEmpty => mins.push(1),
// `maxLength` counts characters, not bytes.
Kind::MinBytes(_) | Kind::MaxBytes(_) => {}
Kind::StartsWith(p) => patterns.push(format!("^{}", regex_escape(p))),
Kind::EndsWith(p) => patterns.push(format!("{}$", regex_escape(p))),
Kind::Contains(p) => patterns.push(regex_escape(p)),
#[cfg(feature = "regex")]
Kind::Pattern(p) => patterns.push(p.as_str().to_string()),
Kind::Rule(r) => extra.extend(r.json_schema()),
#[cfg(feature = "crypto")]
Kind::Crypto(c) => extra.push(c.json_schema()),
Kind::Format(f) => {
if !formats.contains(&f.schema_format()) {
formats.push(f.schema_format());
}
}
Kind::Password(r) => {
if !formats.contains(&"password") {
formats.push("password");
}
let (name, value) = r.schema_entry();
password.insert(name.to_string(), value);
}
}
}
crate::primitives::length_schema(&mut schema, ("minLength", "maxLength"), mins, maxs);
let mut all_of = vec![];
let mut formats = formats.into_iter();
if let Some(f) = formats.next() {
schema["format"] = serde_json::json!(f);
}
all_of.extend(formats.map(|f| serde_json::json!({ "format": f })));
let mut patterns = patterns.into_iter();
if let Some(p) = patterns.next() {
schema["pattern"] = serde_json::json!(p);
}
all_of.extend(patterns.map(|p| serde_json::json!({ "pattern": p })));
if !all_of.is_empty() {
schema["allOf"] = serde_json::Value::Array(all_of);
}
// Password rules: OpenAPI's `format: password` plus the rules (not
// expressible as ECMA patterns without look-arounds).
if !password.is_empty() {
schema["x-password"] = serde_json::Value::Object(password);
}
for keywords in extra {
crate::rules::merge_keywords(&mut schema, keywords);
}
schema
}
}
#[cfg(feature = "openapi")]
impl crate::openapi::CollectNestedSchemas for ZString {}