Class: Ask::Schema
- Inherits:
-
Object
- Object
- Ask::Schema
- Extended by:
- DSL
- Includes:
- JsonOutput
- Defined in:
- lib/ask-schema.rb,
lib/ask/schema/dsl.rb,
lib/ask/schema/errors.rb,
lib/ask/schema/helpers.rb,
lib/ask/schema/version.rb,
lib/ask/schema/validator.rb,
lib/ask/schema/json_output.rb,
lib/ask/schema/dsl/utilities.rb,
lib/ask/schema/dsl/conditionals.rb,
lib/ask/schema/dsl/complex_types.rb,
lib/ask/schema/dsl/primitive_types.rb,
lib/ask/schema/dsl/schema_builders.rb
Overview
Build standards-compliant JSON Schema documents using a compact Ruby DSL.
Supports both block-based and class-based usage patterns, with full support for JSON Schema Draft 07/2020-12 features including composition, conditionals, definitions, and validation.
Defined Under Namespace
Modules: DSL, Helpers, JsonOutput Classes: Error, InvalidArrayTypeError, InvalidObjectTypeError, InvalidSchemaError, InvalidSchemaTypeError, LimitExceededError, ValidationError, Validator
Constant Summary collapse
- PRIMITIVE_TYPES =
Primitive JSON Schema types available in the DSL.
%i[string number integer boolean null].freeze
- VERSION =
"0.1.0"
Class Method Summary collapse
-
.additional_properties(value = nil) ⇒ Boolean
Set or get whether additional (undeclared) properties are allowed.
-
.create(&block) ⇒ Class<Schema>
Create a new Schema subclass from a DSL block.
-
.definitions ⇒ Hash{Symbol => Hash}
Named sub-schema definitions for ‘$defs`.
-
.description(description = nil) ⇒ String?
Set or get the schema description used in JSON output.
-
.name(name = nil) ⇒ String?
Set or get the schema name used in JSON output.
-
.properties ⇒ Hash{Symbol => Hash}
Properties defined on this schema class (inheritable).
-
.required_properties ⇒ Array<Symbol>
Required property names for this schema class (inheritable).
-
.strict(*args) ⇒ Boolean
Set or get strict mode for the schema.
-
.valid? ⇒ Boolean
Check if the schema definition is valid.
-
.validate! ⇒ nil
Validate the schema definition, raising on circular references or other errors.
Instance Method Summary collapse
-
#initialize(name = nil, description: nil) ⇒ Schema
constructor
Create a new schema instance.
-
#method_missing(method_name) ⇒ Object
Delegate DSL methods (string, number, integer, etc.) to the class level.
-
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
Check if a method can be delegated to the class level.
-
#valid? ⇒ Boolean
Check if this schema instance is valid.
-
#validate! ⇒ nil
Validate this schema instance.
Methods included from DSL::Utilities
Methods included from DSL::Conditionals
#conditions, #dependencies, #dependent, #given
Methods included from DSL::ComplexTypes
#any_of, #array, #object, #one_of, #optional
Methods included from DSL::PrimitiveTypes
#boolean, #integer, #null, #number, #string
Methods included from DSL::SchemaBuilders
#any_of_schema, #array_schema, #boolean_schema, #integer_schema, #null_schema, #number_schema, #object_schema, #one_of_schema, #string_schema
Methods included from JsonOutput
Constructor Details
#initialize(name = nil, description: nil) ⇒ Schema
Create a new schema instance.
142 143 144 145 |
# File 'lib/ask-schema.rb', line 142 def initialize(name = nil, description: nil) @name = name || self.class.name || "Schema" @description = description end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name) ⇒ Object
Delegate DSL methods (string, number, integer, etc.) to the class level.
163 164 165 166 167 168 169 |
# File 'lib/ask-schema.rb', line 163 def method_missing(method_name, ...) if respond_to_missing?(method_name) self.class.send(method_name, ...) else super end end |
Class Method Details
.additional_properties(value = nil) ⇒ Boolean
Set or get whether additional (undeclared) properties are allowed.
100 101 102 103 104 |
# File 'lib/ask-schema.rb', line 100 def additional_properties(value = nil) return @additional_properties ||= false if value.nil? @additional_properties = value end |
.create(&block) ⇒ Class<Schema>
Create a new Schema subclass from a DSL block.
49 50 51 52 53 |
# File 'lib/ask-schema.rb', line 49 def create(&block) schema_class = Class.new(Schema) schema_class.class_eval(&block) schema_class end |
.definitions ⇒ Hash{Symbol => Hash}
Named sub-schema definitions for ‘$defs`.
72 73 74 |
# File 'lib/ask-schema.rb', line 72 def definitions @definitions ||= {} end |
.description(description = nil) ⇒ String?
Set or get the schema description used in JSON output.
91 92 93 94 |
# File 'lib/ask-schema.rb', line 91 def description(description = nil) @description = description if description @description end |
.name(name = nil) ⇒ String?
Set or get the schema name used in JSON output.
80 81 82 83 84 85 |
# File 'lib/ask-schema.rb', line 80 def name(name = nil) @schema_name = name if name return @schema_name if defined?(@schema_name) super() end |
.properties ⇒ Hash{Symbol => Hash}
Properties defined on this schema class (inheritable).
58 59 60 |
# File 'lib/ask-schema.rb', line 58 def properties @properties ||= {} end |
.required_properties ⇒ Array<Symbol>
Required property names for this schema class (inheritable).
65 66 67 |
# File 'lib/ask-schema.rb', line 65 def required_properties @required_properties ||= [] end |
.strict(*args) ⇒ Boolean
Set or get strict mode for the schema.
When strict, the schema includes ‘“strict”: true` in output.
112 113 114 115 116 117 118 |
# File 'lib/ask-schema.rb', line 112 def strict(*args) if args.empty? instance_variable_defined?(:@strict) ? @strict : true else @strict = args.first end end |
Instance Method Details
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
Check if a method can be delegated to the class level.
172 173 174 |
# File 'lib/ask-schema.rb', line 172 def respond_to_missing?(method_name, include_private = false) %i[string number integer boolean array object any_of one_of null].include?(method_name) || super end |
#valid? ⇒ Boolean
Check if this schema instance is valid.
158 159 160 |
# File 'lib/ask-schema.rb', line 158 def valid? self.class.valid? end |
#validate! ⇒ nil
Validate this schema instance.
151 152 153 |
# File 'lib/ask-schema.rb', line 151 def validate! self.class.validate! end |