From 040b719570aea8fc98a6438002c693df6c92f54d Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Thu, 8 Aug 2019 12:47:22 +0100 Subject: feat: Support better error messages for list/attrs element checks --- yants.nix | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/yants.nix b/yants.nix index feb3b8fd3410..03e5114033a6 100644 --- a/yants.nix +++ b/yants.nix @@ -4,17 +4,16 @@ # All types (should) compose as expected. # # TODO: -# - error messages for type-checks of map/list elements are bad # - enums? -{ toPretty ? ((import {}).lib.generators.toPretty) }: +{ toPretty ? ((import {}).lib.generators.toPretty {}) }: # Checks are simply functions of the type `a -> bool` which check # whether `a` conforms to the specification. with builtins; let # Internal utilities: typeError = type: val: - throw "Expected type '${type}', but value '${toPretty {} val}' is of type '${typeOf val}'"; + throw "Expected type '${type}', but value '${toPretty val}' is of type '${typeOf val}'"; typedef = name: check: { inherit name check; @@ -80,9 +79,18 @@ in (typeSet [ # Polymorphic types (poly "option" (t: v: (isNull v) || t.check v)) - (poly "list" (t: v: isList v && (foldl' (s: e: s && (t.check e)) true v))) + + (poly "list" (t: v: isList v && (foldl' (s: e: s && ( + if t.check e then true + else throw "Expected list element of type '${t.name}', but '${toPretty e}' is of type '${typeOf e}'" + )) true v))) + (poly "attrs" (t: v: - isAttrs v && (foldl' (s: e: s && (t.check e)) true (attrValues v)) + isAttrs v && (foldl' (s: e: s && ( + if t.check e then true + else throw "Expected attribute set element of type '${t.name}', but '${toPretty e}' is of type '${typeOf e}'" + )) true (attrValues v)) )) + (poly2 "either" (t1: t2: v: t1.check v || t2.check v)) ]) // { inherit struct; } -- cgit 1.4.1