Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions terminusdb_client/client/Client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1850,6 +1850,36 @@ def get_class_frame(self, class_name):
)
return json.loads(_finish_response(result))

def get_json_schema(self, class_name=None):
"""Get the JSON Schema (draft-07) document generated server-side for a class, or for every class in the database. Useful for validating documents client-side and for scaffolding typings (e.g. TypeScript interfaces via json-schema-to-typescript).

Parameters
----------
class_name : str, optional
Name of the class. If omitted, a single schema document covering every class under "definitions" is returned.

Returns
-------
dict
Dictionary containing the JSON Schema document

Examples
--------
>>> client.get_json_schema("Person")
>>> client.get_json_schema()
"""
self._check_connection()
opts = {"format": "jsonschema"}
if class_name is not None:
opts["type"] = class_name
result = self._session.get(
self._class_frame_url(),
headers=self._default_headers,
params=opts,
auth=self._auth(),
)
return json.loads(_finish_response(result))

def commit(self):
"""Not implementated: open transactions currently not suportted. Please check back later."""

Expand Down
21 changes: 21 additions & 0 deletions terminusdb_client/tests/integration_tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,27 @@ def test_class_frame(docker_url):
}


def test_get_json_schema(docker_url):
client = Client(docker_url, user_agent=test_user_agent)
client.connect()
db_name = "jsonschema" + str(random())
client.create_database(db_name)
client.connect(db=db_name)
schema = {"@type": "Class", "@id": "Philosopher", "name": "xsd:string"}
client.insert_document(schema, graph_type=GraphType.SCHEMA)

result = client.get_json_schema("Philosopher")
assert result["$schema"] == "https://json-schema.org/draft-07/schema#"
assert result["type"] == "object"
assert result["properties"]["name"] == {"type": "string"}
assert result["required"] == ["name"]

all_result = client.get_json_schema()
assert all_result["definitions"]["Philosopher"]["properties"]["name"] == {
"type": "string"
}


def test_woql_substr(docker_url):
client = Client(docker_url, user_agent=test_user_agent)
client.connect()
Expand Down
25 changes: 25 additions & 0 deletions terminusdb_client/tests/test_Client.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,31 @@ def test_delete_document(
)


@mock.patch.object(requests.Session, "head", side_effect=mocked_request_success)
@mock.patch.object(requests.Session, "get", side_effect=mocked_request_success)
def test_get_json_schema(mocked_requests, mocked_requests2):
client = Client("http://localhost:6363", user="admin", key="root", team="admin")
client.connect(db="myDBName")

client.get_json_schema("Person")

client._session.get.assert_called_with(
"http://localhost:6363/api/schema/admin/myDBName/local/branch/main",
auth=("admin", "root"),
headers={"user-agent": f"terminusdb-client-python/{__version__}"},
params={"type": "Person", "format": "jsonschema"},
)

client.get_json_schema()

client._session.get.assert_called_with(
"http://localhost:6363/api/schema/admin/myDBName/local/branch/main",
auth=("admin", "root"),
headers={"user-agent": f"terminusdb-client-python/{__version__}"},
params={"format": "jsonschema"},
)


@mock.patch.object(requests.Session, "get", side_effect=mocked_request_success)
@mock.patch.object(requests.Session, "post", side_effect=mocked_request_success)
def test_add_user(mocked_requests, mocked_requests2):
Expand Down
Loading