dry-schema icon indicating copy to clipboard operation
dry-schema copied to clipboard

various missing predicates for JSON schema

Open cramt opened this issue 2 months ago • 5 comments

Describe the bug

A few predicates from dry-logic is missing in the json_schema compiler

To Reproduce

require "dry-types"
require "dry-schema"
Dry::Schema.load_extensions(:json_schema)

module Types
  include Dry.Types()
end

puts (Dry::Schema.Params do
  required(:_).value(Types::Hash.schema(
                       size: Types::Array.constrained(size: 5),
                       format: Types::String.constrained(format: /[A-Z]{2}/),
                       bool: Types::Bool,
                     ))
end).json_schema

Expected behavior

To not error and return the correct schema

My environment

  • Affects my production application: yes, but i've just monkeypatched it for now
  • Ruby version: 3.3.9
  • OS: NixOS

cramt avatar Oct 16 '25 09:10 cramt

My current monkeypatch solve is in case anyone else runs into this

module Dry
  module Schema
    module JSONSchema
      class SchemaCompiler
        PREDICATE_TO_TYPE = PREDICATE_TO_TYPE.merge(
          size?: {
            minLength: TO_INTEGER,
            maxLength: TO_INTEGER,
          },
          format?: {
            pattern: proc do |x|
              x.to_s.delete_prefix("(?-mix").delete_suffix(")")
            end,
          },
          true?: {},
          false?: {},
        ).freeze
      end
    end
  end
end

cramt avatar Oct 16 '25 09:10 cramt

The error message:

Could not find an equivalent conversion for predicate :size?.
(Dry::Schema::JSONSchema::SchemaCompiler::UnknownConversionError)

This means that your generated JSON schema may be missing this validation.

You can ignore this by generating the schema in "loose" mode, i.e.:
    my_schema.json_schema(loose: true)

esparta avatar Oct 20 '25 17:10 esparta

Thanks for reporting this, folks. @cramt, do you think you might be able to turn that monkey patch into a PR? 😇

timriley avatar Oct 20 '25 23:10 timriley

Ah, I missed #499. Thank you @cramt! I'll get onto it this week.

timriley avatar Oct 21 '25 06:10 timriley

well i did just update it with the latest pissing predicates i found

cramt avatar Oct 21 '25 06:10 cramt