germanic 0.2.3

Schema-validated binary data for AI agents. JSON to .grm compiler with zero-copy FlatBuffers.
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
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
//! # JSON Schema Draft 7 Adapter
//!
//! Converts JSON Schema Draft 7 input into GERMANIC's internal
//! [`SchemaDefinition`] format. This provides a second "entry door"
//! so that tools speaking standard JSON Schema (e.g. OpenClaw llm-task)
//! can use GERMANIC without knowing the proprietary format.
//!
//! ```text
//!                               +------------------------------+
//!   .schema.json (GERMANIC) --->|                              |
//!                               |      SchemaDefinition        |
//!                               |   (internal source of truth) |---> validate ---> compile
//!   .json (JSON Schema D7) --->|                              |
//!             ^                 +------------------------------+
//!             |
//!        json_schema.rs
//!        (this module)
//! ```
//!
//! ## Supported Features
//!
//! - `type`: string, boolean, integer, number, object, array
//! - `required`: object-level list inverted to per-field flags
//! - `default`: passed through as string
//! - `properties`: recursive conversion (nested objects become Tables)
//! - `items`: array item type inference (string/integer arrays)
//!
//! ## Intentionally Ignored (with warnings)
//!
//! `$ref`, `anyOf`, `oneOf`, `allOf`, `enum`, `pattern`, `minimum`,
//! `maximum`, `format`, `additionalProperties`

use indexmap::IndexMap;
use serde::Deserialize;

use super::schema_def::{FieldDefinition, FieldType, SchemaDefinition};
use crate::error::GermanicError;

// ============================================================================
// JSON SCHEMA STRUCTS (input deserialization)
// ============================================================================

/// Reduced JSON Schema representation -- only the features GERMANIC needs.
#[derive(Debug, Deserialize)]
struct JsonSchema {
    #[serde(rename = "$schema")]
    #[allow(dead_code)]
    schema_url: Option<String>,

    #[serde(rename = "type")]
    typ: Option<String>,

    properties: Option<IndexMap<String, JsonSchemaProperty>>,
    required: Option<Vec<String>>,

    #[serde(rename = "$id")]
    id: Option<String>,

    title: Option<String>,

    #[allow(dead_code)]
    description: Option<String>,
}

/// A single property in a JSON Schema object.
#[derive(Debug, Deserialize)]
struct JsonSchemaProperty {
    #[serde(rename = "type")]
    typ: Option<String>,

    properties: Option<IndexMap<String, JsonSchemaProperty>>,
    required: Option<Vec<String>>,
    items: Option<Box<JsonSchemaProperty>>,
    default: Option<serde_json::Value>,

    // Recognized but only warned about:
    #[serde(rename = "$ref")]
    reference: Option<String>,
    #[serde(rename = "anyOf")]
    any_of: Option<serde_json::Value>,
    #[serde(rename = "oneOf")]
    one_of: Option<serde_json::Value>,
    #[serde(rename = "allOf")]
    all_of: Option<serde_json::Value>,
    #[serde(rename = "enum")]
    enum_values: Option<serde_json::Value>,
    #[allow(dead_code)]
    pattern: Option<String>,
}

// ============================================================================
// PUBLIC API
// ============================================================================

/// Detects whether a JSON string is a JSON Schema Draft 7.
///
/// Heuristic: has `"$schema"` key, OR has `"type": "object"` + `"properties"`.
pub fn is_json_schema(input: &str) -> bool {
    let Ok(value) = serde_json::from_str::<serde_json::Value>(input) else {
        return false;
    };
    let Some(obj) = value.as_object() else {
        return false;
    };

    // Definitive: has $schema key
    if obj.contains_key("$schema") {
        return true;
    }

    // Heuristic: has "type": "object" + "properties"
    let is_object_type = obj
        .get("type")
        .and_then(|v| v.as_str())
        .is_some_and(|t| t == "object");
    let has_properties = obj.contains_key("properties");

    is_object_type && has_properties
}

/// Converts a JSON Schema Draft 7 string into a [`SchemaDefinition`].
///
/// Returns `(SchemaDefinition, Vec<String>)` where the second element
/// contains warnings for unsupported features that were ignored.
///
/// # Errors
///
/// Returns `GermanicError` if:
/// - The input is not valid JSON
/// - The root type is not `"object"`
/// - Array items have mixed/unsupported types
pub fn convert_json_schema(input: &str) -> Result<(SchemaDefinition, Vec<String>), GermanicError> {
    let js: JsonSchema = serde_json::from_str(input)?;
    let mut warnings: Vec<String> = Vec::new();

    // Root must be "type": "object"
    match js.typ.as_deref() {
        Some("object") | None => {} // None is acceptable if properties exist
        Some(other) => {
            return Err(GermanicError::General(format!(
                "JSON Schema root must be \"object\", found \"{}\"",
                other
            )));
        }
    }

    // Derive schema_id from $id, title, or generate fallback
    let schema_id = js
        .id
        .or(js.title.map(|t| t.to_lowercase().replace(' ', "-")))
        .unwrap_or_else(|| "converted.json-schema.v1".to_string());

    // Convert properties
    let required_list = js.required.unwrap_or_default();
    let fields = match js.properties {
        Some(props) => convert_properties(props, &required_list, &mut warnings)?,
        None => IndexMap::new(),
    };

    let schema = SchemaDefinition {
        schema_id,
        version: 1,
        fields,
    };

    Ok((schema, warnings))
}

// ============================================================================
// INTERNAL CONVERSION
// ============================================================================

/// Converts a map of JSON Schema properties into GERMANIC FieldDefinitions.
fn convert_properties(
    properties: IndexMap<String, JsonSchemaProperty>,
    required_list: &[String],
    warnings: &mut Vec<String>,
) -> Result<IndexMap<String, FieldDefinition>, GermanicError> {
    let mut fields = IndexMap::new();

    for (name, prop) in properties {
        let is_required = required_list.contains(&name);
        let field = convert_property(&name, prop, is_required, warnings)?;
        fields.insert(name, field);
    }

    Ok(fields)
}

/// Converts a single JSON Schema property to a GERMANIC FieldDefinition.
fn convert_property(
    name: &str,
    prop: JsonSchemaProperty,
    required: bool,
    warnings: &mut Vec<String>,
) -> Result<FieldDefinition, GermanicError> {
    // Emit warnings for unsupported features
    if prop.reference.is_some() {
        warnings.push(format!(
            "Field \"{name}\": $ref not resolved (not supported)"
        ));
    }
    if prop.any_of.is_some() {
        warnings.push(format!("Field \"{name}\": anyOf not supported, ignored"));
    }
    if prop.one_of.is_some() {
        warnings.push(format!("Field \"{name}\": oneOf not supported, ignored"));
    }
    if prop.all_of.is_some() {
        warnings.push(format!("Field \"{name}\": allOf not supported, ignored"));
    }
    if prop.enum_values.is_some() {
        warnings.push(format!("Field \"{name}\": enum constraint ignored"));
    }

    // Determine field type
    let typ_str = prop.typ.as_deref().unwrap_or("string");

    let (field_type, nested_fields) = match typ_str {
        "string" => (FieldType::String, None),
        "boolean" => (FieldType::Bool, None),
        "integer" => (FieldType::Int, None),
        "number" => (FieldType::Float, None),
        "object" => {
            let nested_required = prop.required.unwrap_or_default();
            let nested = match prop.properties {
                Some(props) => Some(convert_properties(props, &nested_required, warnings)?),
                None => Some(IndexMap::new()),
            };
            (FieldType::Table, nested)
        }
        "array" => {
            let array_type = resolve_array_type(name, &prop.items)?;
            (array_type, None)
        }
        other => {
            warnings.push(format!(
                "Field \"{name}\": unknown type \"{other}\", defaulting to string"
            ));
            (FieldType::String, None)
        }
    };

    // Convert default value to string representation
    let default = prop.default.map(|v| match v {
        serde_json::Value::String(s) => s,
        other => other.to_string(),
    });

    Ok(FieldDefinition {
        field_type,
        required,
        default,
        fields: nested_fields,
    })
}

/// Determines the GERMANIC array type from JSON Schema `items`.
fn resolve_array_type(
    field_name: &str,
    items: &Option<Box<JsonSchemaProperty>>,
) -> Result<FieldType, GermanicError> {
    let Some(items) = items else {
        // No items specified, default to string array
        return Ok(FieldType::StringArray);
    };

    match items.typ.as_deref() {
        Some("string") | None => Ok(FieldType::StringArray),
        Some("integer") => Ok(FieldType::IntArray),
        Some("number") => Ok(FieldType::IntArray), // Closest mapping
        Some(other) => Err(GermanicError::General(format!(
            "Field \"{field_name}\": unsupported array item type \"{other}\""
        ))),
    }
}

// ============================================================================
// TESTS
// ============================================================================

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_simple_object() {
        let input = r#"{
            "type": "object",
            "properties": {
                "name": { "type": "string" }
            }
        }"#;

        let (schema, warnings) = convert_json_schema(input).unwrap();
        assert_eq!(schema.fields.len(), 1);
        assert_eq!(schema.fields["name"].field_type, FieldType::String);
        assert!(!schema.fields["name"].required);
        assert!(warnings.is_empty());
    }

    #[test]
    fn test_required_inversion() {
        let input = r#"{
            "type": "object",
            "required": ["a", "b"],
            "properties": {
                "a": { "type": "string" },
                "b": { "type": "integer" },
                "c": { "type": "string" }
            }
        }"#;

        let (schema, _) = convert_json_schema(input).unwrap();
        assert!(schema.fields["a"].required);
        assert!(schema.fields["b"].required);
        assert!(!schema.fields["c"].required);
    }

    #[test]
    fn test_nested_object() {
        let input = r#"{
            "type": "object",
            "properties": {
                "address": {
                    "type": "object",
                    "required": ["street"],
                    "properties": {
                        "street": { "type": "string" },
                        "city": { "type": "string" }
                    }
                }
            }
        }"#;

        let (schema, _) = convert_json_schema(input).unwrap();
        assert_eq!(schema.fields["address"].field_type, FieldType::Table);
        let nested = schema.fields["address"].fields.as_ref().unwrap();
        assert_eq!(nested.len(), 2);
        assert!(nested["street"].required);
        assert!(!nested["city"].required);
    }

    #[test]
    fn test_string_array() {
        let input = r#"{
            "type": "object",
            "properties": {
                "tags": {
                    "type": "array",
                    "items": { "type": "string" }
                }
            }
        }"#;

        let (schema, _) = convert_json_schema(input).unwrap();
        assert_eq!(schema.fields["tags"].field_type, FieldType::StringArray);
    }

    #[test]
    fn test_int_array() {
        let input = r#"{
            "type": "object",
            "properties": {
                "scores": {
                    "type": "array",
                    "items": { "type": "integer" }
                }
            }
        }"#;

        let (schema, _) = convert_json_schema(input).unwrap();
        assert_eq!(schema.fields["scores"].field_type, FieldType::IntArray);
    }

    #[test]
    fn test_default_values() {
        let input = r#"{
            "type": "object",
            "properties": {
                "country": { "type": "string", "default": "DE" },
                "count": { "type": "integer", "default": 42 }
            }
        }"#;

        let (schema, _) = convert_json_schema(input).unwrap();
        assert_eq!(schema.fields["country"].default, Some("DE".into()));
        assert_eq!(schema.fields["count"].default, Some("42".into()));
    }

    #[test]
    fn test_schema_id_from_dollar_id() {
        let input = r#"{
            "$id": "practice.v1",
            "type": "object",
            "properties": {}
        }"#;

        let (schema, _) = convert_json_schema(input).unwrap();
        assert_eq!(schema.schema_id, "practice.v1");
    }

    #[test]
    fn test_schema_id_from_title() {
        let input = r#"{
            "title": "My Practice",
            "type": "object",
            "properties": {}
        }"#;

        let (schema, _) = convert_json_schema(input).unwrap();
        assert_eq!(schema.schema_id, "my-practice");
    }

    #[test]
    fn test_warning_on_ref() {
        let input = r##"{
            "type": "object",
            "properties": {
                "other": { "$ref": "#/definitions/Other" }
            }
        }"##;

        let (_, warnings) = convert_json_schema(input).unwrap();
        assert_eq!(warnings.len(), 1);
        assert!(warnings[0].contains("$ref"));
    }

    #[test]
    fn test_warning_on_any_of() {
        let input = r#"{
            "type": "object",
            "properties": {
                "value": { "anyOf": [{"type": "string"}, {"type": "integer"}] }
            }
        }"#;

        let (_, warnings) = convert_json_schema(input).unwrap();
        assert!(warnings.iter().any(|w| w.contains("anyOf")));
    }

    #[test]
    fn test_error_on_non_object_root() {
        let input = r#"{ "type": "string" }"#;

        let result = convert_json_schema(input);
        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(err.contains("object"));
    }

    #[test]
    fn test_empty_properties() {
        let input = r#"{
            "type": "object",
            "properties": {}
        }"#;

        let (schema, warnings) = convert_json_schema(input).unwrap();
        assert!(schema.fields.is_empty());
        assert!(warnings.is_empty());
    }

    #[test]
    fn test_is_json_schema_with_dollar_schema() {
        assert!(is_json_schema(
            r#"{"$schema": "http://json-schema.org/draft-07/schema#", "type": "object"}"#
        ));
    }

    #[test]
    fn test_is_json_schema_with_type_and_properties() {
        assert!(is_json_schema(
            r#"{"type": "object", "properties": {"name": {"type": "string"}}}"#
        ));
    }

    #[test]
    fn test_is_not_json_schema_germanic_format() {
        // GERMANIC native format has schema_id + fields, not $schema/properties
        assert!(!is_json_schema(
            r#"{"schema_id": "test.v1", "version": 1, "fields": {}}"#
        ));
    }

    #[test]
    fn test_openclaw_llm_task_compatible() {
        let json_schema = r#"{
            "type": "object",
            "properties": {
                "intent": { "type": "string" },
                "draft": { "type": "string" }
            },
            "required": ["intent", "draft"],
            "additionalProperties": false
        }"#;

        let (schema, warnings) = convert_json_schema(json_schema).unwrap();
        assert!(schema.fields["intent"].required);
        assert!(schema.fields["draft"].required);
        assert_eq!(schema.fields["intent"].field_type, FieldType::String);
        assert_eq!(schema.fields["draft"].field_type, FieldType::String);
        assert!(warnings.is_empty());
    }

    #[test]
    fn test_praxis_as_json_schema_draft7() {
        let json_schema = r#"{
            "$schema": "http://json-schema.org/draft-07/schema#",
            "$id": "de.health.practice.v1",
            "type": "object",
            "required": ["name", "telefon", "adresse"],
            "properties": {
                "name": { "type": "string" },
                "telefon": { "type": "string" },
                "email": { "type": "string" },
                "adresse": {
                    "type": "object",
                    "required": ["strasse", "ort"],
                    "properties": {
                        "strasse": { "type": "string" },
                        "ort": { "type": "string" },
                        "land": { "type": "string", "default": "DE" }
                    }
                },
                "schwerpunkte": {
                    "type": "array",
                    "items": { "type": "string" }
                },
                "kassenpatienten": { "type": "boolean" }
            }
        }"#;

        let (schema, _) = convert_json_schema(json_schema).unwrap();

        // Schema metadata
        assert_eq!(schema.schema_id, "de.health.practice.v1");

        // Required inversion
        assert!(schema.fields["name"].required);
        assert!(schema.fields["telefon"].required);
        assert!(!schema.fields["email"].required);

        // Nested table
        assert_eq!(schema.fields["adresse"].field_type, FieldType::Table);
        let addr = schema.fields["adresse"].fields.as_ref().unwrap();
        assert!(addr["strasse"].required);
        assert!(addr["ort"].required);
        assert_eq!(addr["land"].default, Some("DE".into()));

        // Array
        assert_eq!(
            schema.fields["schwerpunkte"].field_type,
            FieldType::StringArray
        );

        // Bool
        assert_eq!(schema.fields["kassenpatienten"].field_type, FieldType::Bool);
    }

    #[test]
    fn test_all_field_types() {
        let input = r#"{
            "type": "object",
            "properties": {
                "name": { "type": "string" },
                "active": { "type": "boolean" },
                "age": { "type": "integer" },
                "rating": { "type": "number" },
                "tags": { "type": "array", "items": { "type": "string" } },
                "scores": { "type": "array", "items": { "type": "integer" } },
                "address": {
                    "type": "object",
                    "properties": {
                        "city": { "type": "string" }
                    }
                }
            }
        }"#;

        let (schema, warnings) = convert_json_schema(input).unwrap();
        assert!(warnings.is_empty());
        assert_eq!(schema.fields["name"].field_type, FieldType::String);
        assert_eq!(schema.fields["active"].field_type, FieldType::Bool);
        assert_eq!(schema.fields["age"].field_type, FieldType::Int);
        assert_eq!(schema.fields["rating"].field_type, FieldType::Float);
        assert_eq!(schema.fields["tags"].field_type, FieldType::StringArray);
        assert_eq!(schema.fields["scores"].field_type, FieldType::IntArray);
        assert_eq!(schema.fields["address"].field_type, FieldType::Table);
    }

    #[test]
    fn test_warning_on_enum() {
        let input = r#"{
            "type": "object",
            "properties": {
                "status": {
                    "type": "string",
                    "enum": ["active", "inactive"]
                }
            }
        }"#;

        let (schema, warnings) = convert_json_schema(input).unwrap();
        assert_eq!(schema.fields["status"].field_type, FieldType::String);
        assert!(warnings.iter().any(|w| w.contains("enum")));
    }

    #[test]
    fn test_schema_url_detection() {
        // Has $schema but no "type"+"properties" — should still detect
        assert!(is_json_schema(
            r#"{"$schema": "http://json-schema.org/draft-07/schema#"}"#
        ));
    }

    #[test]
    fn test_fallback_schema_id() {
        let input = r#"{
            "type": "object",
            "properties": {
                "x": { "type": "string" }
            }
        }"#;

        let (schema, _) = convert_json_schema(input).unwrap();
        assert_eq!(schema.schema_id, "converted.json-schema.v1");
    }

    #[test]
    fn test_array_without_items() {
        let input = r#"{
            "type": "object",
            "properties": {
                "things": { "type": "array" }
            }
        }"#;

        let (schema, _) = convert_json_schema(input).unwrap();
        // Defaults to string array when items not specified
        assert_eq!(schema.fields["things"].field_type, FieldType::StringArray);
    }

    #[test]
    fn test_warning_on_one_of() {
        let input = r#"{
            "type": "object",
            "properties": {
                "val": { "oneOf": [{"type": "string"}, {"type": "integer"}] }
            }
        }"#;

        let (_, warnings) = convert_json_schema(input).unwrap();
        assert!(warnings.iter().any(|w| w.contains("oneOf")));
    }

    #[test]
    fn test_warning_on_all_of() {
        let input = r#"{
            "type": "object",
            "properties": {
                "val": { "allOf": [{"type": "string"}] }
            }
        }"#;

        let (_, warnings) = convert_json_schema(input).unwrap();
        assert!(warnings.iter().any(|w| w.contains("allOf")));
    }
}