README 未给出使用示例,需查阅 dry-rb.org 官方文档;具体依赖的 Ruby 版本要求 README 未说明
Ruby 数据结构校验与类型转换库,帮你把外部输入变成可信数据
Ruby 的数据结构校验与类型转换库,用声明式 schema 定义字段规则,自动完成类型强制转换和校验,适合在 API 或表单入口处保证数据质量。
核心能力
- 用 DSL 定义 schema 校验哈希等数据结构
- 支持类型强制转换(coercion)后再校验
- 提供清晰的校验错误信息
- 属于 dry-rb 生态,可与 dry-validation 等配合
容易踩的坑
- v1.16.0最新正式版2026-03-03 · 2026-03-03 08:28 UTC
Added
- Support JRuby (@katafrakt in #512, @timriley in 1a6202f)
Changed
- In JSON Schema output, represent values with an
eql?constraint as "const" values. (@adamransom in #514)require "dry/schema" Dry::Schema.load_extensions(:json_schema) schema = Dry::Schema.JSON do required(:version).value(:integer, eql?: 1) end schema.json_schema # => {"$schema": "http://json-schema.org/draft-06/schema#", type: "object", properties: {version: {type: "integer", const: 1}}, required: ["version"]}
Fixed
- In JSON Schema output, fix representation of arrays that are a combinations of schemas. (@adamransom in #516)
- Avoid mutation of arguments given to
Dry::Schema::Messages::I18n#get. (@flash-gordon in 51d48b6)
- v1.15.02026-01-09 · 2026-01-09 06:40 UTC
查看更新说明
Changed
- Set mimimum Ruby version to 3.2 (@timriley)
- Support UUID v6, v7 and v8 predicates (
:uuid_v6?,:uuid_v7?and:uuid_v8?). (@illiatdesdindes in #509) - Support
size?,format?,true?andfalse?predicates when generating JSON schemas. (@cramt in #499) - Allow symbols to be given for
top_namespacesetting. (@unused in #491)
Fixed
-
Support intersection types (created with
&operator) in schema definitions. (@baweaver in #496)Now works without errors:
intersection_type = Types::Hash.schema(a: Types::String) & (Types::Hash.schema(b: Types::String) | Types::Hash.schema(c: Types::String)) schema = Dry::Schema.Params do required(:body).value(intersection_type) end schema.call(body: {a: "test", b: "value"}) # passes schema.call(body: {b: "value"}) # fails - missing 'a' -
JSON schema generation now properly handles
Dry::Structinstances wrapped in constructors. (@baweaver in #497)Before, when generating JSON schema for a schema containing a Dry::Struct wrapped in a constructor (e.g.,
Address.constructor(&:itself)), all struct properties were omitted from the generated schema, returning only{type: "object"}instead of the full schema with properties.Before/after:
# Before: Missing struct properties Dry::Schema.Params do required(:address).value(Address.constructor(&:itself)) end.json_schema # => {:properties=>{:address=>{:type=>"object"}}} # No properties # After: Full struct schema included Dry::Schema.Params do required(:address).value(Address.constructor(&:itself)) end.json_schema # => {:properties=>{:address=>{:type=>"object", :properties=>{:street=>{...}}}}} # Properties included -
JSON schema generation now correctly uses
minItems/maxItemsfor array size predicates instead ofminLength/maxLength. (@baweaver in #498) -
Show correct index in errors when validating unexpected keys in arrays. (@katafrakt in #510)
-
Support validating nested arrays when using
config.validate_keys = true. (@misdoro in #508) -
Fix handling of i18n messages from proc/lambda-produced hashes. (@rrothenberger in #493)
-
Fix error arising when generating errors when a key is repeated in a nested schema. (@jacob-carlborg in #503)
-
Fix method call typo in
Dry::Schema::Trace#respond_to_missing?. (@flash-gordon in 13ddb51)
- v1.14.12025-03-03 · 2025-03-03 08:43 UTC
查看更新说明
- v1.14.02025-01-06 · 2025-01-06 20:50 UTC
查看更新说明
Added
- Better support for sets in
excluded_from?andincluded_in?predicates (via #480) (@flash-gordon)
Fixed
- Fix info extension for maybe macro (via #484) (@flash-gordon + @santiagodoldan)
- Missing message for :uri? predicate (via #477) (@timjnh)
- JSON schema output for
maybe(:array)(via #463) (@tomgi)
Changed
- Set minimum Ruby version to 3.1 (@flash-gordon)
KeyValidatorworks faster for large schemas (via #461) (@radarek)
- Better support for sets in
- v1.13.42024-05-22 · 2024-05-22 20:33 UTC
查看更新说明
Added
- Default error message for
:uri?(issue #476 via #477) (@timjnh)
Fixed
- Fix json-schema type of objects nested under arrays (issue #400 fixed via #462) (@tomgi)
Changed
- i18n backend is no longer eager-loaded (via 85a9e0b) (@adam12)
- Default error message for
用户点评
登录用户的真实使用记录,可按版本和硬件筛选还没有人点评。第一个说出真实体验的人最有价值。