dry-schema
dry-schema copied to clipboard
various missing predicates for JSON schema
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
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
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)
Thanks for reporting this, folks. @cramt, do you think you might be able to turn that monkey patch into a PR? 😇
Ah, I missed #499. Thank you @cramt! I'll get onto it this week.
well i did just update it with the latest pissing predicates i found