]> ToastFreeware Gitweb - philipp/winterrodeln/wrpylib.git/commitdiff
Implement JSON validation after edit.
authorPhilipp Spitzer <philipp@spitzer.priv.at>
Sun, 28 Nov 2021 10:44:35 +0000 (11:44 +0100)
committerPhilipp Spitzer <philipp@spitzer.priv.at>
Sun, 28 Nov 2021 10:44:35 +0000 (11:44 +0100)
bots/json_edit.py

index bcd0eae68168662a13b4992485689f48a79c4bdf..648b1894a0e622c35b0088278c5de7f3062871fa 100644 (file)
@@ -12,6 +12,7 @@ The following generators and filters are supported:
 import json
 
 import pywikibot
+from jsonschema import validate
 from pywikibot import pagegenerators, Page
 from pywikibot.bot import (
     AutomaticTWSummaryBot,
@@ -21,6 +22,7 @@ from pywikibot.bot import (
     SingleSiteBot,
 )
 
+from wrpylib.json_tools import order_json_keys
 from wrpylib.wrmwmarkup import create_sledrun_wiki
 
 
@@ -34,13 +36,25 @@ class SledrunFromJsonBot(
     NoRedirectPageBot,
     AutomaticTWSummaryBot,
 ):
+    def setup(self) -> None:
+        schema = Page(self.site, 'Winterrodeln:Datenschema/Rodelbahn/V1.json')
+        assert schema.content_model == 'json'
+        self.sledrun_schema = json.loads(schema.text)
+
     def treat_page(self) -> None:
         """Load the given page, do some changes, and save it."""
         if self.current_page.content_model != 'json':
             return
         content_json = json.loads(self.current_page.text)
+
+        # *here*, content_json can be processed
         processed_json = content_json
-        text = json.dumps(processed_json, ensure_ascii=False, indent=4)
+
+        validate(instance=processed_json, schema=self.sledrun_schema)
+        processed_json_ordered = order_json_keys(processed_json, self.sledrun_schema)
+        assert processed_json_ordered == processed_json
+        text = json.dumps(processed_json_ordered, ensure_ascii=False, indent=4)
+
         summary = 'JSON Daten aktualisiert.'
         self.put_current(text, summary=summary)