Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error properly on tuple length mismatch #31

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading