Skip to content

Commit

Permalink
Merge pull request #31 from typst-community/28-make-tuple-fail-gracef…
Browse files Browse the repository at this point in the history
…ully-for-wrong-number-of-values

Error properly on tuple length mismatch
  • Loading branch information
jamesrswift authored Jun 23, 2024
2 parents 78a36ad + 558e14d commit c7f582d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

---

Expand Down
8 changes: 8 additions & 0 deletions src/types/tuple.typ
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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,
Expand Down

0 comments on commit c7f582d

Please sign in to comment.