Schema Tools
This example demonstrates how to use schema inference, validation, and merging tools.
1"""
2Schema Tools Example
3===================
4
5This example demonstrates how to use TOON Converter's schema tools:
61. Inference: Generating a schema from data
72. Validation: Checking data against a schema
83. Merging: Combining multiple schemas into a unified one
9"""
10
11import json
12from toonverter import infer_schema, validate_schema
13
14def main():
15 # 1. Infer Schema from Data
16 print("--- 1. Schema Inference ---")
17 user_data = {
18 "id": 123,
19 "username": "jdoe",
20 "is_active": True,
21 "roles": ["admin", "editor"],
22 "metadata": {"last_login": "2023-01-01"}
23 }
24
25 schema = infer_schema(user_data)
26 print("Inferred Schema:")
27 print(json.dumps(schema.to_dict(), indent=2))
28
29 # 2. Validate Data
30 print("\n--- 2. Schema Validation ---")
31 valid_data = {
32 "id": 456,
33 "username": "alice",
34 "is_active": False,
35 "roles": ["viewer"],
36 "metadata": {"last_login": "2023-02-01"}
37 }
38
39 invalid_data = {
40 "id": "not-an-int", # Error: type mismatch
41 "username": "bob",
42 # Missing 'is_active' (required by default)
43 "roles": "not-a-list" # Error: type mismatch
44 }
45
46 errors = validate_schema(valid_data, schema)
47 if not errors:
48 print("valid_data is Valid!")
49
50 errors = validate_schema(invalid_data, schema)
51 if errors:
52 print(f"invalid_data has {len(errors)} errors:")
53 for e in errors:
54 print(f" - {e}")
55
56 # 3. Schema Merging
57 print("\n--- 3. Schema Merging ---")
58 # Schema 1: Object with 'name' (string)
59 data1 = {"name": "Product A", "price": 10}
60 schema1 = infer_schema(data1)
61
62 # Schema 2: Object with 'name' (string) and optional 'tags' (list)
63 # and 'price' is float here
64 data2 = {"name": "Product B", "price": 12.50, "tags": ["sale"]}
65 schema2 = infer_schema(data2)
66
67 # Merge them
68 merged_schema = schema1.merge(schema2)
69
70 print("Merged Schema:")
71 print(json.dumps(merged_schema.to_dict(), indent=2))
72
73 # Notice:
74 # - 'price' should be float (widened from int)
75 # - 'tags' should be present but not required (since it was missing in data1)
76
77if __name__ == "__main__":
78 main()