- 3 CRUD JSON files: customer_balance, accounting_records, sync_state - Build script with model validation, CRUD validation, DDL generation - DDL: db/schema.sql (72 lines, 7 tables) - Scripts: validate_models.py, validate_crud.py, generate_ddl.py
42 lines
1.2 KiB
Python
Executable File
42 lines
1.2 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
"""Validate SageAPI model JSON definitions."""
|
|
import json
|
|
import os
|
|
import sys
|
|
|
|
def validate_models(models_dir='models'):
|
|
errors = 0
|
|
for f in sorted(os.listdir(models_dir)):
|
|
if not f.endswith('.json'):
|
|
continue
|
|
filepath = os.path.join(models_dir, f)
|
|
with open(filepath) as fh:
|
|
data = json.load(fh)
|
|
|
|
for key in ['summary', 'fields', 'idxfields']:
|
|
if key not in data:
|
|
print(f'ERROR: {f} missing "{key}"')
|
|
errors += 1
|
|
|
|
if not data.get('summary') or not isinstance(data['summary'], list):
|
|
print(f'ERROR: {f} summary must be a non-empty list')
|
|
errors += 1
|
|
continue
|
|
|
|
summary = data['summary'][0]
|
|
if 'primary' not in summary:
|
|
print(f'ERROR: {f} summary must have "primary" field')
|
|
errors += 1
|
|
|
|
table_name = summary.get('name', '?')
|
|
primary = summary.get('primary', '?')
|
|
print(f' OK: {f} ({table_name}, primary={primary})')
|
|
|
|
if errors:
|
|
print(f'\n{errors} error(s) found')
|
|
sys.exit(1)
|
|
print('All model definitions valid')
|
|
|
|
if __name__ == '__main__':
|
|
validate_models()
|