flusso-schema 0.3.2

Config and schema loading for flusso: assembles flusso.toml and *.schema.yml into a validated Config.
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
#![allow(
    unused_crate_dependencies,
    dead_code,
    clippy::unwrap_used,
    clippy::expect_used,
    clippy::indexing_slicing
)]

//! Drift guard between the parsers and the editor-assist JSON Schemas.
//!
//! The schemas the parser crates embed (`config.schema.json` for `flusso.toml`,
//! `index.schema.yml` for `*.schema.yml`) are hand-curated for editor UX, but
//! the **sets** they enumerate — field type tags, field siblings, enum tokens,
//! and sink fields — must stay in lockstep with what the parsers accept. Each
//! test reflects a set from the Rust types and asserts the schema lists exactly
//! the same one.
//!
//! The reflection is compile-coupled to the types, not hand-copied:
//! - enum tokens come from `Serialize` (the real wire name), and the full
//!   variant list is fronted by a **wildcard-free `match`** that fails to
//!   *compile* when a variant is added;
//! - struct field sets come from **field-complete destructures** (no `..`) that
//!   likewise fail to compile when a field is added;
//! - the config-side field sets are read back from a fully-populated sample that
//!   is parsed and re-serialized, so a new field shows up automatically.
//!
//! What this deliberately does NOT check (see the schema file headers and
//! CLAUDE.md): prose descriptions, defaults, the intentionally permissive
//! `field` union grammar, and the identifier `pattern`s — which can't model the
//! newtypes' `trim`/`lowercase` sanitization.

use std::collections::BTreeSet;

use schema::ParseFrom;
use serde::Serialize;
use serde_json::Value;

// ── loading the embedded schemas ──────────────────────────────────────────────
//
// Read the schemas as the crates embed them (`include_str!`), so the test
// validates the bytes that ship and emit — not a loose file that could diverge.

/// The TOML config schema, as JSON.
fn config_schema() -> Value {
    serde_json::from_str(schema::CONFIG_SCHEMA).expect("config schema is JSON")
}

/// The index schema (authored in YAML, but a JSON Schema document), as JSON.
fn index_schema() -> Value {
    serde_yaml::from_str(schema::INDEX_SCHEMA).expect("index schema is YAML")
}

// ── helpers ──────────────────────────────────────────────────────────────────

/// The serde token of a value — its wire name, read from `Serialize` rather than
/// hard-coded, so it tracks any `#[serde(rename)]`.
fn token<T: Serialize>(value: &T) -> String {
    serde_json::to_value(value)
        .expect("serializable")
        .as_str()
        .expect("a string-valued enum token")
        .to_owned()
}

fn tokens<T: Serialize>(values: &[T]) -> BTreeSet<String> {
    values.iter().map(token).collect()
}

fn strs(items: &[&str]) -> BTreeSet<String> {
    items.iter().map(|s| (*s).to_owned()).collect()
}

/// The `enum: [...]` strings at `pointer`.
fn schema_enum(schema: &Value, pointer: &str) -> BTreeSet<String> {
    schema
        .pointer(pointer)
        .unwrap_or_else(|| panic!("no node at {pointer}"))
        .as_array()
        .unwrap_or_else(|| panic!("{pointer} is not an array"))
        .iter()
        .map(|v| v.as_str().expect("enum entry is a string").to_owned())
        .collect()
}

/// The property names of the object at `pointer`.
fn schema_keys(schema: &Value, pointer: &str) -> BTreeSet<String> {
    schema
        .pointer(pointer)
        .unwrap_or_else(|| panic!("no node at {pointer}"))
        .as_object()
        .unwrap_or_else(|| panic!("{pointer} is not an object"))
        .keys()
        .cloned()
        .collect()
}

fn object_keys(value: &Value) -> BTreeSet<String> {
    value.as_object().expect("object").keys().cloned().collect()
}

// ── the parser's enum vocabularies (compile-coupled) ─────────────────────────

fn all_filter_ops() -> Vec<schema_index_yaml::FilterOp> {
    use schema_index_yaml::FilterOp::*;
    fn _exhaustive(op: schema_index_yaml::FilterOp) {
        // Wildcard-free: a new FilterOp variant fails to compile until listed.
        use schema_index_yaml::FilterOp::*;
        match op {
            Eq | Neq | Lt | Lte | Gt | Gte | In | NotIn | Like | Ilike | Between => {}
        }
    }
    vec![Eq, Neq, Lt, Lte, Gt, Gte, In, NotIn, Like, Ilike, Between]
}

fn all_null_ops() -> Vec<schema_index_yaml::NullOp> {
    use schema_index_yaml::NullOp::*;
    fn _exhaustive(op: schema_index_yaml::NullOp) {
        use schema_index_yaml::NullOp::*;
        match op {
            IsNull | IsNotNull => {}
        }
    }
    vec![IsNull, IsNotNull]
}

fn all_transforms() -> Vec<schema_index_yaml::Transform> {
    use schema_index_yaml::Transform::*;
    fn _exhaustive(t: schema_index_yaml::Transform) {
        use schema_index_yaml::Transform::*;
        match t {
            Lowercase | Trim => {}
        }
    }
    vec![Lowercase, Trim]
}

fn all_directions() -> Vec<schema_index_yaml::Direction> {
    use schema_index_yaml::Direction::*;
    fn _exhaustive(d: schema_index_yaml::Direction) {
        use schema_index_yaml::Direction::*;
        match d {
            Asc | Desc => {}
        }
    }
    vec![Asc, Desc]
}

fn all_join_verbs() -> Vec<schema_index_yaml::JoinVerb> {
    use schema_index_yaml::JoinVerb::*;
    fn _exhaustive(j: schema_index_yaml::JoinVerb) {
        use schema_index_yaml::JoinVerb::*;
        match j {
            BelongsTo | HasOne | HasMany | ManyToMany => {}
        }
    }
    vec![BelongsTo, HasOne, HasMany, ManyToMany]
}

fn all_aggregate_ops() -> Vec<schema_index_yaml::AggregateOp> {
    use schema_index_yaml::AggregateOp::*;
    fn _exhaustive(a: schema_index_yaml::AggregateOp) {
        use schema_index_yaml::AggregateOp::*;
        match a {
            Count | Sum | Avg | Min | Max | Ids => {}
        }
    }
    vec![Count, Sum, Avg, Min, Max, Ids]
}

fn all_text_analyses() -> Vec<schema::TextAnalysis> {
    use schema::TextAnalysis::*;
    fn _exhaustive(t: schema::TextAnalysis) {
        use schema::TextAnalysis::*;
        match t {
            Builtin | Icu => {}
        }
    }
    vec![Builtin, Icu]
}

/// The 16 scalar `FlussoType`s — those usable as a field type tag and as an
/// aggregate `value_type`. `GeoPoint`/`Custom` are excluded (their tags are the
/// separate `geo`/`custom` keywords, and they're not valid aggregate results).
fn scalar_type_tokens() -> BTreeSet<String> {
    use schema::FlussoType::*;
    fn _exhaustive(ty: &schema::FlussoType) {
        use schema::FlussoType::*;
        match ty {
            Text | Identifier | Keyword | Enum | Uuid | Boolean | Short | Integer | Long
            | Float | Double | Decimal | Date | Timestamp | Binary | Json => {}
            GeoPoint => {}
            Custom { .. } => {}
        }
    }
    tokens(&[
        Text, Identifier, Keyword, Enum, Uuid, Boolean, Short, Integer, Long, Float, Double,
        Decimal, Date, Timestamp, Binary, Json,
    ])
}

/// The union of every field body's keys, minus the internal `field` (which is
/// the type tag's value on the wire, not a sibling). Fronted by field-complete
/// destructures so adding a body field fails to compile until handled here.
fn body_sibling_keys() -> BTreeSet<String> {
    fn _guards(
        s: schema_index_yaml::ScalarBody,
        c: schema_index_yaml::CustomBody,
        g: schema_index_yaml::GeoBody,
        o: schema_index_yaml::ObjectBody,
        j: schema_index_yaml::JoinBody,
        a: schema_index_yaml::AggregateBody,
        k: schema_index_yaml::ConstantBody,
    ) {
        // No `..`: a new field on any body breaks compilation until added below.
        let schema_index_yaml::ScalarBody {
            field: _,
            column: _,
            required: _,
            options: _,
            transforms: _,
            default: _,
        } = s;
        let schema_index_yaml::CustomBody {
            field: _,
            postgres: _,
            opensearch: _,
            column: _,
            required: _,
            options: _,
            default: _,
        } = c;
        let schema_index_yaml::GeoBody {
            field: _,
            lat: _,
            lon: _,
            column: _,
            required: _,
            options: _,
        } = g;
        let schema_index_yaml::ObjectBody {
            field: _,
            options: _,
            fields: _,
        } = o;
        let schema_index_yaml::JoinBody {
            field: _,
            table: _,
            primary_key: _,
            column: _,
            foreign_key: _,
            through: _,
            filters: _,
            order_by: _,
            limit: _,
            fields: _,
            options: _,
        } = j;
        let schema_index_yaml::AggregateBody {
            field: _,
            table: _,
            column: _,
            value_type: _,
            element_type: _,
            foreign_key: _,
            through: _,
            filters: _,
            options: _,
        } = a;
        let schema_index_yaml::ConstantBody { field: _, value: _ } = k;
    }
    strs(&[
        "column",
        "required",
        "options",
        "transforms",
        "default", // scalar
        "postgres",
        "opensearch", // custom
        "lat",
        "lon",    // geo
        "fields", // object / join
        "table",
        "primary_key",
        "foreign_key",
        "through",
        "filters",
        "order_by",
        "limit",        // join
        "value_type",   // aggregate (sum/min/max)
        "element_type", // aggregate (ids)
        "value",        // constant
    ])
}

// ── index schema ─────────────────────────────────────────────────────────────

/// A field type tag in the schema is any `field` property whose value is exactly
/// a `field_name` ref (the document key); everything else is a sibling.
fn schema_field_props() -> (BTreeSet<String>, BTreeSet<String>) {
    let schema = index_schema();
    let props = schema
        .pointer("/definitions/field/properties")
        .expect("field.properties")
        .as_object()
        .expect("object")
        .clone();
    let is_tag =
        |v: &Value| v.get("$ref").and_then(Value::as_str) == Some("#/definitions/field_name");
    let tags = props
        .iter()
        .filter(|(_, v)| is_tag(v))
        .map(|(k, _)| k.clone())
        .collect();
    let siblings = props
        .iter()
        .filter(|(_, v)| !is_tag(v))
        .map(|(k, _)| k.clone())
        .collect();
    (tags, siblings)
}

#[test]
fn index_field_type_tags_match_parser() {
    let (schema_tags, _) = schema_field_props();

    let mut rust_tags = scalar_type_tokens();
    rust_tags.extend(tokens(&all_join_verbs()));
    rust_tags.extend(tokens(&all_aggregate_ops()));
    // Parser keywords with no 1:1 serializable enum: `geo`→GeoPoint, `object`→
    // Group, `custom`→Custom, `constant`→Constant (see `field::classify`).
    rust_tags.extend(strs(&["geo", "object", "custom", "constant"]));

    assert_eq!(
        schema_tags, rust_tags,
        "field type tags drifted (index.schema.yml vs field::classify)"
    );
}

#[test]
fn index_field_siblings_match_parser() {
    let (_, schema_siblings) = schema_field_props();
    assert_eq!(
        schema_siblings,
        body_sibling_keys(),
        "field siblings drifted (index.schema.yml vs the *Body structs)"
    );
}

#[test]
fn index_value_type_enum_matches_scalar_types() {
    assert_eq!(
        schema_enum(&index_schema(), "/definitions/flusso_type/enum"),
        scalar_type_tokens(),
        "flusso_type/value_type enum drifted (vs FlussoType scalars)"
    );
}

#[test]
fn index_filter_ops_match_parser() {
    let schema = index_schema();
    let branches = schema
        .pointer("/definitions/filter/oneOf")
        .expect("filter.oneOf")
        .as_array()
        .expect("array");

    let mut value_ops = BTreeSet::new();
    let mut null_ops = BTreeSet::new();
    for branch in branches {
        if let Some(arr) = branch
            .pointer("/properties/op/enum")
            .and_then(Value::as_array)
        {
            let set: BTreeSet<String> =
                arr.iter().map(|v| v.as_str().unwrap().to_owned()).collect();
            if set.contains("is_null") {
                null_ops = set;
            } else {
                value_ops = set;
            }
        }
    }

    assert_eq!(
        value_ops,
        tokens(&all_filter_ops()),
        "value filter ops drifted"
    );
    assert_eq!(null_ops, tokens(&all_null_ops()), "null filter ops drifted");
}

#[test]
fn index_transforms_match_parser() {
    assert_eq!(
        schema_enum(&index_schema(), "/definitions/transforms/items/enum"),
        tokens(&all_transforms()),
        "transforms drifted",
    );
}

#[test]
fn index_order_by_directions_match_parser() {
    assert_eq!(
        schema_enum(
            &index_schema(),
            "/definitions/order_by/items/properties/direction/enum"
        ),
        tokens(&all_directions()),
        "order_by directions drifted",
    );
}

// ── config schema ────────────────────────────────────────────────────────────

/// A sample config that populates *every* field (including the optional,
/// skip-when-none ones), parsed and re-serialized so the resulting JSON carries
/// the full key set the parser accepts.
fn populated_config_json() -> Value {
    let toml = r#"
        [source]
        type = "postgres"
        connection_url = { host = "h", port = 5432, user = "u", password = { env = "P" }, database = "d" }

        [sinks.os]
        type = "opensearch"
        url = "https://example:9200"
        username = { env = "U" }
        password = { env = "PW" }
        tls_verify = true
        batch_size = 1
        max_bytes = 1
        timeout_secs = 1
        max_retries = 0
        pipeline = "p"
        number_of_shards = 1
        number_of_replicas = 0
        refresh_interval = "10s"
        text_analysis = "icu"
        auto_subfields = true

        [sinks.out]
        type = "stdout"
        pretty = true

        [[index]]
        name = "i"
        schema = "i.schema.yml"
        enabled = true
        on_error = "skip"
    "#;
    let config = schema_config_toml::ConfigToml::try_parse(toml).expect("sample config parses");
    serde_json::to_value(&config).expect("config serializes")
}

#[test]
fn config_opensearch_sink_fields_match_parser() {
    let cfg = populated_config_json();
    let rust = object_keys(cfg.pointer("/sinks/os").expect("os sink"));
    let schema = schema_keys(&config_schema(), "/definitions/opensearch_sink/properties");
    assert_eq!(rust, schema, "opensearch sink fields drifted");
}

#[test]
fn config_stdout_sink_fields_match_parser() {
    let cfg = populated_config_json();
    let rust = object_keys(cfg.pointer("/sinks/out").expect("out sink"));
    let schema = schema_keys(&config_schema(), "/definitions/stdout_sink/properties");
    assert_eq!(rust, schema, "stdout sink fields drifted");
}

#[test]
fn config_text_analysis_enum_matches_parser() {
    assert_eq!(
        schema_enum(
            &config_schema(),
            "/definitions/opensearch_sink/properties/text_analysis/enum"
        ),
        tokens(&all_text_analyses()),
        "text_analysis enum drifted",
    );
}

#[test]
fn config_sink_types_match_parser() {
    let schema = config_schema();
    let schema_types: BTreeSet<String> = [
        "/definitions/opensearch_sink/properties/type/const",
        "/definitions/stdout_sink/properties/type/const",
    ]
    .iter()
    .map(|p| schema.pointer(p).unwrap().as_str().unwrap().to_owned())
    .collect();

    let cfg = populated_config_json();
    let rust_types: BTreeSet<String> = ["/sinks/os/type", "/sinks/out/type"]
        .iter()
        .map(|p| cfg.pointer(p).unwrap().as_str().unwrap().to_owned())
        .collect();

    assert_eq!(schema_types, rust_types, "sink type discriminators drifted");
}

#[test]
fn config_source_parts_fields_match_parser() {
    // The parts form is the connection_url branch that carries `host`.
    let schema = config_schema();
    let branches = schema
        .pointer("/properties/source/oneOf/0/properties/connection_url/oneOf")
        .expect("connection_url.oneOf")
        .as_array()
        .expect("array");
    let parts = branches
        .iter()
        .find(|b| b.pointer("/properties/host").is_some())
        .expect("a parts branch with host");
    let schema_parts = object_keys(parts.get("properties").expect("properties"));

    let cfg = populated_config_json();
    let rust_parts = object_keys(
        cfg.pointer("/source/connection_url")
            .expect("connection_url"),
    );

    assert_eq!(schema_parts, rust_parts, "source connection parts drifted");
}

#[test]
fn config_index_entry_fields_match_parser() {
    let cfg = populated_config_json();
    let rust = object_keys(cfg.pointer("/index/0").expect("index entry"));
    let schema = schema_keys(&config_schema(), "/properties/index/items/properties");
    assert_eq!(rust, schema, "index entry fields drifted");
}

#[test]
fn config_on_error_enum_matches_parser() {
    let rust = tokens(&[schema::FailurePolicy::Stop, schema::FailurePolicy::Skip]);
    assert_eq!(
        rust,
        schema_enum(&config_schema(), "/properties/on_error/enum"),
        "global on_error tokens drifted from config.schema.json",
    );
    assert_eq!(
        rust,
        schema_enum(
            &config_schema(),
            "/properties/index/items/properties/on_error/enum",
        ),
        "per-index on_error tokens drifted from config.schema.json",
    );
}