lib/jsonschema: add more types and excludes

- Complete the types to cover almost everything
- exclude specific types like functionTo or package
This commit is contained in:
DavHau
2023-11-20 19:40:08 +07:00
parent 3742acda8b
commit 96bd6855e9
2 changed files with 208 additions and 38 deletions

View File

@@ -1,21 +1,27 @@
{ lib ? import <nixpkgs/lib> }: { lib ? import <nixpkgs/lib>
, excludedTypes ? [
"functionTo"
"package"
]
}:
let let
# from nixos type to jsonschema type
typeMap = {
bool = "boolean";
float = "number";
int = "integer";
str = "string";
path = "string"; # TODO add prober path checks
};
# remove _module attribute from options # remove _module attribute from options
clean = opts: builtins.removeAttrs opts [ "_module" ]; clean = opts: builtins.removeAttrs opts [ "_module" ];
# throw error if option type is not supported # throw error if option type is not supported
notSupported = option: throw notSupported = option: lib.trace option throw ''
"option type '${option.type.name}' ('${option.type.description}') not supported by jsonschema converter"; option type '${option.type.name}' ('${option.type.description}') not supported by jsonschema converter
location: ${lib.concatStringsSep "." option.loc}
'';
isExcludedOption = option: (lib.elem (option.type.name or null) excludedTypes);
filterExcluded = lib.filter (opt: ! isExcludedOption opt);
filterExcludedAttrs = lib.filterAttrs (_name: opt: ! isExcludedOption opt);
allBasicTypes =
[ "boolean" "integer" "number" "string" "array" "object" "null" ];
in in
rec { rec {
@@ -32,10 +38,11 @@ rec {
# parses a set of evaluated nixos options to a jsonschema # parses a set of evaluated nixos options to a jsonschema
parseOptions = options': parseOptions = options':
let let
options = clean options'; options = filterExcludedAttrs (clean options');
# parse options to jsonschema properties # parse options to jsonschema properties
properties = lib.mapAttrs (_name: option: parseOption option) options; properties = lib.mapAttrs (_name: option: parseOption option) options;
isRequired = prop: ! (prop ? default || prop.type == "object"); # TODO: figure out how to handle if prop.anyOf is used
isRequired = prop: ! (prop ? default || prop.type or null == "object");
requiredProps = lib.filterAttrs (_: prop: isRequired prop) properties; requiredProps = lib.filterAttrs (_: prop: isRequired prop) properties;
required = lib.optionalAttrs (requiredProps != { }) { required = lib.optionalAttrs (requiredProps != { }) {
required = lib.attrNames requiredProps; required = lib.attrNames requiredProps;
@@ -58,22 +65,45 @@ rec {
}; };
in in
# either type
# TODO: if all nested optiosn are excluded, the parent sould be excluded too
if option.type.name or null == "either"
# return jsonschema property definition for either
then
let
optionsList' = [
{ type = option.type.nestedTypes.left; _type = "option"; loc = option.loc; }
{ type = option.type.nestedTypes.right; _type = "option"; loc = option.loc; }
];
optionsList = filterExcluded optionsList';
in
default // description // {
anyOf = map parseOption optionsList;
}
# handle nested options (not a submodule) # handle nested options (not a submodule)
if ! option ? _type else if ! option ? _type
then parseOptions option then parseOptions option
# throw if not an option # throw if not an option
else if option._type != "option" else if option._type != "option" && option._type != "option-type"
then throw "parseOption: not an option" then throw "parseOption: not an option"
# parse nullOr # parse nullOr
else if option.type.name == "nullOr" else if option.type.name == "nullOr"
# return jsonschema property definition for nullOr # return jsonschema property definition for nullOr
then default // description // { then
type = [ let
"null" nestedOption =
(typeMap.${option.type.functor.wrapped.name} or (notSupported option)) { type = option.type.nestedTypes.elemType; _type = "option"; loc = option.loc; };
]; in
default // description // {
anyOf =
[{ type = "null"; }]
++ (
lib.optional (! isExcludedOption nestedOption)
(parseOption nestedOption)
);
} }
# parse bool # parse bool
@@ -111,6 +141,27 @@ rec {
type = "string"; type = "string";
} }
# parse anything
else if option.type.name == "anything"
# return jsonschema property definition for anything
then default // description // {
type = allBasicTypes;
}
# parse unspecified
else if option.type.name == "unspecified"
# return jsonschema property definition for unspecified
then default // description // {
type = allBasicTypes;
}
# parse raw
else if option.type.name == "raw"
# return jsonschema property definition for raw
then default // description // {
type = allBasicTypes;
}
# parse enum # parse enum
else if option.type.name == "enum" else if option.type.name == "enum"
# return jsonschema property definition for enum # return jsonschema property definition for enum
@@ -127,15 +178,15 @@ rec {
} }
# parse list # parse list
else if else if (option.type.name == "listOf")
(option.type.name == "listOf")
&& (typeMap ? "${option.type.functor.wrapped.name}")
# return jsonschema property definition for list # return jsonschema property definition for list
then default // description // { then
let
nestedOption = { type = option.type.functor.wrapped; _type = "option"; loc = option.loc; };
in
default // description // {
type = "array"; type = "array";
items = { items = parseOption nestedOption;
type = typeMap.${option.type.functor.wrapped.name};
};
} }
# parse list of unspecified # parse list of unspecified
@@ -156,13 +207,27 @@ rec {
} }
# parse attrs # parse attrs
else if option.type.name == "attrsOf" else if option.type.name == "attrs"
# return jsonschema property definition for attrs # return jsonschema property definition for attrs
then default // description // { then default // description // {
type = "object"; type = "object";
additionalProperties = { additionalProperties = true;
type = typeMap.${option.type.nestedTypes.elemType.name} or (notSupported option); }
};
# parse attrsOf
# TODO: if nested option is excluded, the parent sould be excluded too
else if option.type.name == "attrsOf" || option.type.name == "lazyAttrsOf"
# return jsonschema property definition for attrs
then
let
nestedOption = { type = option.type.nestedTypes.elemType; _type = "option"; loc = option.loc; };
in
default // description // {
type = "object";
additionalProperties =
if ! isExcludedOption nestedOption
then parseOption { type = option.type.nestedTypes.elemType; _type = "option"; loc = option.loc; }
else false;
} }
# parse submodule # parse submodule

View File

@@ -115,7 +115,7 @@ in
}; };
}; };
testListOfUnspacified = testListOfUnspecified =
let let
default = [ 1 2 3 ]; default = [ 1 2 3 ];
in in
@@ -123,6 +123,22 @@ in
expr = slib.parseOption (evalType (lib.types.listOf lib.types.unspecified) default); expr = slib.parseOption (evalType (lib.types.listOf lib.types.unspecified) default);
expected = { expected = {
type = "array"; type = "array";
items = {
type = [ "boolean" "integer" "number" "string" "array" "object" "null" ];
};
inherit default description;
};
};
testAttrs =
let
default = { foo = 1; bar = 2; baz = 3; };
in
{
expr = slib.parseOption (evalType (lib.types.attrs) default);
expected = {
type = "object";
additionalProperties = true;
inherit default description; inherit default description;
}; };
}; };
@@ -142,6 +158,21 @@ in
}; };
}; };
testLazyAttrsOfInt =
let
default = { foo = 1; bar = 2; baz = 3; };
in
{
expr = slib.parseOption (evalType (lib.types.lazyAttrsOf lib.types.int) default);
expected = {
type = "object";
additionalProperties = {
type = "integer";
};
inherit default description;
};
};
testNullOrBool = testNullOrBool =
let let
default = null; # null is a valid value for this type default = null; # null is a valid value for this type
@@ -149,7 +180,30 @@ in
{ {
expr = slib.parseOption (evalType (lib.types.nullOr lib.types.bool) default); expr = slib.parseOption (evalType (lib.types.nullOr lib.types.bool) default);
expected = { expected = {
type = [ "null" "boolean" ]; anyOf = [
{ type = "null"; }
{ type = "boolean"; }
];
inherit default description;
};
};
testNullOrNullOr =
let
default = null; # null is a valid value for this type
in
{
expr = slib.parseOption (evalType (lib.types.nullOr (lib.types.nullOr lib.types.bool)) default);
expected = {
anyOf = [
{ type = "null"; }
{
anyOf = [
{ type = "null"; }
{ type = "boolean"; }
];
}
];
inherit default description; inherit default description;
}; };
}; };
@@ -258,4 +312,55 @@ in
inherit default description; inherit default description;
}; };
}; };
testEither =
let
default = "foo";
in
{
expr = slib.parseOption (evalType (lib.types.either lib.types.bool lib.types.str) default);
expected = {
anyOf = [
{ type = "boolean"; }
{ type = "string"; }
];
inherit default description;
};
};
testAnything =
let
default = "foo";
in
{
expr = slib.parseOption (evalType lib.types.anything default);
expected = {
inherit default description;
type = [ "boolean" "integer" "number" "string" "array" "object" "null" ];
};
};
testUnspecified =
let
default = "foo";
in
{
expr = slib.parseOption (evalType lib.types.unspecified default);
expected = {
inherit default description;
type = [ "boolean" "integer" "number" "string" "array" "object" "null" ];
};
};
testRaw =
let
default = "foo";
in
{
expr = slib.parseOption (evalType lib.types.raw default);
expected = {
inherit default description;
type = [ "boolean" "integer" "number" "string" "array" "object" "null" ];
};
};
} }