braze-sync 0.12.0

GitOps CLI for managing Braze configuration as code
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
//! Catalog Schema file I/O.
//!
//! Layout (IMPLEMENTATION.md §9.1):
//!
//! ```text
//! <catalogs_root>/
//! ├── cardiology/
//! │   └── schema.yaml
//! └── dermatology/
//!     └── schema.yaml
//! ```
//!
//! Catalog **items** (row data) are intentionally unsupported. See
//! docs/scope-boundaries.md.

use crate::error::{Error, Result};
use crate::fs::{try_read_resource_dir, validate_resource_name, write_atomic};
use crate::resource::Catalog;
use std::path::Path;

/// Header prepended to every `schema.yaml` written by braze-sync. Editable
/// — the comment is just a hint to humans browsing the file tree.
const SCHEMA_HEADER: &str =
    "# Generated by braze-sync. Edit and run `braze-sync apply` to sync to Braze.\n";

const SCHEMA_FILE_NAME: &str = "schema.yaml";

/// Load every `schema.yaml` under `catalogs_root` into a `Vec<Catalog>`,
/// sorted by catalog name for deterministic output.
///
/// Behaviour:
/// - Missing root → `Ok(vec![])`. A fresh project with no catalogs yet is a
///   valid state.
/// - Root exists but is a file → `Err(InvalidFormat)`.
/// - Subdirectory without `schema.yaml` → silently skipped (auxiliary files
///   outside braze-sync's scope don't break the load).
/// - `schema.yaml` whose `name:` field doesn't match its directory name →
///   `Err(InvalidFormat)`. Stopping here makes diff / apply correctness
///   trivially debuggable.
pub fn load_all_schemas(catalogs_root: &Path) -> Result<Vec<Catalog>> {
    let Some(read_dir) = try_read_resource_dir(catalogs_root, "catalogs")? else {
        return Ok(Vec::new());
    };

    let mut schemas = Vec::new();
    for entry in read_dir {
        let entry = entry?;
        if !entry.file_type()?.is_dir() {
            tracing::debug!(path = %entry.path().display(), "skipping non-directory entry");
            continue;
        }
        let dir = entry.path();
        let schema_path = dir.join(SCHEMA_FILE_NAME);
        if !schema_path.is_file() {
            continue;
        }
        let cat = read_schema_file(&schema_path)?;

        let dir_name = entry.file_name().to_string_lossy().into_owned();
        if cat.name != dir_name {
            return Err(Error::InvalidFormat {
                path: schema_path,
                message: format!(
                    "schema name '{}' does not match its catalog directory '{}'",
                    cat.name, dir_name
                ),
            });
        }
        schemas.push(cat);
    }

    schemas.sort_by(|a, b| a.name.cmp(&b.name));
    Ok(schemas)
}

/// Read a single `schema.yaml` by absolute path. Does not validate that
/// the parent directory name matches `cat.name` — the caller (typically
/// [`load_all_schemas`]) is responsible for that.
///
/// Resource files are forward-compat permissive: unknown fields are
/// ignored, in line with IMPLEMENTATION.md §2.5.
pub fn read_schema_file(path: &Path) -> Result<Catalog> {
    let bytes = std::fs::read_to_string(path)?;
    let cat: Catalog = serde_norway::from_str(&bytes).map_err(|source| Error::YamlParse {
        path: path.to_path_buf(),
        source,
    })?;
    Ok(cat)
}

/// Write `catalog` to `<catalogs_root>/<catalog.name>/schema.yaml`,
/// creating directories as needed. Field ordering is normalized (sorted
/// alphabetically) for deterministic output and diff stability.
///
/// Rejects catalog names that contain path separators or `..` to prevent
/// path traversal — defense in depth on top of the validate command's
/// naming-pattern check (§7.6 / §10).
pub fn save_schema(catalogs_root: &Path, catalog: &Catalog) -> Result<()> {
    validate_resource_name("catalog", &catalog.name)?;

    let dir = catalogs_root.join(&catalog.name);
    let path = dir.join(SCHEMA_FILE_NAME);

    let normalized = catalog.normalized();
    let yaml = serde_norway::to_string(&normalized).map_err(|e| Error::InvalidFormat {
        path: path.clone(),
        message: format!("yaml serialization failed: {e}"),
    })?;

    let mut content = String::with_capacity(SCHEMA_HEADER.len() + yaml.len());
    content.push_str(SCHEMA_HEADER);
    content.push_str(&yaml);

    write_atomic(&path, content.as_bytes())?;
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::resource::{CatalogField, CatalogFieldType};

    fn field(name: &str, t: CatalogFieldType) -> CatalogField {
        CatalogField {
            name: name.into(),
            field_type: t,
        }
    }

    fn cat(name: &str, fields: Vec<CatalogField>) -> Catalog {
        Catalog {
            name: name.into(),
            description: Some(format!("{name} catalog")),
            fields,
        }
    }

    #[test]
    fn round_trip_single_catalog() {
        let dir = tempfile::tempdir().unwrap();
        let c = cat(
            "cardiology",
            vec![
                field("condition_id", CatalogFieldType::String),
                field("display_order", CatalogFieldType::Number),
                field("is_active", CatalogFieldType::Boolean),
            ],
        );
        save_schema(dir.path(), &c).unwrap();
        let loaded = load_all_schemas(dir.path()).unwrap();
        assert_eq!(loaded.len(), 1);
        assert_eq!(loaded[0], c.normalized());
    }

    #[test]
    fn round_trip_all_field_types() {
        let dir = tempfile::tempdir().unwrap();
        let c = cat(
            "everything",
            vec![
                field("a_string", CatalogFieldType::String),
                field("b_number", CatalogFieldType::Number),
                field("c_boolean", CatalogFieldType::Boolean),
                field("d_time", CatalogFieldType::Time),
                field("e_object", CatalogFieldType::Object),
                field("f_array", CatalogFieldType::Array),
            ],
        );
        save_schema(dir.path(), &c).unwrap();
        let loaded = load_all_schemas(dir.path()).unwrap();
        assert_eq!(loaded[0], c);
    }

    #[test]
    fn save_hoists_id_field_to_top_then_sorts_rest() {
        // Mirrors the wire requirement: Braze rejects POST /catalogs
        // bodies whose first field isn't `id`. Keeping the on-disk
        // representation aligned with the wire shape avoids surprise
        // diffs after an `apply` writes back a freshly-created catalog.
        let dir = tempfile::tempdir().unwrap();
        let c = Catalog {
            name: "x".into(),
            description: None,
            fields: vec![
                field("URL", CatalogFieldType::String),
                field("author", CatalogFieldType::String),
                field("id", CatalogFieldType::String),
                field("title", CatalogFieldType::String),
            ],
        };
        save_schema(dir.path(), &c).unwrap();
        let loaded = load_all_schemas(dir.path()).unwrap();
        let names: Vec<_> = loaded[0].fields.iter().map(|f| f.name.as_str()).collect();
        assert_eq!(names, vec!["id", "URL", "author", "title"]);
    }

    #[test]
    fn save_sorts_fields_alphabetically() {
        let dir = tempfile::tempdir().unwrap();
        let c = Catalog {
            name: "x".into(),
            description: None,
            fields: vec![
                field("z", CatalogFieldType::String),
                field("a", CatalogFieldType::String),
                field("m", CatalogFieldType::String),
            ],
        };
        save_schema(dir.path(), &c).unwrap();
        let loaded = load_all_schemas(dir.path()).unwrap();
        assert_eq!(loaded[0].fields[0].name, "a");
        assert_eq!(loaded[0].fields[1].name, "m");
        assert_eq!(loaded[0].fields[2].name, "z");
    }

    #[test]
    fn load_multiple_catalogs_sorted_alphabetically() {
        let dir = tempfile::tempdir().unwrap();
        save_schema(
            dir.path(),
            &cat("zebra", vec![field("id", CatalogFieldType::String)]),
        )
        .unwrap();
        save_schema(
            dir.path(),
            &cat("apple", vec![field("id", CatalogFieldType::String)]),
        )
        .unwrap();
        save_schema(
            dir.path(),
            &cat("mango", vec![field("id", CatalogFieldType::String)]),
        )
        .unwrap();
        let loaded = load_all_schemas(dir.path()).unwrap();
        assert_eq!(
            loaded.iter().map(|c| c.name.as_str()).collect::<Vec<_>>(),
            vec!["apple", "mango", "zebra"]
        );
    }

    #[test]
    fn missing_catalogs_root_returns_empty() {
        let dir = tempfile::tempdir().unwrap();
        let nonexistent = dir.path().join("not_here");
        let loaded = load_all_schemas(&nonexistent).unwrap();
        assert!(loaded.is_empty());
    }

    #[test]
    fn empty_catalogs_root_returns_empty() {
        let dir = tempfile::tempdir().unwrap();
        let loaded = load_all_schemas(dir.path()).unwrap();
        assert!(loaded.is_empty());
    }

    #[test]
    fn catalogs_root_pointing_at_a_file_is_rejected() {
        let dir = tempfile::tempdir().unwrap();
        let file_path = dir.path().join("not_a_dir");
        std::fs::write(&file_path, "x").unwrap();
        let err = load_all_schemas(&file_path).unwrap_err();
        assert!(matches!(err, Error::InvalidFormat { .. }), "got: {err:?}");
    }

    #[test]
    fn dir_without_schema_yaml_is_silently_skipped() {
        let dir = tempfile::tempdir().unwrap();
        // subdirectory that happens to contain some file but no schema.yaml
        let lone = dir.path().join("lonely");
        std::fs::create_dir_all(&lone).unwrap();
        std::fs::write(lone.join("README.md"), "unrelated\n").unwrap();
        // a real catalog
        save_schema(
            dir.path(),
            &cat("real", vec![field("id", CatalogFieldType::String)]),
        )
        .unwrap();

        let loaded = load_all_schemas(dir.path()).unwrap();
        assert_eq!(loaded.len(), 1);
        assert_eq!(loaded[0].name, "real");
    }

    #[test]
    fn schema_name_mismatch_with_dir_name_is_an_error() {
        let dir = tempfile::tempdir().unwrap();
        let cat_dir = dir.path().join("on_disk_name");
        std::fs::create_dir_all(&cat_dir).unwrap();
        std::fs::write(
            cat_dir.join("schema.yaml"),
            "name: in_yaml_name\nfields: []\n",
        )
        .unwrap();

        let err = load_all_schemas(dir.path()).unwrap_err();
        match err {
            Error::InvalidFormat { message, .. } => {
                assert!(message.contains("on_disk_name"));
                assert!(message.contains("in_yaml_name"));
            }
            other => panic!("expected InvalidFormat, got {other:?}"),
        }
    }

    #[test]
    fn unknown_field_in_schema_yaml_is_ignored_for_forward_compat() {
        let dir = tempfile::tempdir().unwrap();
        let cat_dir = dir.path().join("future");
        std::fs::create_dir_all(&cat_dir).unwrap();
        // future_v1_3_field is something a v1.3 binary might emit. v1.0
        // should silently accept it per IMPLEMENTATION.md §2.5.
        let yaml = "\
name: future
description: hi
future_v1_3_field: surprise
fields:
  - name: id
    type: string
";
        std::fs::write(cat_dir.join("schema.yaml"), yaml).unwrap();
        let loaded = load_all_schemas(dir.path()).unwrap();
        assert_eq!(loaded.len(), 1);
        assert_eq!(loaded[0].name, "future");
        assert_eq!(loaded[0].fields.len(), 1);
    }

    #[test]
    fn schema_yaml_with_header_comment_parses() {
        let dir = tempfile::tempdir().unwrap();
        let cat_dir = dir.path().join("commented");
        std::fs::create_dir_all(&cat_dir).unwrap();
        let yaml = "\
# Generated by braze-sync. Edit and run `braze-sync apply` to sync to Braze.
name: commented
fields:
  - name: id
    type: string
";
        std::fs::write(cat_dir.join("schema.yaml"), yaml).unwrap();
        let loaded = load_all_schemas(dir.path()).unwrap();
        assert_eq!(loaded.len(), 1);
        assert_eq!(loaded[0].name, "commented");
    }

    #[test]
    fn save_writes_header_comment() {
        let dir = tempfile::tempdir().unwrap();
        let c = cat("hdr", vec![field("id", CatalogFieldType::String)]);
        save_schema(dir.path(), &c).unwrap();
        let raw = std::fs::read_to_string(dir.path().join("hdr/schema.yaml")).unwrap();
        assert!(
            raw.starts_with("# Generated by braze-sync."),
            "missing header in: {raw}"
        );
    }

    #[test]
    fn save_creates_nested_directories() {
        let dir = tempfile::tempdir().unwrap();
        let nested = dir.path().join("braze").join("catalogs");
        // nested doesn't exist yet
        save_schema(
            &nested,
            &cat("derm", vec![field("id", CatalogFieldType::String)]),
        )
        .unwrap();
        assert!(nested.join("derm").join("schema.yaml").is_file());
    }

    #[test]
    fn save_rejects_path_traversal_in_name() {
        let dir = tempfile::tempdir().unwrap();
        for bad in ["../evil", "..", ".", "", "a/b", "a\\b"] {
            let c = Catalog {
                name: bad.into(),
                description: None,
                fields: vec![],
            };
            let err = save_schema(dir.path(), &c).unwrap_err();
            assert!(
                matches!(err, Error::InvalidFormat { .. }),
                "name {bad:?} should be rejected; got {err:?}"
            );
        }
    }

    #[test]
    fn atomic_save_leaves_no_temp_files() {
        let dir = tempfile::tempdir().unwrap();
        let c = cat("clean", vec![field("id", CatalogFieldType::String)]);
        save_schema(dir.path(), &c).unwrap();
        let cat_dir = dir.path().join("clean");
        let entries: Vec<_> = std::fs::read_dir(&cat_dir)
            .unwrap()
            .map(|e| e.unwrap().file_name().to_string_lossy().into_owned())
            .collect();
        assert_eq!(entries, vec!["schema.yaml".to_string()]);
    }

    #[test]
    fn save_overwrites_existing_schema() {
        let dir = tempfile::tempdir().unwrap();
        let v1 = cat("ovr", vec![field("a", CatalogFieldType::String)]);
        save_schema(dir.path(), &v1).unwrap();
        let v2 = cat(
            "ovr",
            vec![
                field("a", CatalogFieldType::String),
                field("b", CatalogFieldType::Number),
            ],
        );
        save_schema(dir.path(), &v2).unwrap();
        let loaded = load_all_schemas(dir.path()).unwrap();
        assert_eq!(loaded.len(), 1);
        assert_eq!(loaded[0].fields.len(), 2);
    }
}