rulemorph 0.3.3

YAML-based declarative data transformation engine for CSV/JSON to JSON
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
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
use std::fs;

use rulemorph::{
    ErrorCode, RuleFormat, parse_rule_file, parse_rule_file_with_format, validate_rule_file,
    validate_rule_file_with_source,
};

mod common;

use common::validation::{fixtures_dir, load_expected_errors, load_rule, normalize_errors};

include!("validation/core.rs");

include!("validation/input_format.rs");

include!("validation/rule_format.rs");

#[test]
fn typed_value_codecs_are_validated_statically() {
    let version_one_codec_rule = parse_rule_file(
        r#"
version: 1
input:
  format: json
  json: {}
codecs:
  ddb:
    profile: dynamodb_item
mappings:
  - target: x
    source: id
"#,
    )
    .expect("parse rule");
    let errors =
        validate_rule_file(&version_one_codec_rule).expect_err("codecs are rejected for version 1");
    assert!(errors.iter().any(|err| {
        err.code == ErrorCode::InvalidStep
            && err.path.as_deref() == Some("codecs")
            && err
                .message
                .contains("codecs is only supported in version 2")
    }));

    let typo_rule = parse_rule_file(
        r#"
version: 2
input:
  format: json
  json: {}
codecs:
  bad:
    profile: dynamodb_item
    field_type:
      tags: string_set
mappings:
  - target: x
    value: 1
"#,
    )
    .expect("parse rule");
    let errors = validate_rule_file(&typo_rule).expect_err("codec typo should fail validation");
    assert!(errors.iter().any(|err| {
        err.code == ErrorCode::InvalidExprShape
            && err.message.contains("unknown typed value option")
    }));

    let missing_rule = parse_rule_file(
        r#"
version: 2
input:
  format: json
  json: {}
mappings:
  - target: x
    expr:
      - "@input"
      - to_typed_value:
          codec: missing
"#,
    )
    .expect("parse rule");
    let errors =
        validate_rule_file(&missing_rule).expect_err("unknown codec should fail validation");
    assert!(errors.iter().any(|err| {
        err.code == ErrorCode::InvalidExprShape && err.message.contains("unknown codec binding")
    }));

    let dynamic_options_rule = parse_rule_file(
        r#"
version: 2
input:
  format: json
  json: {}
mappings:
  - target: x
    expr:
      - "@input"
      - op: to_typed_value
        args:
          - - "@input"
            - to_typed_value:
                codec: missing
"#,
    )
    .expect("parse rule");
    let errors = validate_rule_file(&dynamic_options_rule)
        .expect_err("dynamic option expressions should still scan nested codec refs");
    assert!(errors.iter().any(|err| {
        err.code == ErrorCode::InvalidExprShape && err.message.contains("unknown codec binding")
    }));

    let string_codec_rule = parse_rule_file(
        r#"
version: 2
input:
  format: json
  json: {}
codecs:
  ddb: dynamodb_item
mappings:
  - target: x
    value: 1
"#,
    )
    .expect("parse rule");
    let errors = validate_rule_file(&string_codec_rule)
        .expect_err("codec bindings must fail closed on non-object values");
    assert!(errors.iter().any(|err| {
        err.code == ErrorCode::InvalidExprShape
            && err.message.contains("codec binding must be an object")
    }));

    let missing_profile_rule = parse_rule_file(
        r#"
version: 2
input:
  format: json
  json: {}
codecs:
  bad:
    field_types:
      tags: string_set
mappings:
  - target: x
    expr:
      - "@input"
      - to_typed_value: {}
"#,
    )
    .expect("parse rule");
    let errors = validate_rule_file(&missing_profile_rule)
        .expect_err("codec options without profile or codec should fail validation");
    assert!(errors.iter().any(|err| {
        err.code == ErrorCode::InvalidExprShape && err.message.contains("require profile or codec")
    }));

    let invalid_codec_ref_type_rule = parse_rule_file(
        r#"
version: 2
input:
  format: json
  json: {}
mappings:
  - target: x
    expr:
      - "@input"
      - to_typed_value:
          codec: 123
"#,
    )
    .expect("parse rule");
    let errors =
        validate_rule_file(&invalid_codec_ref_type_rule).expect_err("codec refs must be strings");
    assert!(errors.iter().any(|err| {
        err.code == ErrorCode::InvalidExprShape && err.message.contains("codec must be a string")
    }));

    let invalid_shorthand_profile_rule = parse_rule_file(
        r#"
version: 2
input:
  format: json
  json: {}
mappings:
  - target: x
    expr:
      - "@input"
      - to_typed_value: dynamodb_itme
"#,
    )
    .expect("parse rule");
    let errors = validate_rule_file(&invalid_shorthand_profile_rule)
        .expect_err("string shorthand profile must be validated");
    assert!(errors.iter().any(|err| {
        err.code == ErrorCode::InvalidExprShape
            && err.message.contains("unknown typed value profile")
    }));

    let nested_codec_binding_rule = parse_rule_file(
        r#"
version: 2
input:
  format: json
  json: {}
codecs:
  base:
    profile: dynamodb_item
  bad:
    codec: base
mappings:
  - target: x
    value: 1
"#,
    )
    .expect("parse rule");
    let errors = validate_rule_file(&nested_codec_binding_rule)
        .expect_err("codec bindings must not reference other codecs");
    assert!(errors.iter().any(|err| {
        err.code == ErrorCode::InvalidExprShape
            && err
                .message
                .contains("codec binding cannot reference another codec")
    }));

    let invalid_value_rule = parse_rule_file(
        r#"
version: 2
input:
  format: json
  json: {}
codecs:
  bad:
    profile: dynamodb_itme
    decode:
      mode: typo
    field_types:
      id: unknown_type
mappings:
  - target: x
    value: 1
"#,
    )
    .expect("parse rule");
    let errors = validate_rule_file(&invalid_value_rule)
        .expect_err("codec option values should fail validation");
    assert!(errors.iter().any(|err| {
        err.code == ErrorCode::InvalidExprShape
            && err.message.contains("unknown typed value profile")
    }));
    assert!(errors.iter().any(|err| {
        err.code == ErrorCode::InvalidExprShape && err.message.contains("unsupported decode mode")
    }));
    assert!(errors.iter().any(|err| {
        err.code == ErrorCode::InvalidExprShape && err.message.contains("unsupported field type")
    }));

    let mut field_types = String::new();
    for index in 0..500 {
        field_types.push_str(&format!("      f{}: string_set\n", index));
    }
    let mut hints = String::new();
    for index in 0..500 {
        hints.push_str(&format!(
            "      - path: h{}\n        type: string_set\n",
            index
        ));
    }
    let mut number_strings = String::new();
    for index in 0..25 {
        number_strings.push_str(&format!("      - n{}\n", index));
    }
    let too_many_hints_yaml = format!(
        r#"
version: 2
input:
  format: json
  json: {{}}
codecs:
  too_many:
    profile: dynamodb_item
    field_types:
{}    hints:
{}    number_strings:
{}mappings:
  - target: x
    value: 1
"#,
        field_types, hints, number_strings
    );
    let too_many_hints_rule = parse_rule_file(&too_many_hints_yaml).expect("parse rule");
    let errors = validate_rule_file(&too_many_hints_rule)
        .expect_err("combined hint count should fail validation");
    assert!(errors.iter().any(|err| {
        err.code == ErrorCode::InvalidExprShape && err.message.contains("hint count")
    }));
}

#[test]
fn typed_value_codec_validation_matches_runtime_hint_and_profile_rules() {
    let valid_quoted_path_rule = parse_rule_file(
        r#"
version: 2
input:
  format: json
  json: {}
codecs:
  ddb:
    profile: dynamodb_item
    field_types:
      m["x..y"]: string_set
mappings:
  - target: x
    value: 1
"#,
    )
    .expect("parse rule");
    validate_rule_file(&valid_quoted_path_rule)
        .expect("quoted hint paths with dots should validate");

    let invalid_bracket_rule = parse_rule_file(
        r#"
version: 2
input:
  format: json
  json: {}
codecs:
  ddb:
    profile: dynamodb_item
    field_types:
      a[0]: string_set
mappings:
  - target: x
    value: 1
"#,
    )
    .expect("parse rule");
    let errors = validate_rule_file(&invalid_bracket_rule)
        .expect_err("numeric hint index should fail static validation");
    assert!(errors.iter().any(|err| {
        err.code == ErrorCode::InvalidExprShape
            && err.message.contains("invalid hint path bracket syntax")
    }));

    let duplicate_rule = parse_rule_file(
        r#"
version: 2
input:
  format: json
  json: {}
codecs:
  ddb:
    profile: dynamodb_item
    field_types:
      tags: string_set
    hints:
      - path: tags
        type: string_set
mappings:
  - target: x
    value: 1
"#,
    )
    .expect("parse rule");
    let errors =
        validate_rule_file(&duplicate_rule).expect_err("duplicate hints should fail validation");
    assert!(errors.iter().any(|err| {
        err.code == ErrorCode::InvalidExprShape && err.message.contains("duplicate field type path")
    }));

    let non_mongo_policy_rule = parse_rule_file(
        r#"
version: 2
input:
  format: json
  json: {}
codecs:
  ddb:
    profile: dynamodb_item
    allow_dollar_prefixed_fields: true
mappings:
  - target: x
    value: 1
"#,
    )
    .expect("parse rule");
    let errors = validate_rule_file(&non_mongo_policy_rule)
        .expect_err("Mongo-only policy should fail on non-Mongo profile");
    assert!(errors.iter().any(|err| {
        err.code == ErrorCode::InvalidExprShape
            && err
                .message
                .contains("MongoDB options require mongo_extended_json profile")
    }));

    let wrong_provider_type_rule = parse_rule_file(
        r#"
version: 2
input:
  format: json
  json: {}
codecs:
  ddb:
    profile: dynamodb_item
    field_types:
      id: object_id
mappings:
  - target: x
    value: 1
"#,
    )
    .expect("parse rule");
    let errors = validate_rule_file(&wrong_provider_type_rule)
        .expect_err("profile-incompatible hint type should fail validation");
    assert!(errors.iter().any(|err| {
        err.code == ErrorCode::InvalidExprShape
            && err
                .message
                .contains("field type is not supported by dynamodb_item profile")
    }));

    let wrong_provider_type_override_rule = parse_rule_file(
        r#"
version: 2
input:
  format: json
  json: {}
codecs:
  ddb:
    profile: dynamodb_item
mappings:
  - target: x
    expr:
      - "@input"
      - to_typed_value:
          codec: ddb
          field_types:
            id: object_id
"#,
    )
    .expect("parse rule");
    let errors = validate_rule_file(&wrong_provider_type_override_rule)
        .expect_err("profile-incompatible override should fail validation");
    assert!(errors.iter().any(|err| {
        err.code == ErrorCode::InvalidExprShape
            && err
                .message
                .contains("field type is not supported by dynamodb_item profile")
    }));
}

#[test]
fn typed_value_codec_refs_are_validated_across_v2_conditions_and_defs() {
    let cases = [
        r#"
version: 2
input:
  format: json
  json: {}
mappings:
  - target: x
    expr:
      - "@input"
      - if:
          cond:
            eq:
              - ["@input", { to_typed_value: { codec: missing } }]
              - {}
          then: ["$", { get: id }]
          else: ["lit:none"]
"#,
        r#"
version: 2
input:
  format: json
  json: {}
defs:
  encode:
    input: json
    returns: json
    expr:
      - "@input"
      - to_typed_value:
          codec: missing
mappings:
  - target: item
    expr:
      - "@input"
      - encode:
          - with: {}
"#,
        r#"
version: 2
input:
  format: json
  json: {}
mappings:
  - target: id
    expr: "@input.id"
finalize:
  filter:
    eq:
      - ["@item", { to_typed_value: { codec: missing } }]
      - {}
"#,
        r#"
version: 2
input:
  format: json
  json: {}
mappings:
  - target: ids
    expr:
      - "@input.items"
      - map:
          - if:
              cond:
                eq:
                  - ["@item", { to_typed_value: { codec: missing } }]
                  - {}
              then: ["@item"]
              else: ["lit:none"]
"#,
        r#"
version: 2
input:
  format: json
  json: {}
mappings:
  - target: id
    expr: "@input.id"
finalize:
  wrap:
    item:
      - "@out"
      - to_typed_value:
          codec: missing
"#,
    ];

    for yaml in cases {
        let rule = parse_rule_file(yaml).expect("parse rule");
        let errors = validate_rule_file(&rule).expect_err("unknown codec should fail validation");
        assert!(
            errors.iter().any(|err| {
                err.code == ErrorCode::InvalidExprShape
                    && err.message.contains("unknown codec binding")
            }),
            "expected unknown codec validation error, got {errors:?}",
        );
    }
}

#[test]
fn typed_value_codec_refs_are_validated_in_v2_steps_conditions() {
    let cases = [
        r#"
version: 2
input:
  format: json
  json: {}
steps:
  - record_when:
      eq:
        - ["@input", { to_typed_value: { codec: missing } }]
        - {}
    mappings:
      - target: x
        value: 1
"#,
        r#"
version: 2
input:
  format: json
  json: {}
steps:
  - asserts:
      - when:
          eq:
            - ["@input", { to_typed_value: { codec: missing } }]
            - {}
        error:
          code: INVALID
          message: invalid
    mappings:
      - target: x
        value: 1
"#,
        r#"
version: 2
input:
  format: json
  json: {}
steps:
  - branch:
      when:
        eq:
          - ["@input", { to_typed_value: { codec: missing } }]
          - {}
      then: tests/fixtures/tv34_branch_return_true/then.yaml
"#,
    ];

    for yaml in cases {
        let rule = parse_rule_file(yaml).expect("parse rule");
        let errors = validate_rule_file(&rule).expect_err("unknown codec should fail validation");
        assert!(
            errors.iter().any(|err| {
                err.code == ErrorCode::InvalidExprShape
                    && err.message.contains("unknown codec binding")
            }),
            "expected unknown codec validation error, got {errors:?}",
        );
    }
}

// =============================================================================
// v2 Validation Tests
// =============================================================================

include!("validation/v2.rs");