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
-
#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.
- #optional? ⇒ Boolean
- #property(name, type, constraints = {}, &blk) ⇒ 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.
19 20 21 22 23 |
# File 'lib/easy_talk/schema_definition.rb', line 19 def initialize(name, schema = {}) @schema = schema @schema[:additional_properties] = false unless schema.key?(:additional_properties) @name = name end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
17 18 19 |
# File 'lib/easy_talk/schema_definition.rb', line 17 def name @name end |
#schema ⇒ Object (readonly)
Returns the value of attribute schema.
17 18 19 |
# File 'lib/easy_talk/schema_definition.rb', line 17 def schema @schema end |
Instance Method Details
#compose(*subschemas) ⇒ Object
31 32 33 34 |
# File 'lib/easy_talk/schema_definition.rb', line 31 def compose(*subschemas) @schema[:subschemas] ||= [] @schema[:subschemas] += subschemas end |
#optional? ⇒ Boolean
64 65 66 |
# File 'lib/easy_talk/schema_definition.rb', line 64 def optional? @schema[:optional] end |
#property(name, type, constraints = {}, &blk) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/easy_talk/schema_definition.rb', line 39 def property(name, type, constraints = {}, &blk) validate_property_name(name) @schema[:properties] ||= {} if block_given? property_schema = SchemaDefinition.new(name) property_schema.instance_eval(&blk) @schema[:properties][name] = { type:, constraints:, properties: property_schema } else @schema[:properties][name] = { type:, constraints: } end end |
#validate_property_name(name) ⇒ Object
57 58 59 60 61 62 |
# File 'lib/easy_talk/schema_definition.rb', line 57 def validate_property_name(name) return if name.to_s.match?(/^[A-Za-z_][A-Za-z0-9_]*$/) raise InvalidPropertyNameError, "Invalid property name '#{name}'. Must start with letter/underscore and contain only letters, numbers, underscores" end |