Return error codes/keys instead of text

I’m trying to use dry-validation with an API where I return codes like blank, inclusion, invalid and I wonder if I can achieve the same with dry-validation.

There’s a really old thread here about this: Get keys instead of sentences for validation errors

But that looks outdated. Is there any new way to get those?

This is supported. Here’s a spec that shows the usage. If you’ve found any issues please report it.

This works for rules only, right?
Or is it possible to do the same for required/optional of a schema?

One way to achieve that is by getting the error list from https://github.com/dry-rb/dry-schema/blob/master/config/errors.yml and changing that to a format like:

en:
  dry_validation:
    errors:
      array?:
        text: "must be an array"
        meta:
          code: :array

      empty?:
        text: "must be empty"
        meta:
          code: :empty

Then you just add your yml to the dry-validation config, as in the last example from https://dry-rb.org/gems/dry-validation/configuration/

Thanks! There is a slight discrepancy between schema and rules but I can live with it for now:

{:age=>[{:text=>"...", :meta=>{"code"=>"..."}}]
{:age=>[{:text=>"...", :code=>"...", :input=>"..."}]

In rules I can also add additional context, for example the input which can be useful for debugging. It would be great if schema and rules would work the same here.

I might try to use an own backend as a (temporarily) solution.