Class: EasyTalk::SchemaDefinition
- Inherits:
-
Object
- Object
- EasyTalk::SchemaDefinition
- Defined in:
- lib/easy_talk/schema_definition.rb
Overview
EasyTalk SchemaDefinition
SchemaDefinition provides the methods for defining a schema within the define_schema block. The @schema is a hash that contains the unvalidated schema definition for the model. A SchemaDefinition instanace is the passed to the Builder.build_schema method to validate and compile the schema.
Instance Attribute Summary collapse
-
#klass ⇒ Object
Add accessor for the model class.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#schema ⇒ Object
readonly
Returns the value of attribute schema.
Instance Method Summary collapse
- #compose(*subschemas) ⇒ Object
-
#initialize(name, schema = {}) ⇒ SchemaDefinition
constructor
A new instance of SchemaDefinition.
-
#nullable_optional_property(name, type, constraints = {}) ⇒ Object
Helper method for nullable and optional properties.
- #optional? ⇒ Boolean
- #property(name, type, constraints = {}) ⇒ Object
- #validate_property_name(name) ⇒ Object
Methods included from T::AnyOf
Methods included from T::OneOf
Methods included from T::AllOf
Constructor Details
#initialize(name, schema = {}) ⇒ SchemaDefinition
Returns a new instance of SchemaDefinition.
22 23 24 25 26 27 |
# File 'lib/easy_talk/schema_definition.rb', line 22 def initialize(name, schema = {}) @schema = schema @schema[:additional_properties] = false unless schema.key?(:additional_properties) @name = name @klass = nil # Initialize klass to nil end |
Instance Attribute Details
#klass ⇒ Object
Add accessor for the model class
20 21 22 |
# File 'lib/easy_talk/schema_definition.rb', line 20 def klass @klass end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
19 20 21 |
# File 'lib/easy_talk/schema_definition.rb', line 19 def name @name end |
#schema ⇒ Object (readonly)
Returns the value of attribute schema.
19 20 21 |
# File 'lib/easy_talk/schema_definition.rb', line 19 def schema @schema end |
Instance Method Details
#compose(*subschemas) ⇒ Object
35 36 37 38 |
# File 'lib/easy_talk/schema_definition.rb', line 35 def compose(*subschemas) @schema[:subschemas] ||= [] @schema[:subschemas] += subschemas end |
#nullable_optional_property(name, type, constraints = {}) ⇒ Object
Helper method for nullable and optional properties
65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/easy_talk/schema_definition.rb', line 65 def nullable_optional_property(name, type, constraints = {}) # Ensure type is nilable nilable_type = if type.respond_to?(:nilable?) && type.nilable? type else T.nilable(type) end # Ensure constraints include optional: true constraints = constraints.merge(optional: true) # Call standard property method property(name, nilable_type, constraints) end |
#optional? ⇒ Boolean
60 61 62 |
# File 'lib/easy_talk/schema_definition.rb', line 60 def optional? @schema[:optional] end |
#property(name, type, constraints = {}) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/easy_talk/schema_definition.rb', line 40 def property(name, type, constraints = {}, &) validate_property_name(name) @schema[:properties] ||= {} if block_given? raise ArgumentError, 'Block-style sub-schemas are no longer supported. Use class references as types instead.' end @schema[:properties][name] = { type:, constraints: } end |
#validate_property_name(name) ⇒ Object
52 53 54 55 56 57 58 |
# File 'lib/easy_talk/schema_definition.rb', line 52 def validate_property_name(name) return if name.to_s.match?(/^[A-Za-z_][A-Za-z0-9_]*$/) = "Invalid property name '#{name}'. Must start with letter/underscore " \ 'and contain only letters, numbers, underscores' raise InvalidPropertyNameError, end |