dry-schema

dry-rbMIT活跃维护

Ruby 的数据结构校验与类型转换库,用声明式 schema 定义字段规则,自动完成类型强制转换和校验,适合在 API 或表单入口处保证数据质量。

Coercion and validation for data structures

v1.16.0发布于 2026-03-03 08:28 UTC495star125fork同步于 2026-11-10 17:59 UTC

怎么上手

由 README 提炼,执行前请确认
gem install dry-schema
硬件要求按 README 说明

README 未说明

下载:源码 zip·GitHub Releases(安装包)本站不镜像安装包

项目说明

由 README 自动翻译提炼

Ruby 数据结构校验与类型转换库,帮你把外部输入变成可信数据

Ruby 的数据结构校验与类型转换库,用声明式 schema 定义字段规则,自动完成类型强制转换和校验,适合在 API 或表单入口处保证数据质量。

核心能力

  • 用 DSL 定义 schema 校验哈希等数据结构
  • 支持类型强制转换(coercion)后再校验
  • 提供清晰的校验错误信息
  • 属于 dry-rb 生态,可与 dry-validation 等配合

容易踩的坑

README 未给出使用示例,需查阅 dry-rb.org 官方文档;具体依赖的 Ruby 版本要求 README 未说明

内容由模型根据 README 生成,可能不完整或不准确,以官方文档为准。看原文 README

最近发版

5 个版本v1.16.0
  1. 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)

    Compare v1.15.0 ... v1.16.0

  2. 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? and false? predicates when generating JSON schemas. (@cramt in #499)
    • Allow symbols to be given for top_namespace setting. (@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::Struct instances 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/maxItems for array size predicates instead of minLength/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)

    Compare v1.14.1 ... v1.15.0

  3. v1.14.12025-03-03 · 2025-03-03 08:43 UTC
    查看更新说明

    Fixed

    • Syntax errors on 3.3.0 (@flash-gordon, see #488)

    Compare v1.14.0...v1.14.1

  4. v1.14.02025-01-06 · 2025-01-06 20:50 UTC
    查看更新说明

    Added

    • Better support for sets in excluded_from? and included_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)
    • KeyValidator works faster for large schemas (via #461) (@radarek)

    Compare v1.13.4...v1.14.0

  5. 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)

    Compare v1.13.3...v1.13.4

用户点评

登录用户的真实使用记录,可按版本和硬件筛选
共 0 条

    还没有人点评。第一个说出真实体验的人最有价值。

    README 原文

    未翻译

    dry-schema Gem Version CI Status

    Forum Chat

    Links

    License

    See LICENSE file.