How to declare schema with tuples of [:string, :integer]?

Hello, I have a schema which requires a metrics key whose value is an array of tuples, where the tuples must be [:string, :integer]. The strings must be filled and the integers must be positive. Here’s a simple example of valid data:

 "metrics": [
    ["start", 153535255],
    ["sendRequest", 153535258],
    ["receiveResponse", 153535260]

I’ve got the SchemaContract validating with this subset:

    class SchemaContract < Dry::Validation::Contract
      schema do
        required(:metrics).array do
          array? && size?(2)

Any suggestions for how I can validate the tuple rules themselves? (I don’t see a tuple type, but could I define one so it could be as succinct as tuple[filled(:string), value(:integer, gt?: 0)] ?)

Thank you!

I’m afraid this is not possible to validate tuples at the schema level, we’d need to improve dry-logic for that, am I right @solnic ?

Yes, there’s no predicate that would handle this at the moment. dry-validation could handle that with custom rules.

@solnic Any tips on how I can do that as a custom rule? I couldn’t figure out how to access .first and .last (or [0] and [1]) and validate each of them separately.

@ColinDKelley see “Defining a rule for each element of an array” section in rule docs. You’ll have to handle validation manually though, there’s no DSL for tuples.

@solnic Thank you for that pointer! I will try that.