Skip to content

Repository files navigation

ask-schema

Gem Version

A compact Ruby DSL for building standards-compliant JSON Schema documents. Zero dependencies. ask-schema powers tool parameter schemas in ask-tools.

Installation

gem "ask-schema"

Quick Start

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 string

Ask::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").

Types and keywords

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
end

Named definitions are emitted under $defs and referenced with $ref.

Conditionals

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
end

Values in given are coerced automatically: scalars become const, arrays become enum, Regexps become pattern. requires marks fields as required, validates adds per-field constraints.

Validation

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::ValidationError

Full documentation

The 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.

Development

bundle install
bundle exec rake test

License

MIT

About

JSON Schema DSL (zero deps) — extracted from ruby_llm-schema

Resources

Contributing

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages