data-modelling-core 2.4.0

Core SDK library for model operations across platforms
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
//! JSON Schema validation helpers
//!
//! Provides schema validation for various file formats (ODCS, ODCL, ODPS, CADS, Decision, Knowledge, etc.)
//! This module is gated by the `schema-validation` feature and is available to all SDK consumers.

/// Format validation error with path information
#[cfg(feature = "schema-validation")]
fn format_validation_error(error: &jsonschema::ValidationError, schema_type: &str) -> String {
    // Extract instance path (JSON path where error occurred)
    let instance_path = error.instance_path();

    // Format the path nicely - Location implements Display/Debug
    let path_str = instance_path.to_string();
    let path_str = if path_str == "/" || path_str.is_empty() {
        "root".to_string()
    } else {
        path_str
    };

    // Get the error message
    let error_message = error.to_string();

    format!(
        "{} validation failed at path '{}': {}",
        schema_type, path_str, error_message
    )
}

/// Validate an ODCS file against the ODCS JSON Schema
/// Automatically detects and validates ODCL format files against ODCL schema
/// Returns a string error for use by both CLI and import/export modules
#[cfg(feature = "schema-validation")]
pub fn validate_odcs_internal(content: &str) -> Result<(), String> {
    use jsonschema::Validator;
    use serde_json::Value;

    // Parse YAML content to check format
    let data: Value =
        serde_yaml::from_str(content).map_err(|e| format!("Failed to parse YAML: {}", e))?;

    // Check if this is an ODCL format file (legacy format)
    // ODCL files have "dataContractSpecification" field or simple "name"/"columns" structure
    let is_odcl_format = if let Some(obj) = data.as_object() {
        // Check for ODCL v3 format (dataContractSpecification)
        obj.contains_key("dataContractSpecification")
            // Check for simple ODCL format (name + columns, but no apiVersion/kind/schema)
            || (obj.contains_key("name")
                && obj.contains_key("columns")
                && !obj.contains_key("apiVersion")
                && !obj.contains_key("kind")
                && !obj.contains_key("schema"))
    } else {
        false
    };

    // Validate against ODCL schema if ODCL format detected
    if is_odcl_format {
        return validate_odcl_internal(content);
    }

    // Load ODCS JSON Schema
    let schema_content = include_str!("../../../../schemas/odcs-json-schema-v3.1.0.json");
    let schema: Value = serde_json::from_str(schema_content)
        .map_err(|e| format!("Failed to load ODCS schema: {}", e))?;

    let validator =
        Validator::new(&schema).map_err(|e| format!("Failed to compile ODCS schema: {}", e))?;

    // Validate against ODCS schema
    if let Err(error) = validator.validate(&data) {
        // Extract path information from validation error
        let error_msg = format_validation_error(&error, "ODCS");
        return Err(error_msg);
    }

    Ok(())
}

#[cfg(not(feature = "schema-validation"))]
pub fn validate_odcs_internal(_content: &str) -> Result<(), String> {
    // Validation disabled - feature not enabled
    Ok(())
}

/// Validate an ODCL file against the ODCL JSON Schema
#[cfg(feature = "schema-validation")]
pub fn validate_odcl_internal(content: &str) -> Result<(), String> {
    use jsonschema::Validator;
    use serde_json::Value;

    // Load ODCL JSON Schema
    let schema_content = include_str!("../../../../schemas/odcl-json-schema-1.2.1.json");
    let schema: Value = serde_json::from_str(schema_content)
        .map_err(|e| format!("Failed to load ODCL schema: {}", e))?;

    let validator =
        Validator::new(&schema).map_err(|e| format!("Failed to compile ODCL schema: {}", e))?;

    // Parse YAML content
    let data: Value =
        serde_yaml::from_str(content).map_err(|e| format!("Failed to parse YAML: {}", e))?;

    // Validate
    if let Err(error) = validator.validate(&data) {
        let error_msg = format_validation_error(&error, "ODCL");
        return Err(error_msg);
    }

    Ok(())
}

#[cfg(not(feature = "schema-validation"))]
pub fn validate_odcl_internal(_content: &str) -> Result<(), String> {
    // Validation disabled - feature not enabled
    Ok(())
}

/// Validate an OpenAPI file against the OpenAPI JSON Schema
#[cfg(feature = "schema-validation")]
pub fn validate_openapi_internal(content: &str) -> Result<(), String> {
    use jsonschema::Validator;
    use serde_json::Value;

    // Load OpenAPI JSON Schema
    let schema_content = include_str!("../../../../schemas/openapi-3.1.1.json");
    let schema: Value = serde_json::from_str(schema_content)
        .map_err(|e| format!("Failed to load OpenAPI schema: {}", e))?;

    let validator =
        Validator::new(&schema).map_err(|e| format!("Failed to compile OpenAPI schema: {}", e))?;

    // Parse YAML or JSON content
    let data: Value = if content.trim_start().starts_with('{') {
        serde_json::from_str(content).map_err(|e| format!("Failed to parse JSON: {}", e))?
    } else {
        serde_yaml::from_str(content).map_err(|e| format!("Failed to parse YAML: {}", e))?
    };

    // Validate
    if let Err(error) = validator.validate(&data) {
        return Err(format!("OpenAPI validation failed: {}", error));
    }

    Ok(())
}

#[cfg(not(feature = "schema-validation"))]
pub fn validate_openapi_internal(_content: &str) -> Result<(), String> {
    // Validation disabled - feature not enabled
    Ok(())
}

/// Validate Protobuf file syntax
pub fn validate_protobuf_internal(content: &str) -> Result<(), String> {
    // Basic syntax validation - check for common proto keywords
    if !content.contains("syntax") && !content.contains("message") && !content.contains("enum") {
        return Err("File does not appear to be a valid Protobuf file".to_string());
    }

    // Check for balanced braces (basic syntax check)
    let open_braces = content.matches('{').count();
    let close_braces = content.matches('}').count();
    if open_braces != close_braces {
        return Err(format!(
            "Unbalanced braces in Protobuf file ({} open, {} close)",
            open_braces, close_braces
        ));
    }

    Ok(())
}

/// Validate AVRO file against AVRO specification
pub fn validate_avro_internal(content: &str) -> Result<(), String> {
    // Parse as JSON
    let _value: serde_json::Value =
        serde_json::from_str(content).map_err(|e| format!("Failed to parse AVRO JSON: {}", e))?;

    // Basic validation - check for required AVRO fields
    // More comprehensive validation would require an AVRO schema validator crate
    Ok(())
}

/// Validate JSON Schema file
#[cfg(feature = "schema-validation")]
pub fn validate_json_schema_internal(content: &str) -> Result<(), String> {
    use jsonschema::Validator;
    use serde_json::Value;

    // Parse JSON Schema
    let schema: Value =
        serde_json::from_str(content).map_err(|e| format!("Failed to parse JSON Schema: {}", e))?;

    // Try to compile the schema (this validates the schema itself)
    Validator::new(&schema).map_err(|e| format!("Invalid JSON Schema: {}", e))?;

    Ok(())
}

#[cfg(not(feature = "schema-validation"))]
pub fn validate_json_schema_internal(_content: &str) -> Result<(), String> {
    // Validation disabled - feature not enabled
    Ok(())
}

/// Internal ODPS validation function that returns a string error (used by both CLI and import/export modules)
#[cfg(feature = "schema-validation")]
pub fn validate_odps_internal(content: &str) -> Result<(), String> {
    use jsonschema::Validator;
    use serde_json::Value;

    // Load ODPS JSON Schema
    let schema_content = include_str!("../../../../schemas/odps-json-schema-latest.json");
    let schema: Value = serde_json::from_str(schema_content)
        .map_err(|e| format!("Failed to load ODPS schema: {}", e))?;

    let validator =
        Validator::new(&schema).map_err(|e| format!("Failed to compile ODPS schema: {}", e))?;

    // Parse YAML content
    let data: Value =
        serde_yaml::from_str(content).map_err(|e| format!("Failed to parse YAML: {}", e))?;

    // Validate
    if let Err(error) = validator.validate(&data) {
        let instance_path = error.instance_path();
        let path_str = instance_path.to_string();
        let path_str = if path_str == "/" || path_str.is_empty() {
            "root".to_string()
        } else {
            path_str
        };
        return Err(format!(
            "ODPS validation failed at path '{}': {}",
            path_str, error
        ));
    }

    Ok(())
}

#[cfg(not(feature = "schema-validation"))]
pub fn validate_odps_internal(_content: &str) -> Result<(), String> {
    // Validation disabled - feature not enabled
    Ok(())
}

/// Internal CADS validation function that returns a string error (used by export modules)
#[cfg(feature = "schema-validation")]
pub fn validate_cads_internal(content: &str) -> Result<(), String> {
    use jsonschema::Validator;
    use serde_json::Value;

    // Load CADS JSON Schema
    let schema_content = include_str!("../../../../schemas/cads.schema.json");
    let schema: Value = serde_json::from_str(schema_content)
        .map_err(|e| format!("Failed to load CADS schema: {}", e))?;

    let validator =
        Validator::new(&schema).map_err(|e| format!("Failed to compile CADS schema: {}", e))?;

    // Parse YAML content
    let data: Value =
        serde_yaml::from_str(content).map_err(|e| format!("Failed to parse YAML: {}", e))?;

    // Validate
    if let Err(error) = validator.validate(&data) {
        let instance_path = error.instance_path();
        let path_str = instance_path.to_string();
        let path_str = if path_str == "/" || path_str.is_empty() {
            "root".to_string()
        } else {
            path_str
        };
        return Err(format!(
            "CADS validation failed at path '{}': {}",
            path_str, error
        ));
    }

    Ok(())
}

#[cfg(not(feature = "schema-validation"))]
pub fn validate_cads_internal(_content: &str) -> Result<(), String> {
    // Validation disabled - feature not enabled
    Ok(())
}

/// Validate SQL syntax using sqlparser
pub fn validate_sql_internal(content: &str) -> Result<(), String> {
    use sqlparser::dialect::GenericDialect;
    use sqlparser::parser::Parser;

    let dialect = GenericDialect {};

    Parser::parse_sql(&dialect, content).map_err(|e| format!("SQL validation failed: {}", e))?;

    Ok(())
}

/// Validate a workspace.yaml file against the workspace JSON Schema
#[cfg(feature = "schema-validation")]
pub fn validate_workspace_internal(content: &str) -> Result<(), String> {
    use jsonschema::Validator;
    use serde_json::Value;

    // Load workspace JSON Schema
    let schema_content = include_str!("../../../../schemas/workspace-schema.json");
    let schema: Value = serde_json::from_str(schema_content)
        .map_err(|e| format!("Failed to load workspace schema: {}", e))?;

    let validator = Validator::new(&schema)
        .map_err(|e| format!("Failed to compile workspace schema: {}", e))?;

    // Parse YAML content
    let data: Value =
        serde_yaml::from_str(content).map_err(|e| format!("Failed to parse YAML: {}", e))?;

    // Validate
    if let Err(error) = validator.validate(&data) {
        let error_msg = format_validation_error(&error, "Workspace");
        return Err(error_msg);
    }

    Ok(())
}

#[cfg(not(feature = "schema-validation"))]
pub fn validate_workspace_internal(_content: &str) -> Result<(), String> {
    // Validation disabled - feature not enabled
    Ok(())
}

/// Validate a relationships.yaml file
pub fn validate_relationships_internal(content: &str) -> Result<(), String> {
    use serde_json::Value;

    // Parse YAML content
    let data: Value =
        serde_yaml::from_str(content).map_err(|e| format!("Failed to parse YAML: {}", e))?;

    // Check structure - should be an object with "relationships" array or a direct array
    let relationships = data
        .get("relationships")
        .and_then(|v| v.as_array())
        .or_else(|| data.as_array());

    if let Some(rels) = relationships {
        for (i, rel) in rels.iter().enumerate() {
            // Each relationship should have source_table_id and target_table_id
            if rel.get("source_table_id").is_none() {
                return Err(format!("Relationship {} is missing 'source_table_id'", i));
            }
            if rel.get("target_table_id").is_none() {
                return Err(format!("Relationship {} is missing 'target_table_id'", i));
            }
        }
    }

    Ok(())
}

/// Internal decision validation function that returns a string error (used by import/export modules)
#[cfg(feature = "schema-validation")]
pub fn validate_decision_internal(content: &str) -> Result<(), String> {
    use jsonschema::Validator;
    use serde_json::Value;

    // Load Decision JSON Schema
    let schema_content = include_str!("../../../../schemas/decision-schema.json");
    let schema: Value = serde_json::from_str(schema_content)
        .map_err(|e| format!("Failed to load decision schema: {}", e))?;

    let validator =
        Validator::new(&schema).map_err(|e| format!("Failed to compile decision schema: {}", e))?;

    // Parse YAML content
    let data: Value =
        serde_yaml::from_str(content).map_err(|e| format!("Failed to parse YAML: {}", e))?;

    // Validate
    if let Err(error) = validator.validate(&data) {
        let instance_path = error.instance_path();
        let path_str = instance_path.to_string();
        let path_str = if path_str == "/" || path_str.is_empty() {
            "root".to_string()
        } else {
            path_str
        };
        return Err(format!(
            "Decision validation failed at path '{}': {}",
            path_str, error
        ));
    }

    Ok(())
}

#[cfg(not(feature = "schema-validation"))]
pub fn validate_decision_internal(_content: &str) -> Result<(), String> {
    // Validation disabled - feature not enabled
    Ok(())
}

/// Internal knowledge validation function that returns a string error (used by import/export modules)
#[cfg(feature = "schema-validation")]
pub fn validate_knowledge_internal(content: &str) -> Result<(), String> {
    use jsonschema::Validator;
    use serde_json::Value;

    // Load Knowledge JSON Schema
    let schema_content = include_str!("../../../../schemas/knowledge-schema.json");
    let schema: Value = serde_json::from_str(schema_content)
        .map_err(|e| format!("Failed to load knowledge schema: {}", e))?;

    let validator = Validator::new(&schema)
        .map_err(|e| format!("Failed to compile knowledge schema: {}", e))?;

    // Parse YAML content
    let data: Value =
        serde_yaml::from_str(content).map_err(|e| format!("Failed to parse YAML: {}", e))?;

    // Validate
    if let Err(error) = validator.validate(&data) {
        let instance_path = error.instance_path();
        let path_str = instance_path.to_string();
        let path_str = if path_str == "/" || path_str.is_empty() {
            "root".to_string()
        } else {
            path_str
        };
        return Err(format!(
            "Knowledge validation failed at path '{}': {}",
            path_str, error
        ));
    }

    Ok(())
}

#[cfg(not(feature = "schema-validation"))]
pub fn validate_knowledge_internal(_content: &str) -> Result<(), String> {
    // Validation disabled - feature not enabled
    Ok(())
}

/// Validate a decisions index (decisions.yaml) file against the decisions-index JSON Schema
#[cfg(feature = "schema-validation")]
pub fn validate_decisions_index_internal(content: &str) -> Result<(), String> {
    use jsonschema::Validator;
    use serde_json::Value;

    // Load Decisions Index JSON Schema
    let schema_content = include_str!("../../../../schemas/decisions-index-schema.json");
    let schema: Value = serde_json::from_str(schema_content)
        .map_err(|e| format!("Failed to load decisions-index schema: {}", e))?;

    let validator = Validator::new(&schema)
        .map_err(|e| format!("Failed to compile decisions-index schema: {}", e))?;

    // Parse YAML content
    let data: Value =
        serde_yaml::from_str(content).map_err(|e| format!("Failed to parse YAML: {}", e))?;

    // Validate
    if let Err(error) = validator.validate(&data) {
        let error_msg = format_validation_error(&error, "Decisions Index");
        return Err(error_msg);
    }

    Ok(())
}

#[cfg(not(feature = "schema-validation"))]
pub fn validate_decisions_index_internal(_content: &str) -> Result<(), String> {
    // Validation disabled - feature not enabled
    Ok(())
}

/// Validate a knowledge index (knowledge.yaml) file against the knowledge-index JSON Schema
#[cfg(feature = "schema-validation")]
pub fn validate_knowledge_index_internal(content: &str) -> Result<(), String> {
    use jsonschema::Validator;
    use serde_json::Value;

    // Load Knowledge Index JSON Schema
    let schema_content = include_str!("../../../../schemas/knowledge-index-schema.json");
    let schema: Value = serde_json::from_str(schema_content)
        .map_err(|e| format!("Failed to load knowledge-index schema: {}", e))?;

    let validator = Validator::new(&schema)
        .map_err(|e| format!("Failed to compile knowledge-index schema: {}", e))?;

    // Parse YAML content
    let data: Value =
        serde_yaml::from_str(content).map_err(|e| format!("Failed to parse YAML: {}", e))?;

    // Validate
    if let Err(error) = validator.validate(&data) {
        let error_msg = format_validation_error(&error, "Knowledge Index");
        return Err(error_msg);
    }

    Ok(())
}

#[cfg(not(feature = "schema-validation"))]
pub fn validate_knowledge_index_internal(_content: &str) -> Result<(), String> {
    // Validation disabled - feature not enabled
    Ok(())
}

/// Internal sketch validation function that returns a string error (used by import/export modules)
#[cfg(feature = "schema-validation")]
pub fn validate_sketch_internal(content: &str) -> Result<(), String> {
    use jsonschema::Validator;
    use serde_json::Value;

    // Load Sketch JSON Schema
    let schema_content = include_str!("../../../../schemas/sketch-schema.json");
    let schema: Value = serde_json::from_str(schema_content)
        .map_err(|e| format!("Failed to load sketch schema: {}", e))?;

    let validator =
        Validator::new(&schema).map_err(|e| format!("Failed to compile sketch schema: {}", e))?;

    // Parse YAML content
    let data: Value =
        serde_yaml::from_str(content).map_err(|e| format!("Failed to parse YAML: {}", e))?;

    // Validate
    if let Err(error) = validator.validate(&data) {
        let instance_path = error.instance_path();
        let path_str = instance_path.to_string();
        let path_str = if path_str == "/" || path_str.is_empty() {
            "root".to_string()
        } else {
            path_str
        };
        return Err(format!(
            "Sketch validation failed at path '{}': {}",
            path_str, error
        ));
    }

    Ok(())
}

#[cfg(not(feature = "schema-validation"))]
pub fn validate_sketch_internal(_content: &str) -> Result<(), String> {
    // Validation disabled - feature not enabled
    Ok(())
}

/// Validate a sketch index (sketches.yaml) file against the sketch-index JSON Schema
#[cfg(feature = "schema-validation")]
pub fn validate_sketch_index_internal(content: &str) -> Result<(), String> {
    use jsonschema::Validator;
    use serde_json::Value;

    // Load Sketch Index JSON Schema
    let schema_content = include_str!("../../../../schemas/sketch-index-schema.json");
    let schema: Value = serde_json::from_str(schema_content)
        .map_err(|e| format!("Failed to load sketch-index schema: {}", e))?;

    let validator = Validator::new(&schema)
        .map_err(|e| format!("Failed to compile sketch-index schema: {}", e))?;

    // Parse YAML content
    let data: Value =
        serde_yaml::from_str(content).map_err(|e| format!("Failed to parse YAML: {}", e))?;

    // Validate
    if let Err(error) = validator.validate(&data) {
        let error_msg = format_validation_error(&error, "Sketch Index");
        return Err(error_msg);
    }

    Ok(())
}

#[cfg(not(feature = "schema-validation"))]
pub fn validate_sketch_index_internal(_content: &str) -> Result<(), String> {
    // Validation disabled - feature not enabled
    Ok(())
}

/// Internal DBMV validation function that returns a string error (used by import/export modules)
#[cfg(feature = "schema-validation")]
pub fn validate_dbmv_internal(content: &str) -> Result<(), String> {
    use jsonschema::Validator;
    use serde_json::Value;

    // Load DBMV JSON Schema
    let schema_content = include_str!("../../../../schemas/dbmv.schema.json");
    let schema: Value = serde_json::from_str(schema_content)
        .map_err(|e| format!("Failed to load DBMV schema: {}", e))?;

    let validator =
        Validator::new(&schema).map_err(|e| format!("Failed to compile DBMV schema: {}", e))?;

    // Parse YAML content
    let data: Value =
        serde_yaml::from_str(content).map_err(|e| format!("Failed to parse YAML: {}", e))?;

    // Validate
    if let Err(error) = validator.validate(&data) {
        let error_msg = format_validation_error(&error, "DBMV");
        return Err(error_msg);
    }

    Ok(())
}

#[cfg(not(feature = "schema-validation"))]
pub fn validate_dbmv_internal(_content: &str) -> Result<(), String> {
    // Validation disabled - feature not enabled
    Ok(())
}