Feat(jsonschema): simplify isRequired, look into default and defaultText

This commit is contained in:
Johannes Kirschbauer
2025-06-04 19:53:22 +02:00
parent 13ab73873f
commit dad55040ba
4 changed files with 441 additions and 457 deletions

View File

@@ -96,12 +96,15 @@ rec {
makeModuleInfo = makeModuleInfo =
{ {
path, path,
required,
defaultText ? null, defaultText ? null,
}: ...
}@attrs:
{ {
"$exportedModuleInfo" = "$exportedModuleInfo" =
{ attrs
inherit path; // {
inherit path required;
} }
// lib.optionalAttrs (defaultText != null) { // lib.optionalAttrs (defaultText != null) {
inherit defaultText; inherit defaultText;
@@ -119,13 +122,20 @@ rec {
path ? [ ], path ? [ ],
}: }:
let let
options' = filterInvisibleOpts (filterExcludedAttrs (clean options)); options' = (filterInvisibleOpts (filterExcludedAttrs (clean options)));
# parse options to jsonschema properties # parse options to jsonschema properties
properties = lib.mapAttrs (_name: option: (parseOption' (path ++ [ _name ]) option)) options'; properties = (lib.mapAttrs (_name: option: (parseOption' (path ++ [ _name ]) option)) options');
# TODO: figure out how to handle if prop.anyOf is used # TODO: figure out how to handle if prop.anyOf is used
isRequired = prop: !(prop ? default || prop."$exportedModuleInfo".required or false); isRequired = prop: prop."$exportedModuleInfo".required or true;
requiredProps = lib.filterAttrs (_: prop: isRequired prop) properties; requiredProps = lib.filterAttrs (_: prop: isRequired prop) (properties);
required = lib.optionalAttrs (requiredProps != { }) { required = lib.attrNames requiredProps; };
# json schema spec 6.5.3: required
# The value of this keyword MUST be an array. Elements of this array, if any, MUST be strings, and MUST be unique.
# ...
# Omitting this keyword has the same behavior as an empty array.
required = {
required = lib.attrNames requiredProps;
};
header' = if addHeader then header else { }; header' = if addHeader then header else { };
# freeformType is a special type # freeformType is a special type
@@ -148,15 +158,9 @@ rec {
} }
else else
{ }; { };
# Metadata about the module that is made available to the schema via '$propagatedModuleInfo'
exportedModuleInfo = makeModuleInfo {
inherit path;
};
in in
# return jsonschema # return jsonschema
header' header'
// exportedModuleInfo
// required // required
// { // {
type = "object"; type = "object";
@@ -169,287 +173,277 @@ rec {
parseOption = parseOption' [ ]; parseOption = parseOption' [ ];
parseOption' = parseOption' =
currentPath: option: currentPath: option:
let # lib.trace
default = getDefaultFrom option; (
example = lib.optionalAttrs (option ? example) {
examples =
if (builtins.typeOf option.example) == "list" then option.example else [ option.example ];
};
description = lib.optionalAttrs (option ? description) {
description = option.description.text or option.description;
};
exposedModuleInfo = makeModuleInfo {
path = option.loc;
defaultText = option.defaultText or null;
};
in
# either type
# TODO: if all nested options are excluded, the parent should be excluded too
if
option.type.name or null == "either" || option.type.name or null == "coercedTo"
# return jsonschema property definition for either
then
let let
optionsList' = [ default = (getDefaultFrom (option));
{ example = lib.optionalAttrs (option ? example) {
type = option.type.nestedTypes.left or option.type.nestedTypes.coercedType; examples =
_type = "option"; if (builtins.typeOf option.example) == "list" then option.example else [ option.example ];
loc = option.loc; };
description = lib.optionalAttrs (option ? description) {
description = option.description.text or option.description;
};
exposedModuleInfo = (
makeModuleInfo {
path = option.loc;
defaultText = option.defaultText or null;
required = !(option.defaultText or null != null || option ? default);
default = option.default or null;
} }
{ );
type = option.type.nestedTypes.right or option.type.nestedTypes.finalType; # default =
_type = "option";
loc = option.loc;
}
];
optionsList = filterExcluded optionsList';
in in
exposedModuleInfo // default // example // description // { oneOf = map parseOption optionsList; } # either type
# handle nested options (not a submodule) # TODO: if all nested options are excluded, the parent should be excluded too
# foo.bar = mkOption { type = str; }; if
else if !option ? _type then option.type.name or null == "either" || option.type.name or null == "coercedTo"
(parseOptions option { # return jsonschema property definition for either
addHeader = false; then
path = currentPath; let
}) optionsList' = [
# throw if not an option {
else if option._type != "option" && option._type != "option-type" then type = option.type.nestedTypes.left or option.type.nestedTypes.coercedType;
throw "parseOption: not an option"
# parse nullOr
else if
option.type.name == "nullOr"
# return jsonschema property definition for nullOr
then
let
nestedOption = {
type = option.type.nestedTypes.elemType;
_type = "option";
loc = option.loc;
};
in
default
// exposedModuleInfo
// example
// description
// {
oneOf = [
{ type = "null"; }
] ++ (lib.optional (!isExcludedOption nestedOption) (parseOption nestedOption));
}
# parse bool
else if
option.type.name == "bool"
# return jsonschema property definition for bool
then
exposedModuleInfo // default // example // description // { type = "boolean"; }
# parse float
else if
option.type.name == "float"
# return jsonschema property definition for float
then
exposedModuleInfo // default // example // description // { type = "number"; }
# parse int
else if
(option.type.name == "int" || option.type.name == "positiveInt")
# return jsonschema property definition for int
then
exposedModuleInfo // default // example // description // { type = "integer"; }
# TODO: Add support for intMatching in jsonschema
# parse port type aka. "unsignedInt16"
else if
option.type.name == "unsignedInt16"
|| option.type.name == "unsignedInt"
|| option.type.name == "pkcs11"
|| option.type.name == "intBetween"
then
exposedModuleInfo // default // example // description // { type = "integer"; }
# parse string
# TODO: parse more precise string types
else if
option.type.name == "str"
|| option.type.name == "singleLineStr"
|| option.type.name == "passwdEntry str"
|| option.type.name == "passwdEntry path"
# return jsonschema property definition for string
then
exposedModuleInfo // default // example // description // { type = "string"; }
# TODO: Add support for stringMatching in jsonschema
# parse stringMatching
else if lib.strings.hasPrefix "strMatching" option.type.name then
exposedModuleInfo // default // example // description // { type = "string"; }
# TODO: Add support for separatedString in jsonschema
else if lib.strings.hasPrefix "separatedString" option.type.name then
exposedModuleInfo // default // example // description // { type = "string"; }
# parse string
else if
option.type.name == "path"
# return jsonschema property definition for path
then
exposedModuleInfo // default // example // description // { type = "string"; }
# parse anything
else if
option.type.name == "anything"
# return jsonschema property definition for anything
then
exposedModuleInfo // default // example // description // { type = allBasicTypes; }
# parse unspecified
else if
option.type.name == "unspecified"
# return jsonschema property definition for unspecified
then
exposedModuleInfo // default // example // description // { type = allBasicTypes; }
# parse raw
else if
option.type.name == "raw"
# 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"
# return jsonschema property definition for enum
then
exposedModuleInfo
// default
// example
// description
// {
enum = option.type.functor.payload.values;
}
# parse listOf submodule
else if
option.type.name == "listOf" && option.type.nestedTypes.elemType.name == "submodule"
# return jsonschema property definition for listOf submodule
then
default
// exposedModuleInfo
// example
// description
// {
type = "array";
items = parseSubOptions { inherit option; };
}
# parse list
else if
(option.type.name == "listOf")
# return jsonschema property definition for list
then
let
nestedOption = {
type = option.type.nestedTypes.elemType;
_type = "option";
loc = option.loc;
};
in
default
// exposedModuleInfo
// example
// description
// {
type = "array";
}
// (lib.optionalAttrs (!isExcludedOption nestedOption) { items = parseOption nestedOption; })
# parse list of unspecified
else if
(option.type.name == "listOf") && (option.type.nestedTypes.elemType.name == "unspecified")
# return jsonschema property definition for list
then
exposedModuleInfo // default // example // description // { type = "array"; }
# parse attrsOf submodule
else if
option.type.name == "attrsOf" && option.type.nestedTypes.elemType.name == "submodule"
# return jsonschema property definition for attrsOf submodule
then
default
// exposedModuleInfo
// example
// description
// {
type = "object";
additionalProperties = parseSubOptions {
inherit option;
prefix = [ "<name>" ];
};
}
# parse attrs
else if
option.type.name == "attrs"
# return jsonschema property definition for attrs
then
default
// (lib.recursiveUpdate exposedModuleInfo (
lib.optionalAttrs (!default ? default) {
"$exportedModuleInfo" = {
required = true;
};
}
))
// example
// description
// {
type = "object";
additionalProperties = true;
}
# parse attrsOf
# TODO: if nested option is excluded, the parent should 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
// exposedModuleInfo
// example
// description
// {
type = "object";
additionalProperties =
if !isExcludedOption nestedOption then
parseOption {
type = option.type.nestedTypes.elemType;
_type = "option"; _type = "option";
loc = option.loc; loc = option.loc;
} }
else {
false; type = option.type.nestedTypes.right or option.type.nestedTypes.finalType;
} _type = "option";
# parse submodule loc = option.loc;
else if }
option.type.name == "submodule" ];
# return jsonschema property definition for submodule optionsList = filterExcluded optionsList';
# then (lib.attrNames (option.type.getSubOptions option.loc).opt) in
then exposedModuleInfo // default // example // description // { oneOf = map parseOption optionsList; }
(lib.recursiveUpdate exposedModuleInfo ( # handle nested options (not a submodule)
if (default ? default) then # foo.bar = mkOption { type = str; };
default else if !option ? _type then
else (parseOptions option {
{ addHeader = false;
"$exportedModuleInfo" = { path = currentPath;
required = true; })
}; # throw if not an option
} else if option._type != "option" && option._type != "option-type" then
)) throw "parseOption: not an option"
// example # parse nullOr
// description else if
// parseSubOptions { inherit option; } option.type.name == "nullOr"
# throw error if option type is not supported # return jsonschema property definition for nullOr
else then
notSupported option; let
nestedOption = {
type = option.type.nestedTypes.elemType;
_type = "option";
loc = option.loc;
};
in
default
// exposedModuleInfo
// example
// description
// {
oneOf = [
{ type = "null"; }
] ++ (lib.optional (!isExcludedOption nestedOption) (parseOption nestedOption));
}
# parse bool
else if
option.type.name == "bool"
# return jsonschema property definition for bool
then
exposedModuleInfo // default // example // description // { type = "boolean"; }
# parse float
else if
option.type.name == "float"
# return jsonschema property definition for float
then
exposedModuleInfo // default // example // description // { type = "number"; }
# parse int
else if
(option.type.name == "int" || option.type.name == "positiveInt")
# return jsonschema property definition for int
then
exposedModuleInfo // default // example // description // { type = "integer"; }
# TODO: Add support for intMatching in jsonschema
# parse port type aka. "unsignedInt16"
else if
option.type.name == "unsignedInt16"
|| option.type.name == "unsignedInt"
|| option.type.name == "pkcs11"
|| option.type.name == "intBetween"
then
exposedModuleInfo // default // example // description // { type = "integer"; }
# parse string
# TODO: parse more precise string types
else if
option.type.name == "str"
|| option.type.name == "singleLineStr"
|| option.type.name == "passwdEntry str"
|| option.type.name == "passwdEntry path"
# return jsonschema property definition for string
then
exposedModuleInfo // default // example // description // { type = "string"; }
# TODO: Add support for stringMatching in jsonschema
# parse stringMatching
else if lib.strings.hasPrefix "strMatching" option.type.name then
exposedModuleInfo // default // example // description // { type = "string"; }
# TODO: Add support for separatedString in jsonschema
else if lib.strings.hasPrefix "separatedString" option.type.name then
exposedModuleInfo // default // example // description // { type = "string"; }
# parse string
else if
option.type.name == "path"
# return jsonschema property definition for path
then
exposedModuleInfo // default // example // description // { type = "string"; }
# parse anything
else if
option.type.name == "anything"
# return jsonschema property definition for anything
then
exposedModuleInfo // default // example // description // { type = allBasicTypes; }
# parse unspecified
else if
option.type.name == "unspecified"
# return jsonschema property definition for unspecified
then
exposedModuleInfo // default // example // description // { type = allBasicTypes; }
# parse raw
else if
option.type.name == "raw"
# 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"
# return jsonschema property definition for enum
then
exposedModuleInfo
// default
// example
// description
// {
enum = option.type.functor.payload.values;
}
# parse listOf submodule
else if
option.type.name == "listOf" && option.type.nestedTypes.elemType.name == "submodule"
# return jsonschema property definition for listOf submodule
then
default
// exposedModuleInfo
// example
// description
// {
type = "array";
items = parseSubOptions { inherit option; };
}
# parse list
else if
(option.type.name == "listOf")
# return jsonschema property definition for list
then
let
nestedOption = {
type = option.type.nestedTypes.elemType;
_type = "option";
loc = option.loc;
};
in
default
// exposedModuleInfo
// example
// description
// {
type = "array";
}
// (lib.optionalAttrs (!isExcludedOption nestedOption) { items = parseOption nestedOption; })
# parse list of unspecified
else if
(option.type.name == "listOf") && (option.type.nestedTypes.elemType.name == "unspecified")
# return jsonschema property definition for list
then
exposedModuleInfo // default // example // description // { type = "array"; }
# parse attrsOf submodule
else if
option.type.name == "attrsOf" && option.type.nestedTypes.elemType.name == "submodule"
# return jsonschema property definition for attrsOf submodule
then
default
// exposedModuleInfo
// example
// description
// {
type = "object";
additionalProperties = parseSubOptions {
inherit option;
prefix = [ "<name>" ];
};
}
# parse attrs
else if
option.type.name == "attrs"
# return jsonschema property definition for attrs
then
default
// exposedModuleInfo
// example
// description
// {
type = "object";
additionalProperties = true;
}
# parse attrsOf
# TODO: if nested option is excluded, the parent should 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
// exposedModuleInfo
// example
// description
// {
type = "object";
additionalProperties =
if !isExcludedOption nestedOption then
parseOption {
type = option.type.nestedTypes.elemType;
_type = "option";
loc = option.loc;
}
else
false;
}
# parse submodule
else if
option.type.name == "submodule"
# return jsonschema property definition for submodule
# then (lib.attrNames (option.type.getSubOptions option.loc).opt)
then
default // exposedModuleInfo // example // description // parseSubOptions ({ inherit option; })
# throw error if option type is not supported
else
notSupported option
);
} }

View File

@@ -1,40 +1,33 @@
{ {
"$schema": "http://json-schema.org/draft-07/schema#", "$schema": "http://json-schema.org/draft-07/schema#",
"$exportedModuleInfo": { "path": [] },
"type": "object", "type": "object",
"additionalProperties": false, "additionalProperties": false,
"required": ["services", "userModules"], "required": ["services", "userModules"],
"properties": { "properties": {
"name": { "name": {
"$exportedModuleInfo": { "path": ["name"] },
"type": "string", "type": "string",
"default": "John Doe", "default": "John Doe",
"description": "The name of the user" "description": "The name of the user"
}, },
"age": { "age": {
"$exportedModuleInfo": { "path": ["age"] },
"type": "integer", "type": "integer",
"default": 42, "default": 42,
"description": "The age of the user" "description": "The age of the user"
}, },
"isAdmin": { "isAdmin": {
"$exportedModuleInfo": { "path": ["isAdmin"] },
"type": "boolean", "type": "boolean",
"default": false, "default": false,
"description": "Is the user an admin?" "description": "Is the user an admin?"
}, },
"kernelModules": { "kernelModules": {
"$exportedModuleInfo": { "path": ["kernelModules"] },
"type": "array", "type": "array",
"items": { "items": {
"$exportedModuleInfo": { "path": ["kernelModules"] },
"type": "string" "type": "string"
}, },
"default": ["nvme", "xhci_pci", "ahci"], "default": ["nvme", "xhci_pci", "ahci"],
"description": "A list of enabled kernel modules" "description": "A list of enabled kernel modules"
}, },
"userIds": { "userIds": {
"$exportedModuleInfo": { "path": ["userIds"] },
"type": "object", "type": "object",
"default": { "default": {
"horst": 1, "horst": 1,
@@ -42,23 +35,17 @@
"albrecht": 3 "albrecht": 3
}, },
"additionalProperties": { "additionalProperties": {
"$exportedModuleInfo": { "path": ["userIds"] },
"type": "integer" "type": "integer"
}, },
"description": "Some attributes" "description": "Some attributes"
}, },
"userModules": { "userModules": {
"$exportedModuleInfo": { "path": ["userModules"] },
"type": "object", "type": "object",
"additionalProperties": { "additionalProperties": {
"$exportedModuleInfo": { "path": ["userModules", "<name>"] },
"additionalProperties": false, "additionalProperties": false,
"type": "object", "type": "object",
"properties": { "properties": {
"foo": { "foo": {
"$exportedModuleInfo": {
"path": ["userModules", "<name>", "foo"]
},
"type": [ "type": [
"boolean", "boolean",
"integer", "integer",
@@ -74,55 +61,44 @@
} }
}, },
"colour": { "colour": {
"$exportedModuleInfo": { "path": ["colour"] },
"default": "red", "default": "red",
"description": "The colour of the user", "description": "The colour of the user",
"enum": ["red", "blue", "green"] "enum": ["red", "blue", "green"]
}, },
"services": { "services": {
"$exportedModuleInfo": { "path": ["services"] },
"type": "object", "type": "object",
"additionalProperties": false, "additionalProperties": false,
"properties": { "properties": {
"opt": { "opt": {
"$exportedModuleInfo": { "path": ["services", "opt"] },
"type": "string", "type": "string",
"default": "foo", "default": "foo",
"description": "A submodule option" "description": "A submodule option"
} }
} },
"required": []
}, },
"programs": { "programs": {
"$exportedModuleInfo": { "path": ["programs"] },
"type": "object", "type": "object",
"additionalProperties": false, "additionalProperties": false,
"properties": { "properties": {
"opt": { "opt": {
"$exportedModuleInfo": { "path": ["programs", "opt"] },
"type": "string", "type": "string",
"default": "bar", "default": "bar",
"description": "Another submodule option" "description": "Another submodule option"
} }
}, },
"required": [],
"default": {} "default": {}
}, },
"destinations": { "destinations": {
"$exportedModuleInfo": { "path": ["destinations"] },
"additionalProperties": { "additionalProperties": {
"$exportedModuleInfo": { "path": ["destinations", "<name>"] },
"properties": { "properties": {
"name": { "name": {
"$exportedModuleInfo": {
"path": ["destinations", "<name>", "name"]
},
"default": "name", "default": "name",
"description": "the name of the backup job", "description": "the name of the backup job",
"type": "string" "type": "string"
}, },
"repo": { "repo": {
"$exportedModuleInfo": {
"path": ["destinations", "<name>", "repo"]
},
"description": "the borgbackup repository to backup to", "description": "the borgbackup repository to backup to",
"type": "string" "type": "string"
} }

View File

@@ -7,29 +7,31 @@
let let
description = "Test Description"; description = "Test Description";
# Wrap the parseOption function to reduce the surface that needs to be migrated, when '$exportedModuleInfo' changes
parseOption = opt: filterSchema (slib.parseOption opt);
evalType = evalType =
type: default: type: default:
(evalModuleOptions {
options.opt = lib.mkOption {
inherit type description;
default = default;
};
}).opt;
evalModuleOptions =
module:
let let
evaledConfig = lib.evalModules { evaledConfig = lib.evalModules {
modules = [ modules = [
{ module
options.opt = lib.mkOption {
inherit type;
inherit default;
inherit description;
};
}
]; ];
}; };
in in
evaledConfig.options.opt; evaledConfig.options;
# All options should have the same path filterSchema =
commonModuleInfo = { schema: lib.filterAttrsRecursive (name: _value: name != "$exportedModuleInfo") schema;
"$exportedModuleInfo" = {
path = [ "opt" ];
};
};
in in
{ {
testNoDefaultNoDescription = testNoDefaultNoDescription =
@@ -39,8 +41,8 @@ in
}; };
in in
{ {
expr = slib.parseOption evaledConfig.options.opt; expr = parseOption evaledConfig.options.opt;
expected = commonModuleInfo // { expected = {
type = "boolean"; type = "boolean";
}; };
}; };
@@ -62,8 +64,8 @@ in
}; };
in in
{ {
expr = slib.parseOption evaledConfig.options.opt; expr = parseOption evaledConfig.options.opt;
expected = commonModuleInfo // { expected = {
type = "boolean"; type = "boolean";
inherit description; inherit description;
}; };
@@ -74,8 +76,8 @@ in
default = false; default = false;
in in
{ {
expr = slib.parseOption (evalType lib.types.bool default); expr = parseOption (evalType lib.types.bool default);
expected = commonModuleInfo // { expected = {
type = "boolean"; type = "boolean";
inherit default description; inherit default description;
}; };
@@ -86,8 +88,8 @@ in
default = "hello"; default = "hello";
in in
{ {
expr = slib.parseOption (evalType lib.types.str default); expr = parseOption (evalType lib.types.str default);
expected = commonModuleInfo // { expected = {
type = "string"; type = "string";
inherit default description; inherit default description;
}; };
@@ -98,8 +100,8 @@ in
default = 42; default = 42;
in in
{ {
expr = slib.parseOption (evalType lib.types.int default); expr = parseOption (evalType lib.types.int default);
expected = commonModuleInfo // { expected = {
type = "integer"; type = "integer";
inherit default description; inherit default description;
}; };
@@ -110,8 +112,8 @@ in
default = 42.42; default = 42.42;
in in
{ {
expr = slib.parseOption (evalType lib.types.float default); expr = parseOption (evalType lib.types.float default);
expected = commonModuleInfo // { expected = {
type = "number"; type = "number";
inherit default description; inherit default description;
}; };
@@ -127,8 +129,8 @@ in
]; ];
in in
{ {
expr = slib.parseOption (evalType (lib.types.enum values) default); expr = parseOption (evalType (lib.types.enum values) default);
expected = commonModuleInfo // { expected = {
enum = values; enum = values;
inherit default description; inherit default description;
}; };
@@ -143,14 +145,11 @@ in
]; ];
in in
{ {
expr = slib.parseOption (evalType (lib.types.listOf lib.types.int) default); expr = parseOption (evalType (lib.types.listOf lib.types.int) default);
expected = commonModuleInfo // { expected = {
type = "array"; type = "array";
items = { items = {
type = "integer"; type = "integer";
"$exportedModuleInfo" = {
path = [ "opt" ];
};
}; };
inherit default description; inherit default description;
}; };
@@ -165,13 +164,10 @@ in
]; ];
in in
{ {
expr = slib.parseOption (evalType (lib.types.listOf lib.types.unspecified) default); expr = parseOption (evalType (lib.types.listOf lib.types.unspecified) default);
expected = commonModuleInfo // { expected = {
type = "array"; type = "array";
items = { items = {
"$exportedModuleInfo" = {
path = [ "opt" ];
};
type = [ type = [
"boolean" "boolean"
"integer" "integer"
@@ -195,8 +191,8 @@ in
}; };
in in
{ {
expr = slib.parseOption (evalType (lib.types.attrs) default); expr = parseOption (evalType (lib.types.attrs) default);
expected = commonModuleInfo // { expected = {
type = "object"; type = "object";
additionalProperties = true; additionalProperties = true;
inherit default description; inherit default description;
@@ -212,13 +208,11 @@ in
}; };
in in
{ {
expr = slib.parseOption (evalType (lib.types.attrsOf lib.types.int) default); expr = parseOption (evalType (lib.types.attrsOf lib.types.int) default);
expected = commonModuleInfo // { expected = {
type = "object"; type = "object";
additionalProperties = { additionalProperties = {
"$exportedModuleInfo" = {
path = [ "opt" ];
};
type = "integer"; type = "integer";
}; };
inherit default description; inherit default description;
@@ -234,13 +228,11 @@ in
}; };
in in
{ {
expr = slib.parseOption (evalType (lib.types.lazyAttrsOf lib.types.int) default); expr = parseOption (evalType (lib.types.lazyAttrsOf lib.types.int) default);
expected = commonModuleInfo // { expected = {
type = "object"; type = "object";
additionalProperties = { additionalProperties = {
"$exportedModuleInfo" = {
path = [ "opt" ];
};
type = "integer"; type = "integer";
}; };
inherit default description; inherit default description;
@@ -252,14 +244,17 @@ in
default = null; # null is a valid value for this type default = null; # null is a valid value for this type
in in
{ {
expr = slib.parseOption (evalType (lib.types.nullOr lib.types.bool) default); expr = parseOption (evalType (lib.types.nullOr lib.types.bool) default);
expected = commonModuleInfo // { expected = {
oneOf = [ oneOf = [
{ type = "null"; } { type = "null"; }
{ {
type = "boolean"; type = "boolean";
"$exportedModuleInfo" = { "$exportedModuleInfo" = {
path = [ "opt" ]; path = [ "opt" ];
default = null;
defaultText = null;
required = true;
}; };
} }
]; ];
@@ -272,19 +267,25 @@ in
default = null; # null is a valid value for this type default = null; # null is a valid value for this type
in in
{ {
expr = slib.parseOption (evalType (lib.types.nullOr (lib.types.nullOr lib.types.bool)) default); expr = parseOption (evalType (lib.types.nullOr (lib.types.nullOr lib.types.bool)) default);
expected = commonModuleInfo // { expected = {
oneOf = [ oneOf = [
{ type = "null"; } { type = "null"; }
{ {
"$exportedModuleInfo" = { "$exportedModuleInfo" = {
default = null;
defaultText = null;
path = [ "opt" ]; path = [ "opt" ];
required = true;
}; };
oneOf = [ oneOf = [
{ type = "null"; } { type = "null"; }
{ {
"$exportedModuleInfo" = { "$exportedModuleInfo" = {
default = null;
defaultText = null;
path = [ "opt" ]; path = [ "opt" ];
required = true;
}; };
type = "boolean"; type = "boolean";
} }
@@ -306,8 +307,8 @@ in
}; };
in in
{ {
expr = slib.parseOption (evalType (lib.types.submodule subModule) { }); expr = parseOption (evalType (lib.types.submodule subModule) { });
expected = commonModuleInfo // { expected = {
type = "object"; type = "object";
additionalProperties = false; additionalProperties = false;
description = "Test Description"; description = "Test Description";
@@ -316,14 +317,9 @@ in
type = "boolean"; type = "boolean";
default = true; default = true;
inherit description; inherit description;
"$exportedModuleInfo" = {
path = [
"opt"
"opt"
];
};
}; };
}; };
required = [ ];
default = { }; default = { };
}; };
}; };
@@ -331,32 +327,28 @@ in
testSubmoduleOptionWithoutDefault = testSubmoduleOptionWithoutDefault =
let let
subModule = { subModule = {
options.opt = lib.mkOption { options.foo = lib.mkOption {
type = lib.types.bool; type = lib.types.bool;
inherit description; inherit description;
}; };
}; };
opt = evalType (lib.types.submodule subModule) { };
in in
{ {
expr = slib.parseOption (evalType (lib.types.submodule subModule) { }); inherit opt;
expected = commonModuleInfo // { expr = parseOption (opt);
expected = {
type = "object"; type = "object";
additionalProperties = false; additionalProperties = false;
description = "Test Description"; description = "Test Description";
properties = { properties = {
opt = { foo = {
type = "boolean"; type = "boolean";
inherit description; inherit description;
"$exportedModuleInfo" = {
path = [
"opt"
"opt"
];
};
}; };
}; };
default = { }; default = { };
required = [ "opt" ]; required = [ "foo" ];
}; };
}; };
@@ -375,32 +367,21 @@ in
}; };
in in
{ {
expr = slib.parseOption (evalType (lib.types.attrsOf (lib.types.submodule subModule)) default); expr = parseOption (evalType (lib.types.attrsOf (lib.types.submodule subModule)) default);
expected = commonModuleInfo // { expected = {
type = "object"; type = "object";
additionalProperties = { additionalProperties = {
"$exportedModuleInfo" = {
path = [
"opt"
"<name>"
];
};
type = "object"; type = "object";
additionalProperties = false; additionalProperties = false;
properties = { properties = {
opt = { opt = {
"$exportedModuleInfo" = {
path = [
"opt"
"<name>"
"opt"
];
};
type = "boolean"; type = "boolean";
default = true; default = true;
inherit description; inherit description;
}; };
}; };
required = [ ];
}; };
inherit default description; inherit default description;
}; };
@@ -421,13 +402,10 @@ in
]; ];
in in
{ {
expr = slib.parseOption (evalType (lib.types.listOf (lib.types.submodule subModule)) default); expr = parseOption (evalType (lib.types.listOf (lib.types.submodule subModule)) default);
expected = commonModuleInfo // { expected = {
type = "array"; type = "array";
items = { items = {
"$exportedModuleInfo" = {
path = [ "opt" ];
};
type = "object"; type = "object";
additionalProperties = false; additionalProperties = false;
properties = { properties = {
@@ -435,15 +413,9 @@ in
type = "boolean"; type = "boolean";
default = true; default = true;
inherit description; inherit description;
"$exportedModuleInfo" = {
path = [
"opt"
"*"
"opt"
];
};
}; };
}; };
required = [ ];
}; };
inherit default description; inherit default description;
}; };
@@ -454,18 +426,24 @@ in
default = "foo"; default = "foo";
in in
{ {
expr = slib.parseOption (evalType (lib.types.either lib.types.bool lib.types.str) default); expr = parseOption (evalType (lib.types.either lib.types.bool lib.types.str) default);
expected = commonModuleInfo // { expected = {
oneOf = [ oneOf = [
{ {
"$exportedModuleInfo" = { "$exportedModuleInfo" = {
path = [ "opt" ]; path = [ "opt" ];
default = null;
defaultText = null;
required = true;
}; };
type = "boolean"; type = "boolean";
} }
{ {
"$exportedModuleInfo" = { "$exportedModuleInfo" = {
path = [ "opt" ]; path = [ "opt" ];
default = null;
defaultText = null;
required = true;
}; };
type = "string"; type = "string";
} }
@@ -479,8 +457,8 @@ in
default = "foo"; default = "foo";
in in
{ {
expr = slib.parseOption (evalType lib.types.anything default); expr = parseOption (evalType lib.types.anything default);
expected = commonModuleInfo // { expected = {
inherit default description; inherit default description;
type = [ type = [
"boolean" "boolean"
@@ -499,8 +477,8 @@ in
default = "foo"; default = "foo";
in in
{ {
expr = slib.parseOption (evalType lib.types.unspecified default); expr = parseOption (evalType lib.types.unspecified default);
expected = commonModuleInfo // { expected = {
inherit default description; inherit default description;
type = [ type = [
"boolean" "boolean"
@@ -519,8 +497,8 @@ in
default = "foo"; default = "foo";
in in
{ {
expr = slib.parseOption (evalType lib.types.raw default); expr = parseOption (evalType lib.types.raw default);
expected = commonModuleInfo // { expected = {
inherit default description; inherit default description;
type = [ type = [
"boolean" "boolean"
@@ -533,4 +511,61 @@ in
]; ];
}; };
}; };
test_option_with_default_text = {
expr = (
parseOption (evalModuleOptions {
options.opt = lib.mkOption {
type = lib.types.bool;
defaultText = "This option is a optional, but we cannot assign a default value to it yet.";
};
})
);
expected = {
additionalProperties = false;
properties = {
opt = {
type = "boolean";
};
};
# opt is not required, because it has a defaultText
required = [ ];
type = "object";
};
};
test_nested_option_with_default_text = {
expr = (
parseOption (evalModuleOptions {
options.opt = lib.mkOption {
type = lib.types.submodule {
options = {
foo = lib.mkOption {
type = lib.types.bool;
defaultText = "Not required";
};
};
};
defaultText = "Not required";
};
})
);
expected = {
additionalProperties = false;
properties = {
opt = {
additionalProperties = false;
properties = {
foo = {
type = "boolean";
};
};
required = [ ];
type = "object";
};
};
required = [ ];
type = "object";
};
};
} }

View File

@@ -4,9 +4,13 @@
lib ? (import <nixpkgs> { }).lib, lib ? (import <nixpkgs> { }).lib,
slib ? (import ./. { inherit lib; } { }), slib ? (import ./. { inherit lib; } { }),
}: }:
let
filterSchema =
schema: lib.filterAttrsRecursive (name: _value: name != "$exportedModuleInfo") schema;
in
{ {
testParseOptions = { testParseOptions = {
expr = slib.parseModule ./example-interface.nix; expr = filterSchema (slib.parseModule ./example-interface.nix);
expected = builtins.fromJSON (builtins.readFile ./example-schema.json); expected = builtins.fromJSON (builtins.readFile ./example-schema.json);
}; };
@@ -27,36 +31,18 @@
}; };
in in
{ {
expr = slib.parseOptions evaled.options { }; expr = filterSchema (slib.parseOptions evaled.options { });
expected = { expected = {
"$schema" = "http://json-schema.org/draft-07/schema#"; "$schema" = "http://json-schema.org/draft-07/schema#";
"$exportedModuleInfo" = {
path = [ ];
};
additionalProperties = false; additionalProperties = false;
properties = { properties = {
foo = { foo = {
"$exportedModuleInfo" = {
path = [ "foo" ];
};
additionalProperties = false; additionalProperties = false;
properties = { properties = {
bar = { bar = {
"$exportedModuleInfo" = {
path = [
"foo"
"bar"
];
};
type = "boolean"; type = "boolean";
}; };
baz = { baz = {
"$exportedModuleInfo" = {
path = [
"foo"
"baz"
];
};
type = "boolean"; type = "boolean";
default = false; default = false;
}; };
@@ -78,7 +64,7 @@
}; };
in in
{ {
expr = expr = filterSchema (
slib.parseOptions slib.parseOptions
(lib.evalModules { (lib.evalModules {
modules = [ modules = [
@@ -91,29 +77,22 @@
default default
]; ];
}).options }).options
{ }; { }
);
expected = { expected = {
"$schema" = "http://json-schema.org/draft-07/schema#"; "$schema" = "http://json-schema.org/draft-07/schema#";
"$exportedModuleInfo" = {
path = [ ];
};
additionalProperties = { additionalProperties = {
"$exportedModuleInfo" = {
path = [ ];
};
type = "integer"; type = "integer";
}; };
properties = { properties = {
enable = { enable = {
"$exportedModuleInfo" = {
path = [ "enable" ];
};
default = false; default = false;
description = "Whether to enable enable this."; description = "Whether to enable enable this.";
examples = [ true ]; examples = [ true ];
type = "boolean"; type = "boolean";
}; };
}; };
required = [ ];
type = "object"; type = "object";
}; };
}; };