A compact Ruby DSL for building standards-compliant JSON Schema documents. Zero dependencies. ask-schema powers tool parameter schemas in ask-tools.
gem "ask-schema"require "ask-schema"
schema = Ask::Schema.create do
string :name, description: "Full name"
integer :age, description: "Age in years", minimum: 0
boolean :active, required: false
end
instance = schema.new("user", description: "A user profile")
instance.to_json_schema
# => { name: "user", description: "A user profile", schema: { type: "object", ... } }
instance.to_json # pretty-printed JSON stringAsk::Schema.create returns a schema class. Instantiate it with .new("name") and call to_json_schema (a Hash with :name, :description, :schema keys) or to_json (pretty-printed JSON). Class-based usage works the same way: class Product < Ask::Schema with the same DSL, instantiated with Product.new("product").
| Category | DSL methods |
|---|---|
| Primitives | string, number, integer, boolean, null |
| Complex | object, array, any_of, one_of, optional (nullable via anyOf + null) |
| Named sub-schemas | define(:address) { ... }, referenced with object :billing, of: :address |
Keywords: description:, required: (default true), enum:, minimum: / maximum: / multiple_of:, min_length: / max_length: / pattern: / format:, min_items: / max_items:.
schema = Ask::Schema.create do
string :username, description: "Username", enum: %w[admin user guest], min_length: 3
number :price, minimum: 0, maximum: 999_999.99
array :tags, of: :string, min_items: 1
define(:address) do
string :street
string :city
end
object :billing, of: :address
optional :nickname do
string
end
endNamed definitions are emitted under $defs and referenced with $ref.
schema = Ask::Schema.create do
integer :age
string :country
given(age: 18, country: "US") do
requires :license_number
otherwise do
requires :country_name
end
end
dependent :shipping_address do
requires :name, :street, :city
end
endValues in given are coerced automatically: scalars become const, arrays become enum, Regexps become pattern. requires marks fields as required, validates adds per-field constraints.
schema = Ask::Schema.create do
define(:a) { object :b, of: :b }
define(:b) { object :a, of: :a }
end
schema.valid? # => false (circular reference)
schema.validate! # raises Ask::Schema::ValidationErrorThe full ask-rb documentation lives at https://ask-rb.github.io/ask-docs. ask-schema in depth covers the complete DSL. API reference: https://ask-rb.github.io/ask-docs/reference/api.
bundle install
bundle exec rake test
MIT