nitr-std 0.0.0-beta.7

The built-in `nitr.*` standard library for Nitr: dbg, fetch, template, json, http, log, crypto, and the SQLite driver.
Documentation
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
// SPDX-License-Identifier: MIT OR Apache-2.0
// This file is part of Nitr.
// See https://nitrweb.com/ for more information
// Copyright (C) 2024-present Jose Quintana <joseluisq.net>

//! The checker: one synchronous walk that applies every Rust rule and
//! collects the script-side checks it must run, then those checks in
//! order — so a value that fails a Rust rule never costs a Lua call, and
//! the Lua that does run is bounded by the caller's budget.
//!
//! Split by concern: [`errors`] is the verdict, [`path`] the position
//! type, [`value`] one value against one rule, [`cross`] the cross-field
//! rules, [`file`] the upload rules, [`util`] the pure helpers. This file
//! holds the context, the table walk, and the deferred-check runner.

use std::collections::BTreeMap;
use std::sync::Arc;

use mlua::{Function, Lua, Table, Value};

mod cross;
mod errors;
mod file;
mod path;
mod util;
mod value;

pub use errors::{ErrorEntry, ValidationError};
use path::FieldPath;
use util::{Verdict, matches_kind, verdict};

use super::format::CustomFormat;
use super::message::{self, AppMessages, Param, Template, default_message};
use super::{Kind, Rule, SchemaDef};

/// Unknown fields listed per table under `strict`; past this one entry
/// gives the remainder, so a hostile body cannot inflate the response.
const MAX_UNKNOWN_LISTED: usize = 32;

/// Every parameter list is tiny; a `Vec` of pairs keeps insertion order
/// for `errors[].params` and a map view for templates.
type Params = Vec<(&'static str, Param)>;

fn params_map(params: &Params) -> BTreeMap<&str, Param> {
    params.iter().map(|(k, v)| (*k, v.clone())).collect()
}

/// Where a checked value landed in the output, so a custom check can
/// replace it.
struct Slot {
    table: Table,
    key: Value,
}

/// A script-side check the sync walk deferred.
enum Pending {
    Field {
        slot: Slot,
        path: String,
        field: String,
        rule: Arc<Rule>,
        owner: Arc<SchemaDef>,
        value: Value,
        check: PendingCheck,
    },
    Schema {
        table: Table,
        prefix: String,
        schema: Arc<SchemaDef>,
        index: usize,
    },
}

enum PendingCheck {
    Custom(Function),
    Format(Arc<CustomFormat>),
    Transform(Function),
}

/// One validation's state.
struct Ctx<'a> {
    lua: &'a Lua,
    strict: bool,
    errors: Vec<ErrorEntry>,
    pending: Vec<Pending>,
    /// One instant per check, read the first time `"now"` is compared.
    now: Option<chrono::DateTime<chrono::Utc>>,
}

impl<'a> Ctx<'a> {
    /// A scratch context for a nested check (the filename rule) that must
    /// not record into the parent.
    fn child(parent: &Ctx<'a>) -> Self {
        Self {
            lua: parent.lua,
            strict: false,
            errors: Vec::new(),
            pending: Vec::new(),
            now: parent.now,
        }
    }

    /// The instant `"now"` bounds compare against: read once per check
    /// through the standard clock (`chrono` is built without its clock
    /// feature), so a request sees one instant.
    fn now(&mut self) -> chrono::DateTime<chrono::Utc> {
        *self.now.get_or_insert_with(|| {
            chrono::DateTime::<chrono::Utc>::from(crate::clock::now_system())
        })
    }

    /// Resolves the message for a failed rule: field per-rule → field-wide
    /// → schema per-rule → app per-rule → `reason` (a custom check's) →
    /// `fallback` (a custom format's or group's own template) → default.
    #[allow(clippy::too_many_arguments)]
    fn resolve(
        &self,
        rule: &str,
        kind: Kind,
        params: &Params,
        field_rule: Option<&Rule>,
        owner: &SchemaDef,
        path: &str,
        fallback: Option<&Template>,
        reason: Option<&str>,
    ) -> String {
        let label = field_rule
            .and_then(|r| r.label.as_deref())
            .map(str::to_string)
            .unwrap_or_else(|| path.rsplit('.').next().unwrap_or(path).to_string());
        let map = params_map(params);
        let render = |t: &Template| t.render(&map, &label, path);
        if let Some(t) = field_rule.and_then(|r| r.messages.get(rule)) {
            return message::sanitize(&render(t));
        }
        if let Some(t) = field_rule.and_then(|r| r.message.as_ref()) {
            return message::sanitize(&render(t));
        }
        if let Some(t) = owner.messages.get(rule) {
            return message::sanitize(&render(t));
        }
        if let Some(app) = self.lua.app_data_ref::<AppMessages>()
            && let Some(t) = app.rules.get(rule)
        {
            return message::sanitize(&render(t));
        }
        if let Some(reason) = reason {
            return message::sanitize(reason);
        }
        if let Some(t) = fallback {
            return message::sanitize(&render(t));
        }
        message::sanitize(&default_message(rule, kind.name(), &map))
    }

    #[allow(clippy::too_many_arguments)]
    fn fail(
        &mut self,
        path: &str,
        field: &str,
        rule: &str,
        kind: Kind,
        params: Params,
        field_rule: Option<&Rule>,
        owner: &SchemaDef,
        fallback: Option<&Template>,
        reason: Option<&str>,
    ) {
        let message = self.resolve(
            rule, kind, &params, field_rule, owner, path, fallback, reason,
        );
        self.errors.push(ErrorEntry {
            path: path.to_string(),
            field: field.to_string(),
            part: None,
            rule: rule.to_string(),
            message,
            params: params
                .into_iter()
                .map(|(k, v)| (k.to_string(), v))
                .collect(),
            label: field_rule.and_then(|r| r.label.clone()),
        });
    }

    /// Validates a table against a field map, building the output table
    /// with only the declared fields — undeclared input never passes
    /// through, so a handler cannot be mass-assigned a field the schema
    /// never mentioned. Returns whether every field passed.
    fn check_fields(
        &mut self,
        schema: &Arc<SchemaDef>,
        input: &Table,
        path: &FieldPath<'_>,
        out: &Table,
    ) -> mlua::Result<bool> {
        let mut ok = true;
        for (name, rule) in &schema.fields {
            let field_path = path.field(name);
            let mut value: Value = input.get(name.as_str())?;
            if value.is_nil()
                && let Some(default) = &rule.default
            {
                value = default.to_lua(self.lua)?;
            }
            if value.is_nil() {
                if rule.required {
                    let p = field_path.render();
                    self.fail(
                        &p,
                        name,
                        "required",
                        rule.kind,
                        Vec::new(),
                        Some(rule),
                        schema,
                        None,
                        None,
                    );
                    ok = false;
                }
                continue;
            }
            let slot = Slot {
                table: out.clone(),
                key: Value::String(self.lua.create_string(name)?),
            };
            match self.check_value(rule, schema, value, &field_path, slot)? {
                Some(value) => out.set(name.as_str(), value)?,
                None => ok = false,
            }
        }
        if self.strict || schema.strict {
            ok &= self.report_unknown(schema, input, path)?;
        }
        if ok {
            ok = self.check_cross(schema, out, path)?;
        }
        if ok && !schema.checks.is_empty() {
            for index in 0..schema.checks.len() {
                self.pending.push(Pending::Schema {
                    table: out.clone(),
                    prefix: path.render(),
                    schema: schema.clone(),
                    index,
                });
            }
        }
        Ok(ok)
    }

    /// `strict`: every undeclared key is an error, bounded in number.
    fn report_unknown(
        &mut self,
        schema: &Arc<SchemaDef>,
        input: &Table,
        path: &FieldPath<'_>,
    ) -> mlua::Result<bool> {
        let mut ok = true;
        let mut listed = 0usize;
        let mut extra = 0usize;
        for pair in input.pairs::<Value, Value>() {
            let (key, _) = pair?;
            let Value::String(key) = key else { continue };
            let key = key.to_string_lossy().to_string();
            if schema.rule(&key).is_some() {
                continue;
            }
            ok = false;
            if listed < MAX_UNKNOWN_LISTED {
                listed += 1;
                let p = path.key(&key).render();
                self.fail(
                    &p,
                    &key,
                    "unknown",
                    Kind::Any,
                    Vec::new(),
                    None,
                    schema,
                    None,
                    None,
                );
            } else {
                extra += 1;
            }
        }
        if extra > 0 {
            self.errors.push(ErrorEntry {
                path: path.render(),
                field: path.field_name().to_string(),
                part: None,
                rule: "unknown".into(),
                message: format!("has {extra} more unknown fields"),
                params: vec![("count".into(), Param::Num(extra as f64))],
                label: None,
            });
        }
        Ok(ok)
    }
}

/// Runs the deferred field-level checks (custom `check`s and custom
/// formats), in arrival order.
async fn run_field_checks(ctx: &mut Ctx<'_>, pending: Vec<Pending>) -> mlua::Result<Vec<Pending>> {
    let mut schema_checks = Vec::new();
    let mut failed: std::collections::HashSet<String> = std::collections::HashSet::new();
    for item in pending {
        let Pending::Field {
            slot,
            path,
            field,
            rule,
            owner,
            value,
            check,
        } = item
        else {
            schema_checks.push(item);
            continue;
        };
        if let PendingCheck::Transform(func) = &check {
            // A transform runs only for a field whose checks all passed.
            if failed.contains(&path) {
                continue;
            }
            let replacement = func.call_async::<Value>(value.clone()).await?;
            if !matches_kind(&replacement, rule.kind) {
                return Err(mlua::Error::RuntimeError(format!(
                    "the transform for `{path}` returned a {}, but the field is a `{}`",
                    replacement.type_name(),
                    rule.kind.name()
                )));
            }
            slot.table.set(slot.key, replacement)?;
            continue;
        }
        let (func, what, rule_name) = match &check {
            PendingCheck::Transform(_) => continue,
            PendingCheck::Custom(f) => (f.clone(), format!("the check for `{path}`"), "check"),
            PendingCheck::Format(c) => (
                c.check.clone(),
                format!("the format `{}` check for `{path}`", c.name),
                "format",
            ),
        };
        let results = func.call_async::<mlua::MultiValue>(value.clone()).await?;
        match verdict(results, &what)? {
            Verdict::Pass => {}
            Verdict::Fail(reason) => {
                failed.insert(path.clone());
                let reason = match reason {
                    None => None,
                    Some(Value::String(s)) => Some(s.to_string_lossy().to_string()),
                    Some(other) => {
                        return Err(mlua::Error::RuntimeError(format!(
                            "{what} returned a reason of type {}; a reason is a string",
                            other.type_name()
                        )));
                    }
                };
                let (params, fallback): (Params, Option<&Template>) = match &check {
                    PendingCheck::Custom(_) | PendingCheck::Transform(_) => (Vec::new(), None),
                    PendingCheck::Format(c) => (
                        vec![("format", Param::Str(format!("a valid {}", c.name)))],
                        c.message.as_ref(),
                    ),
                };
                ctx.fail(
                    &path,
                    &field,
                    rule_name,
                    rule.kind,
                    params,
                    Some(&rule),
                    &owner,
                    fallback,
                    reason.as_deref(),
                );
            }
        }
    }
    Ok(schema_checks)
}

/// Runs the schema-level checks, only reached when nothing else failed.
async fn run_schema_checks(ctx: &mut Ctx<'_>, pending: Vec<Pending>) -> mlua::Result<()> {
    for item in pending {
        let Pending::Schema {
            table,
            prefix,
            schema,
            index,
        } = item
        else {
            continue;
        };
        let check = &schema.checks[index];
        let what = format!("schema check {} (\"{}\")", index + 1, check.description);
        let results = check
            .func
            .call_async::<mlua::MultiValue>(table.clone())
            .await?;
        let Verdict::Fail(reason) = verdict(results, &what)? else {
            continue;
        };
        let root = if prefix == "$" {
            String::new()
        } else {
            prefix.clone()
        };
        match reason {
            None => ctx.fail(
                &prefix,
                "",
                "check",
                Kind::Table,
                Vec::new(),
                None,
                &schema,
                check.message.as_ref(),
                None,
            ),
            Some(Value::String(s)) => {
                let s = s.to_string_lossy().to_string();
                ctx.fail(
                    &prefix,
                    "",
                    "check",
                    Kind::Table,
                    Vec::new(),
                    None,
                    &schema,
                    check.message.as_ref(),
                    Some(&s),
                );
            }
            Some(Value::Table(map)) => {
                for pair in map.pairs::<Value, Value>() {
                    let (key, reason) = pair?;
                    let (Value::String(key), Value::String(reason)) = (key, reason) else {
                        return Err(mlua::Error::RuntimeError(format!(
                            "{what} returned a reason table whose entries are not field = \"reason\""
                        )));
                    };
                    let field = key.to_string_lossy().to_string();
                    let Some(rule) = schema.rule(&field) else {
                        return Err(mlua::Error::RuntimeError(format!(
                            "{what} named `{field}`, which is not a field of the schema"
                        )));
                    };
                    let path = if root.is_empty() {
                        field.clone()
                    } else {
                        format!("{root}.{field}")
                    };
                    let reason = reason.to_string_lossy().to_string();
                    ctx.fail(
                        &path,
                        &field,
                        "check",
                        rule.kind,
                        Vec::new(),
                        Some(rule),
                        &schema,
                        check.message.as_ref(),
                        Some(&reason),
                    );
                }
            }
            Some(other) => {
                return Err(mlua::Error::RuntimeError(format!(
                    "{what} returned a reason of type {}; a reason is a string or a table",
                    other.type_name()
                )));
            }
        }
    }
    Ok(())
}

/// How deep validations may nest through their own script checks. A
/// `check` that calls `schema:check` on the schema it belongs to would
/// otherwise recurse until the Rust stack gives out — Lua's C-stack
/// bound does not fire first across the async boundary — and a stack
/// overflow aborts the process instead of unwinding into a 500.
const MAX_NESTING: u32 = 8;

/// The live nesting depth of validation runs on one Lua state. Runs on a
/// state are strictly nested (one request at a time, an inner
/// `schema:check` awaited by the outer), so one counter is exact.
#[derive(Default)]
struct Nesting(u32);

/// Decrements the depth however the run ends.
struct NestingGuard<'a>(&'a Lua);

impl Drop for NestingGuard<'_> {
    fn drop(&mut self) {
        if let Some(mut n) = self.0.app_data_mut::<Nesting>() {
            n.0 = n.0.saturating_sub(1);
        }
    }
}

fn enter(lua: &Lua) -> mlua::Result<NestingGuard<'_>> {
    if lua.app_data_ref::<Nesting>().is_none() {
        lua.set_app_data(Nesting::default());
    }
    let mut n = lua.app_data_mut::<Nesting>().ok_or_else(|| {
        mlua::Error::RuntimeError("the validation nesting counter is borrowed".into())
    })?;
    if n.0 >= MAX_NESTING {
        return Err(mlua::Error::RuntimeError(format!(
            "validation nested deeper than {MAX_NESTING} levels: does a `check` call \
             `schema:check` on a schema whose own check calls it back?"
        )));
    }
    n.0 += 1;
    Ok(NestingGuard(lua))
}

/// Runs one validation: the sync walk, then the deferred script checks.
pub(super) async fn run(
    lua: &Lua,
    schema: &Arc<SchemaDef>,
    value: Value,
    strict: Option<bool>,
) -> mlua::Result<Result<Table, ValidationError>> {
    let _nesting = enter(lua)?;
    let mut ctx = Ctx {
        lua,
        strict: strict.unwrap_or(false),
        errors: Vec::new(),
        pending: Vec::new(),
        now: None,
    };
    let out = lua.create_table()?;
    match &value {
        Value::Table(input) => {
            ctx.check_fields(schema, input, &FieldPath::ROOT, &out)?;
        }
        other => {
            let p = FieldPath::ROOT.render();
            ctx.fail(
                &p,
                "",
                "body",
                Kind::Table,
                vec![("type", Param::Str(other.type_name().into()))],
                None,
                schema,
                None,
                None,
            );
        }
    }

    // Field-level script checks first (in arrival order), then schema
    // checks — and those only when nothing else failed, so a check can
    // trust the shape it receives.
    let pending = std::mem::take(&mut ctx.pending);
    let schema_checks = run_field_checks(&mut ctx, pending).await?;
    if ctx.errors.is_empty() {
        run_schema_checks(&mut ctx, schema_checks).await?;
    }

    if ctx.errors.is_empty() {
        return Ok(Ok(out));
    }
    let mut entries = std::mem::take(&mut ctx.errors);
    entries.sort_by(|a, b| a.path.cmp(&b.path));
    let summary = lua
        .app_data_ref::<AppMessages>()
        .and_then(|app| app.summary.clone())
        .unwrap_or_else(|| "validation failed".into());
    Ok(Err(ValidationError {
        message: summary,
        entries,
    }))
}