From 9136e5caa8b86fb70048ce4c00710e9c6318c45d Mon Sep 17 00:00:00 2001 From: alexion Date: Tue, 25 Aug 2026 22:28:25 -0400 Subject: [PATCH] refactor(registry): model validation as checks --- lib/registry.nix | 43 +++++++++++++++++++---------------------- registry/address.nix | 10 ++++++---- registry/route.nix | 10 ++++++---- registry/stable-uid.nix | 4 ++-- 4 files changed, 34 insertions(+), 33 deletions(-) diff --git a/lib/registry.nix b/lib/registry.nix index 22d2dca..ee2cf30 100644 --- a/lib/registry.nix +++ b/lib/registry.nix @@ -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; diff --git a/registry/address.nix b/registry/address.nix index f8d7630..3f4eb65 100644 --- a/registry/address.nix +++ b/registry/address.nix @@ -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."; + } + ]; } diff --git a/registry/route.nix b/registry/route.nix index ffa12c9..bee18a9 100644 --- a/registry/route.nix +++ b/registry/route.nix @@ -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 = { diff --git a/registry/stable-uid.nix b/registry/stable-uid.nix index bb29bd5..584c84a 100644 --- a/registry/stable-uid.nix +++ b/registry/stable-uid.nix @@ -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."; } ];