refactor(registry): model validation as checks
This commit is contained in:
@@ -19,40 +19,31 @@ let
|
||||
unique
|
||||
;
|
||||
|
||||
assertionType = types.submodule {
|
||||
checkType = types.submodule {
|
||||
options = {
|
||||
assertion = mkOption {
|
||||
type = types.bool;
|
||||
type = types.functionTo types.bool;
|
||||
description = ''
|
||||
Whether this registry validation assertion passed.
|
||||
Predicate run against this registry's flattened collected entries.
|
||||
'';
|
||||
};
|
||||
|
||||
message = mkOption {
|
||||
type = types.str;
|
||||
description = ''
|
||||
Error message shown when this registry validation assertion fails.
|
||||
Error message shown when this registry validation check fails.
|
||||
'';
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
validators = {
|
||||
uniqueValue = { value, message }: entries:
|
||||
let
|
||||
values = map value entries;
|
||||
in
|
||||
[
|
||||
{
|
||||
assertion = builtins.length (unique values) == builtins.length values;
|
||||
inherit message;
|
||||
}
|
||||
];
|
||||
predicates = {
|
||||
uniqueValue = value: entries: builtins.length (unique (map value entries)) == builtins.length entries;
|
||||
};
|
||||
|
||||
registryLib = lib // {
|
||||
registry = (lib.registry or { }) // {
|
||||
inherit validators;
|
||||
inherit predicates;
|
||||
};
|
||||
};
|
||||
|
||||
@@ -73,11 +64,11 @@ let
|
||||
'';
|
||||
};
|
||||
|
||||
validate = mkOption {
|
||||
type = types.functionTo (types.listOf assertionType);
|
||||
default = _entries: [ ];
|
||||
checks = mkOption {
|
||||
type = types.listOf checkType;
|
||||
default = [ ];
|
||||
description = ''
|
||||
Validate this registry's flattened collected entries.
|
||||
Validation checks for this registry's flattened collected entries.
|
||||
'';
|
||||
};
|
||||
};
|
||||
@@ -104,7 +95,7 @@ let
|
||||
};
|
||||
in
|
||||
{
|
||||
inherit (evaluated.config) type replicate validate;
|
||||
inherit (evaluated.config) type replicate checks;
|
||||
};
|
||||
|
||||
definitions = mapAttrs' (
|
||||
@@ -169,7 +160,13 @@ let
|
||||
concatLists (
|
||||
mapAttrsToList (
|
||||
registryName: registry:
|
||||
registry.validate (flattenRegistryEntries registryName data.${registryName})
|
||||
let
|
||||
entries = flattenRegistryEntries registryName data.${registryName};
|
||||
in
|
||||
map (check: {
|
||||
assertion = check.assertion entries;
|
||||
inherit (check) message;
|
||||
}) registry.checks
|
||||
) definitions
|
||||
);
|
||||
|
||||
@@ -209,7 +206,7 @@ let
|
||||
};
|
||||
in
|
||||
{
|
||||
inherit definitions validators;
|
||||
inherit definitions predicates;
|
||||
|
||||
module = {
|
||||
options.registry = mapAttrs (_name: registry: registryOption registry) definitions;
|
||||
|
||||
@@ -2,8 +2,10 @@
|
||||
{
|
||||
type = lib.types.str;
|
||||
|
||||
validate = lib.registry.validators.uniqueValue {
|
||||
value = entry: entry.value;
|
||||
message = "Address registry values must be unique.";
|
||||
};
|
||||
checks = [
|
||||
{
|
||||
assertion = lib.registry.predicates.uniqueValue (entry: entry.value);
|
||||
message = "Address registry values must be unique.";
|
||||
}
|
||||
];
|
||||
}
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
{ lib, ... }:
|
||||
{
|
||||
validate = lib.registry.validators.uniqueValue {
|
||||
value = entry: entry.value.domain;
|
||||
message = "Route domains must be unique.";
|
||||
};
|
||||
checks = [
|
||||
{
|
||||
assertion = lib.registry.predicates.uniqueValue (entry: entry.value.domain);
|
||||
message = "Route domains must be unique.";
|
||||
}
|
||||
];
|
||||
|
||||
type = lib.types.submodule {
|
||||
options = {
|
||||
|
||||
@@ -22,9 +22,9 @@ in
|
||||
|
||||
type = lib.types.ints.positive;
|
||||
|
||||
validate = entries: [
|
||||
checks = [
|
||||
{
|
||||
assertion = duplicateUids entries == { };
|
||||
assertion = entries: duplicateUids entries == { };
|
||||
message = "Stable UID allocations must be unique.";
|
||||
}
|
||||
];
|
||||
|
||||
Reference in New Issue
Block a user