summary refs log tree commit diff
path: root/lib
diff options
context:
space:
mode:
authorProfpatsch <mail@profpatsch.de>2023-10-18 16:50:59 +0200
committerProfpatsch <mail@profpatsch.de>2023-11-09 17:27:20 +0100
commit2099ebdf593da8babffb96f3909fb8568ce483fb (patch)
tree31768322fc97575b445790c42af410a3aeed4da0 /lib
parent811db8c616bd17836fdbd99b0e62f20fa0efde8c (diff)
downloadnixpkgs-2099ebdf593da8babffb96f3909fb8568ce483fb.tar
nixpkgs-2099ebdf593da8babffb96f3909fb8568ce483fb.tar.gz
nixpkgs-2099ebdf593da8babffb96f3909fb8568ce483fb.tar.bz2
nixpkgs-2099ebdf593da8babffb96f3909fb8568ce483fb.tar.lz
nixpkgs-2099ebdf593da8babffb96f3909fb8568ce483fb.tar.xz
nixpkgs-2099ebdf593da8babffb96f3909fb8568ce483fb.tar.zst
nixpkgs-2099ebdf593da8babffb96f3909fb8568ce483fb.zip
lib: add asserts.assertEachOneOf
Along the lines of `assertOneOf`, but expects a list of values to be
compared. This gives a good error message and is useful for lists of
values, like `supportedGhcVersions` in the arguments of
`haskell-language-server`.
Diffstat (limited to 'lib')
-rw-r--r--lib/asserts.nix29
1 files changed, 29 insertions, 0 deletions
diff --git a/lib/asserts.nix b/lib/asserts.nix
index 98e0b490acf..8d0a621f4c1 100644
--- a/lib/asserts.nix
+++ b/lib/asserts.nix
@@ -50,4 +50,33 @@ rec {
       lib.generators.toPretty {} xs}, but is: ${
         lib.generators.toPretty {} val}";
 
+  /* Specialized `assertMsg` for checking if every one of `vals` is one of the elements
+     of the list `xs`. Useful for checking lists of supported attributes.
+
+     Example:
+       let sslLibraries = [ "libressl" "bearssl" ];
+       in assertEachOneOf "sslLibraries" sslLibraries [ "openssl" "bearssl" ]
+       stderr> error: each element in sslLibraries must be one of [
+       stderr>   "openssl"
+       stderr>   "bearssl"
+       stderr> ], but is: [
+       stderr>   "libressl"
+       stderr>   "bearssl"
+       stderr> ]
+
+     Type:
+       assertEachOneOf :: String -> List ComparableVal -> List ComparableVal -> Bool
+  */
+  assertEachOneOf =
+    # The name of the variable the user entered `val` into, for inclusion in the error message
+    name:
+    # The list of values of what the user provided, to be compared against the values in `xs`
+    vals:
+    # The list of valid values
+    xs:
+    assertMsg
+    (lib.all (val: lib.elem val xs) vals)
+    "each element in ${name} must be one of ${
+      lib.generators.toPretty {} xs}, but is: ${
+        lib.generators.toPretty {} vals}";
 }