typst-community / valkyrie

Type safe type safety for Typst
Other
30 stars 3 forks source link

Make tuple fail gracefully for wrong number of values #28

Closed jneug closed 3 months ago

jneug commented 4 months ago

The tuple type currently throws a somewhat confusing error message when the input has to few values, while it passes for inputs with more values than expected.

See this example:

#{
  let schema = t.tuple(t.integer(), t.integer())
  t.parse((0, 1, 2), schema) // passes
  t.parse((0, 1), schema)    // passes
  t.parse((0,), schema)      // fails with error
}

The first call to parse throws this error:

error: array index out of bounds (index: 1, len: 1) and no default value was specified
   ┌─ @preview/valkyrie:0.2.0/src/types/tuple.typ:25:10
   │
25 │           it.at(key),

It would be nice to get a more helpful error message like with the other types.

In addition, I would expect the validation to fail, if the supplied value has more values as expected. In the example, the first call to parse should have failed.

jamesrswift commented 4 months ago

Thanks for posting! This should be a pretty easy fix, I'll work on this this evening