Skip to content

Commit

Permalink
move loosen_schema() to nerdm.utils
Browse files Browse the repository at this point in the history
  • Loading branch information
RayPlante committed Nov 16, 2023
1 parent 9aa1ac9 commit 3515947
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
34 changes: 34 additions & 0 deletions python/nistoar/nerdm/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,12 @@ def declutter_schema(schema: Mapping, post2020: bool=False):
for defname in schema[deftag]:
declutter_schema(schema[deftag][defname])

for seq in "allOf anyOf oneOf".split():
if seq in schema:
for itm in schema[seq]:
declutter_schema(itm)


def unrequire_props_in(schema: Mapping, locations: Union[str, List[str]], post2020: bool=False):
"""
remove ``"required"`` fields at the specified locations from within the given JSON Schema.
Expand Down Expand Up @@ -231,3 +237,31 @@ def unrequire_props_in(schema: Mapping, locations: Union[str, List[str]], post20
unrequire_props_in(itm, "$", post2020)


def loosen_schema(schema: Mapping, directives: Mapping, opts=None):
"""
apply the given loosening directive to the given JSON Schema. The directives is a
dictionary describes what to do with the following properties (the directives) supported:
``derequire``
a list of type definitions within the schema from which the required property
should be removed (via :py:func:`~nistoar.nerdm.utils.unrequire_props_in`). Each
type name listed will be assumed to be an item under the "definitions" node in the
schema this directive is applied to.
``dedocument``
a boolean indicating whether the documentation annotations should be removed from
the schema. If not set, the default is determined by opts.dedoc if opts is given or
True, otherwise.
:param dict schema: the schema document as a JSON Schema schema dictionary
:param dict directives: the dictionary of directives to apply
:param opt: an options object (containing scripts command-line options)
"""
if directives.get("dedocument", True):
declutter_schema(schema)

p2020 = directives.get("post2020")
deftag = "$defs" if p2020 else "definitions"

dereqtps = [ deftag+'.'+t for t in directives.get("derequire", []) ]
unrequire_props_in(schema, dereqtps, p2020)

41 changes: 41 additions & 0 deletions python/tests/nistoar/nerdm/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os, sys, pdb, shutil, logging, json
import unittest as test
from pathlib import Path
from collections import OrderedDict

from nistoar.nerdm import utils
from nistoar.nerdm import constants as const
Expand Down Expand Up @@ -189,6 +190,46 @@ def test_unrequire_props_in(self):
self.assertTrue(not utils.hget(schema, "definitions.Topic.required"))
self.assertTrue(not utils.hget(schema, "definitions.Organization.required"))

def test_loosen_schema(self):
with open(schemadir/"nerdm-schema.json") as fd:
schema = json.load(fd, object_pairs_hook=OrderedDict)

self.assertTrue(utils.hget(schema, "title"))
self.assertTrue(utils.hget(schema, "description"))
self.assertTrue(utils.hget(schema, "definitions.Resource.required"))
self.assertTrue(utils.hget(schema, "definitions.Resource.description"))
self.assertTrue(utils.hget(schema, "definitions.Organization.required"))
self.assertTrue(utils.hget(schema, "definitions.Organization.description"))

utils.loosen_schema(schema, {"derequire": ["Resource"], "dedocument": True})

self.assertTrue(not utils.hget(schema, "title"))
self.assertTrue(not utils.hget(schema, "description"))
self.assertTrue(not utils.hget(schema, "definitions.Resource.required"))
self.assertTrue(not utils.hget(schema, "definitions.Resource.description"))
self.assertTrue(utils.hget(schema, "definitions.Organization.required"))
self.assertTrue(not utils.hget(schema, "definitions.Organization.description"))

def test_loosen_schema_no_dedoc(self):
with open(schemadir/"nerdm-schema.json") as fd:
schema = json.load(fd, object_pairs_hook=OrderedDict)

self.assertTrue(utils.hget(schema, "title"))
self.assertTrue(utils.hget(schema, "description"))
self.assertTrue(utils.hget(schema, "definitions.Resource.required"))
self.assertTrue(utils.hget(schema, "definitions.Resource.description"))
self.assertTrue(utils.hget(schema, "definitions.Organization.required"))
self.assertTrue(utils.hget(schema, "definitions.Organization.description"))

utils.loosen_schema(schema, {"derequire": ["Resource"], "dedocument": False})

self.assertTrue(utils.hget(schema, "title"))
self.assertTrue(utils.hget(schema, "description"))
self.assertTrue(not utils.hget(schema, "definitions.Resource.required"))
self.assertTrue(utils.hget(schema, "definitions.Resource.description"))
self.assertTrue(utils.hget(schema, "definitions.Organization.required"))
self.assertTrue(utils.hget(schema, "definitions.Organization.description"))



class TestVersion(test.TestCase):
Expand Down

0 comments on commit 3515947

Please sign in to comment.