json-schema-utils 0.8.2__tar.gz → 0.8.4__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (23) hide show
  1. {json_schema_utils-0.8.2 → json_schema_utils-0.8.4}/PKG-INFO +1 -1
  2. {json_schema_utils-0.8.2 → json_schema_utils-0.8.4}/json_schema_utils.egg-info/PKG-INFO +1 -1
  3. {json_schema_utils-0.8.2 → json_schema_utils-0.8.4}/jsutils/convert.py +15 -3
  4. {json_schema_utils-0.8.2 → json_schema_utils-0.8.4}/jsutils/scripts.py +33 -4
  5. {json_schema_utils-0.8.2 → json_schema_utils-0.8.4}/pyproject.toml +1 -1
  6. {json_schema_utils-0.8.2 → json_schema_utils-0.8.4}/.gitignore +0 -0
  7. {json_schema_utils-0.8.2 → json_schema_utils-0.8.4}/LICENSE +0 -0
  8. {json_schema_utils-0.8.2 → json_schema_utils-0.8.4}/MANIFEST.in +0 -0
  9. {json_schema_utils-0.8.2 → json_schema_utils-0.8.4}/Makefile +0 -0
  10. {json_schema_utils-0.8.2 → json_schema_utils-0.8.4}/README.md +0 -0
  11. {json_schema_utils-0.8.2 → json_schema_utils-0.8.4}/json_schema_utils.egg-info/SOURCES.txt +0 -0
  12. {json_schema_utils-0.8.2 → json_schema_utils-0.8.4}/json_schema_utils.egg-info/dependency_links.txt +0 -0
  13. {json_schema_utils-0.8.2 → json_schema_utils-0.8.4}/json_schema_utils.egg-info/entry_points.txt +0 -0
  14. {json_schema_utils-0.8.2 → json_schema_utils-0.8.4}/json_schema_utils.egg-info/requires.txt +0 -0
  15. {json_schema_utils-0.8.2 → json_schema_utils-0.8.4}/json_schema_utils.egg-info/top_level.txt +0 -0
  16. {json_schema_utils-0.8.2 → json_schema_utils-0.8.4}/jsutils/__init__.py +0 -0
  17. {json_schema_utils-0.8.2 → json_schema_utils-0.8.4}/jsutils/inline.py +0 -0
  18. {json_schema_utils-0.8.2 → json_schema_utils-0.8.4}/jsutils/recurse.py +0 -0
  19. {json_schema_utils-0.8.2 → json_schema_utils-0.8.4}/jsutils/schemas.py +0 -0
  20. {json_schema_utils-0.8.2 → json_schema_utils-0.8.4}/jsutils/simplify.py +0 -0
  21. {json_schema_utils-0.8.2 → json_schema_utils-0.8.4}/jsutils/stats.py +0 -0
  22. {json_schema_utils-0.8.2 → json_schema_utils-0.8.4}/jsutils/utils.py +0 -0
  23. {json_schema_utils-0.8.2 → json_schema_utils-0.8.4}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: json_schema_utils
3
- Version: 0.8.2
3
+ Version: 0.8.4
4
4
  Summary: JSON Schema Utils
5
5
  Author: Fabien Coelho, Claire Yannou-Medrala
6
6
  License-Expression: CC0-1.0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: json_schema_utils
3
- Version: 0.8.2
3
+ Version: 0.8.4
4
4
  Summary: JSON Schema Utils
5
5
  Author: Fabien Coelho, Claire Yannou-Medrala
6
6
  License-Expression: CC0-1.0
@@ -10,6 +10,18 @@ type JsonPath = list[str|int]
10
10
  log = logging.getLogger("convert")
11
11
  # log.setLevel(logging.DEBUG)
12
12
 
13
+ def tname(v) -> str:
14
+ return (
15
+ "null" if v is None else
16
+ "bool" if isinstance(v, bool) else
17
+ "int" if isinstance(v, int) else
18
+ "number" if isinstance(v, float) else
19
+ "string" if isinstance(v, str) else
20
+ "array" if isinstance(v, list) else
21
+ "object" if isinstance(v, dict) else
22
+ "UNKNOWN"
23
+ )
24
+
13
25
  #
14
26
  # BEST EFFORT SCHEMA TO MODEL CONVERSION
15
27
  #
@@ -820,15 +832,15 @@ def schema2model(schema, path: JsonPath = [],
820
832
  if "minProperties" in schema and "maxProperties" in schema and \
821
833
  schema["minProperties"] == schema["maxProperties"]:
822
834
  ival = schema["maxProperties"]
823
- assert isinstance(ival, int), f"int # props at [{spath}]"
835
+ assert isinstance(ival, int), f"int # props at [{spath}] ({tname(ival)})"
824
836
  else:
825
837
  if "minProperties" in schema:
826
838
  mini = schema["minProperties"]
827
- assert type(mini) is int, f"int min props at [{spath}]"
839
+ assert type(mini) is int, f"int min props at [{spath}] ({tname(mini)})"
828
840
  constraints[">="] = mini
829
841
  if "maxProperties" in schema:
830
842
  maxi = schema["maxProperties"]
831
- assert type(maxi) is int, f"int max props at [{spath}]"
843
+ assert type(maxi) is int, f"int max props at [{spath}] ({tname(maxi)})"
832
844
  constraints["<="] = maxi
833
845
 
834
846
  if "patternProperties" in schema:
@@ -10,8 +10,8 @@ from importlib.metadata import version as pkg_version
10
10
  logging.basicConfig()
11
11
 
12
12
  from .schemas import Schemas
13
- from .utils import log, JSUError
14
- from .recurse import hasDirectRef
13
+ from .utils import log, JSUError, JsonSchema
14
+ from .recurse import hasDirectRef, recurseSchema
15
15
  from .inline import inlineRefs
16
16
  from .simplify import simplifySchema, scopeDefs
17
17
  from .stats import json_schema_stats, json_metrics, normalize_ods
@@ -169,7 +169,7 @@ def jsu_check():
169
169
  try:
170
170
  if args.engine == "jschon":
171
171
  import jschon
172
- jschon.create_catalog(args.version)
172
+ jschon.create_catalog(args.draft)
173
173
  schema = jschon.JSONSchema(jschema)
174
174
 
175
175
  def check(data):
@@ -342,9 +342,38 @@ ID2MODEL: dict[str, tuple[str, str]] = {
342
342
  "https://spec.openapis.org/oas/3.1/schema/2022-10-07": (
343
343
  f"{JM}/openapi-311.model.json",
344
344
  f"{JM}/openapi-311.model.json", # TODO fuzzy
345
+ ),
346
+ "sha3:58df1e36909f3f8033f4da3e9a6179f3d3e53c51501d7f14a557e34ecef988e1": (
347
+ f"{JM}/cypress.model.json",
348
+ f"{JM}/cypress.model.json",
345
349
  )
346
350
  }
347
351
 
352
+ # generate an id
353
+ def schema2id(schema: JsonSchema) -> str:
354
+
355
+ # copy
356
+ schema = copy.deepcopy(schema)
357
+
358
+ # cleanup non essential stuff
359
+ def nocomment(schema: JsonSchema, _: list[str]) -> bool:
360
+ if isinstance(schema, dict):
361
+ for p in ("$comment", "title", "description", "default",
362
+ "examples", "readOnly", "writeOnly", "deprecated"):
363
+ if p in schema:
364
+ del schema[p]
365
+ return True
366
+ return False
367
+
368
+ recurseSchema(schema, "", flt=nocomment)
369
+
370
+ # hash serialized json
371
+ serial = json.dumps(schema, sort_keys=True)
372
+ shid = "sha3:" + hashlib.sha3_256(serial.encode("UTF-8")).hexdigest()
373
+
374
+ log.info(f"schema id: {shid}")
375
+ return shid
376
+
348
377
  # add ~# versions
349
378
  for k in list(ID2MODEL.keys()):
350
379
  if k.endswith("/schema"):
@@ -385,7 +414,7 @@ def jsu_model():
385
414
  if args.id and isinstance(schema, dict):
386
415
  sid = (schema["$id"] if "$id" in schema else
387
416
  schema["id"] if "id" in schema else
388
- None)
417
+ schema2id(schema))
389
418
  if sid in ID2MODEL:
390
419
  log.info(f"using predefined model for {sid}")
391
420
  model = f"${ID2MODEL[sid][0 if args.strict else 1]}"
@@ -8,7 +8,7 @@ exclude = ["tests*"]
8
8
 
9
9
  [project]
10
10
  name = "json_schema_utils"
11
- version = "0.8.2"
11
+ version = "0.8.4"
12
12
  authors = [ { name = "Fabien Coelho" }, { name = "Claire Yannou-Medrala" } ]
13
13
  description = "JSON Schema Utils"
14
14
  readme = "README.md"