Feat(jsonschema): convert deferredModule to unknown

This commit is contained in:
Johannes Kirschbauer
2025-05-20 15:48:28 +02:00
parent f721f4fa9f
commit 7a0665da6d
2 changed files with 39 additions and 0 deletions

View File

@@ -304,6 +304,17 @@ rec {
# return jsonschema property definition for raw
then
exposedModuleInfo // default // example // description // { type = allBasicTypes; }
else if
# This is a special case for the deferred clan.service 'settings', we assume it is JSON serializable
# To get the type of a Deferred modules we need to know the interface of the place where it is evaluated.
# i.e. in case of a clan.service this is the interface of the service which dynamically changes depending on the service
# We assign "type" = []
# This means any value is valid — or like TypeScripts unknown.
# We can assign the type later, when we know the exact interface.
# tsType = "unknown" is a type that we preload for json2ts, such that it gets the correct type in typescript
(option.type.name == "deferredModule")
then
exposedModuleInfo // default // example // description // { tsType = "unknown"; }
# parse enum
else if
option.type.name == "enum"

View File

@@ -294,3 +294,31 @@ def test_enum_roundtrip() -> None:
assert from_dict(Person, data2) == expected2
assert dataclass_to_dict(expected2) == data2
# for the test below
# we would import this from the nix_models
class Unknown:
pass
def test_unknown_deserialize() -> None:
@dataclass
class Person:
name: Unknown
data = {"name": ["a", "b"]}
person = from_dict(Person, data)
person.name = ["a", "b"]
def test_unknown_serialize() -> None:
@dataclass
class Person:
name: Unknown
data = Person(["a", "b"]) # type: ignore
person = dataclass_to_dict(data)
assert person == {"name": ["a", "b"]}