From 3c50efa7826ebaae8772ba6b9a22449aa6e48813 Mon Sep 17 00:00:00 2001 From: James R Swift Date: Fri, 7 Jun 2024 20:10:09 +0100 Subject: [PATCH] bug(tuple): Error properly on tuple length mismatch --- CHANGELOG.md | 1 + src/types/tuple.typ | 8 ++++++++ 2 files changed, 9 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index cef3cbd..481753e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ - Added schema generator for argument sinks. Takes `positional` and `named` as optional parameters that take schema types. Absence of one of these parameters indicate that these must also be absent from the argument type being validated. - **(Potentially Breaking)** Content now accepts `symbol` as a valid input type by default (see #20) - **(Potentially Breaking)** If the tested value is `auto`, parsing no longer fails. If `default` is set, it takes the default value. If `optional` is set but not `default`, value is parsed as `none`. +- **(Potentially Breaking)** `tuple` has a new parameter `exact` which defaults to true, whereby the length of a tuple must match exactly to be valid. --- diff --git a/src/types/tuple.typ b/src/types/tuple.typ index 36ddc24..151c3f7 100644 --- a/src/types/tuple.typ +++ b/src/types/tuple.typ @@ -5,9 +5,11 @@ /// Valkyrie schema generator for an array type with positional type reqruiements. If all entries /// have the same type, see @@array. +/// exact (bool): Requires a tuple to match in length /// /// -> schema #let tuple( + exact: true, ..args, ) = { assert-base-type-array(args.pos()) @@ -17,8 +19,14 @@ types: (type(()),), ..args.named(), ) + ( + tuple-exact: exact, tuple-schema: args.pos(), handle-descendents: (self, it, ctx: z-ctx(), scope: ()) => { + if (self.tuple-exact and self.tuple-schema.len() != it.len()){ + (self.fail-validation)(self, it, ctx: ctx, scope: scope, + message: "Expected " + str(self.tuple-schema.len()) + " values, but got " + str(it.len()) + ) + } for (key, schema) in self.tuple-schema.enumerate() { it.at(key) = (schema.validate)( schema,