diff --git a/lib/easy_talk/builders/array_builder.rb b/lib/easy_talk/builders/array_builder.rb index 129c1f1..5b13720 100644 --- a/lib/easy_talk/builders/array_builder.rb +++ b/lib/easy_talk/builders/array_builder.rb @@ -11,6 +11,7 @@ class ArrayBuilder < BaseBuilder const: { type: T::Array[T.untyped], key: :const } }.freeze + sig { params(name: Symbol, type: T.untyped, options: T::Hash[Symbol, T.untyped]).void } def initialize(name, type, options = {}) @inner_type = type.respond_to?(:raw_type) ? type.raw_type : type update_option_types diff --git a/lib/easy_talk/builders/base_builder.rb b/lib/easy_talk/builders/base_builder.rb index fae69a6..662f219 100644 --- a/lib/easy_talk/builders/base_builder.rb +++ b/lib/easy_talk/builders/base_builder.rb @@ -13,6 +13,14 @@ class BaseBuilder attr_reader :name, :schema + sig do + params( + name: Symbol, + schema: T::Hash[Symbol, T.untyped], + options: T::Hash[Symbol, String], + valid_options: T::Hash[Symbol, T.untyped] + ).void + end def initialize(name, schema, options = {}, valid_options = {}) @valid_options = COMMON_OPTIONS.merge(valid_options) options.assert_valid_keys(@valid_options.keys) diff --git a/lib/easy_talk/builders/boolean_builder.rb b/lib/easy_talk/builders/boolean_builder.rb index 776ab18..e09498e 100644 --- a/lib/easy_talk/builders/boolean_builder.rb +++ b/lib/easy_talk/builders/boolean_builder.rb @@ -11,6 +11,7 @@ class BooleanBuilder < BaseBuilder default: { type: T::Boolean, key: :default } }.freeze + sig { params(name: Symbol, options: T::Hash[Symbol, T.nilable(T.any(String, Integer))]).void } def initialize(name, options = {}) super(name, { type: 'boolean' }, options, VALID_OPTIONS) end diff --git a/lib/easy_talk/builders/integer_builder.rb b/lib/easy_talk/builders/integer_builder.rb index dcb077b..e5ddfad 100644 --- a/lib/easy_talk/builders/integer_builder.rb +++ b/lib/easy_talk/builders/integer_builder.rb @@ -15,7 +15,7 @@ class IntegerBuilder < BaseBuilder default: { type: Integer, key: :default } }.freeze - sig { params(name: String, options: T::Hash[Symbol, T.nilable(T.any(String, Integer))]).void } + sig { params(name: Symbol, options: T::Hash[Symbol, T.nilable(T.any(String, Integer))]).void } def initialize(name, options = {}) super(name, { type: 'integer' }, options, VALID_OPTIONS) end diff --git a/lib/easy_talk/builders/null_builder.rb b/lib/easy_talk/builders/null_builder.rb index 2fc8d47..e84ed2b 100644 --- a/lib/easy_talk/builders/null_builder.rb +++ b/lib/easy_talk/builders/null_builder.rb @@ -5,6 +5,7 @@ module Builders class NullBuilder < BaseBuilder VALID_OPTIONS = {} + sig { params(name: Symbol, options: T::Hash[Symbol, T.nilable(T.any(String, Integer))]).void } def initialize(name, options = {}) super(name, { type: 'null' }, options, VALID_OPTIONS) end diff --git a/lib/easy_talk/builders/number_builder.rb b/lib/easy_talk/builders/number_builder.rb index ba48215..72b3eff 100644 --- a/lib/easy_talk/builders/number_builder.rb +++ b/lib/easy_talk/builders/number_builder.rb @@ -14,6 +14,7 @@ class NumberBuilder < BaseBuilder default: { type: T.any(Integer, Float), key: :default } }.freeze + sig { params(name: Symbol, options: T::Hash[Symbol, T.nilable(T.any(String, Integer))]).void } def initialize(name, options = {}) super(name, { type: 'number' }, options) end diff --git a/lib/easy_talk/builders/string_builder.rb b/lib/easy_talk/builders/string_builder.rb index b1b796e..6425820 100644 --- a/lib/easy_talk/builders/string_builder.rb +++ b/lib/easy_talk/builders/string_builder.rb @@ -15,7 +15,7 @@ class StringBuilder < BaseBuilder default: { type: String, key: :default } }.freeze - sig { params(name: String, options: T::Hash[Symbol, T.nilable(T.any(String, Integer))]).void } + sig { params(name: Symbol, options: T::Hash[Symbol, T.nilable(T.any(String, Integer))]).void } def initialize(name, options = {}) super(name, { type: 'string' }, options, VALID_OPTIONS) end diff --git a/lib/easy_talk/schema_definition.rb b/lib/easy_talk/schema_definition.rb index eb7e6fb..4a1fe32 100644 --- a/lib/easy_talk/schema_definition.rb +++ b/lib/easy_talk/schema_definition.rb @@ -18,7 +18,7 @@ def initialize(klass, schema_definition) end end - # sig { params(name: Symbol, type: T.untyped, constraints: T::Hash[Symbol, T.untyped]).void } + sig { params(name: Symbol, type: T.untyped, constraints: T.untyped).void } def property(name, type, **constraints) @schema_definition[:properties] ||= {} @schema_definition[:properties].merge!(name => constraints.merge!(type:)) diff --git a/spec/easy_talk/array_builder_spec.rb b/spec/easy_talk/array_builder_spec.rb index 0b58815..7d92315 100644 --- a/spec/easy_talk/array_builder_spec.rb +++ b/spec/easy_talk/array_builder_spec.rb @@ -6,49 +6,49 @@ context 'with valid options' do context 'with a string inner type' do it 'returns a basic json object' do - prop = described_class.new('name', String).build + prop = described_class.new(:name, String).build expect(prop).to eq({ type: 'array', items: { type: 'string' } }) end it 'includes a title' do - prop = described_class.new('name', String, title: 'Title').build + prop = described_class.new(:name, String, title: 'Title').build expect(prop).to eq({ title: 'Title', type: 'array', items: { type: 'string' } }) end it 'includes a description' do - prop = described_class.new('name', String, description: 'Description').build + prop = described_class.new(:name, String, description: 'Description').build expect(prop).to eq({ description: 'Description', type: 'array', items: { type: 'string' } }) end it 'includes the minItems' do - prop = described_class.new('name', String, min_items: 1).build + prop = described_class.new(:name, String, min_items: 1).build expect(prop).to eq({ type: 'array', minItems: 1, items: { type: 'string' } }) end it 'includes the maxItems' do - prop = described_class.new('name', String, max_items: 10).build + prop = described_class.new(:name, String, max_items: 10).build expect(prop).to eq({ type: 'array', maxItems: 10, items: { type: 'string' } }) end it 'includes the uniqueItems' do - prop = described_class.new('name', String, unique_items: true).build + prop = described_class.new(:name, String, unique_items: true).build expect(prop).to eq({ type: 'array', uniqueItems: true, items: { type: 'string' } }) end it 'includes the enum' do - prop = described_class.new('name', String, enum: %w[one two three]).build + prop = described_class.new(:name, String, enum: %w[one two three]).build expect(prop).to eq({ type: 'array', enum: %w[one two three], items: { type: 'string' } }) end it 'includes the const' do - prop = described_class.new('name', String, const: %w[one]).build + prop = described_class.new(:name, String, const: %w[one]).build expect(prop).to eq({ type: 'array', const: %w[one], items: { type: 'string' } }) end context 'with an invalid constraint value' do it 'raises an error' do # unclear why this does not throw an error expect do - described_class.new('name', String, enum: [1, 2, 3]).build + described_class.new(:name, String, enum: [1, 2, 3]).build end.to raise_error(TypeError) end end @@ -56,49 +56,49 @@ context 'with an integer inner type' do it 'returns a basic json object' do - prop = described_class.new('name', Integer).build + prop = described_class.new(:name, Integer).build expect(prop).to eq({ type: 'array', items: { type: 'integer' } }) end it 'includes a title' do - prop = described_class.new('name', Integer, title: 'Title').build + prop = described_class.new(:name, Integer, title: 'Title').build expect(prop).to eq({ title: 'Title', type: 'array', items: { type: 'integer' } }) end it 'includes a description' do - prop = described_class.new('name', Integer, description: 'Description').build + prop = described_class.new(:name, Integer, description: 'Description').build expect(prop).to eq({ description: 'Description', type: 'array', items: { type: 'integer' } }) end it 'includes the minItems' do - prop = described_class.new('name', Integer, min_items: 1).build + prop = described_class.new(:name, Integer, min_items: 1).build expect(prop).to eq({ type: 'array', minItems: 1, items: { type: 'integer' } }) end it 'includes the maxItems' do - prop = described_class.new('name', Integer, max_items: 10).build + prop = described_class.new(:name, Integer, max_items: 10).build expect(prop).to eq({ type: 'array', maxItems: 10, items: { type: 'integer' } }) end it 'includes the uniqueItems' do - prop = described_class.new('name', Integer, unique_items: true).build + prop = described_class.new(:name, Integer, unique_items: true).build expect(prop).to eq({ type: 'array', uniqueItems: true, items: { type: 'integer' } }) end it 'includes the enum' do - prop = described_class.new('name', Integer, enum: [1, 2, 3]).build + prop = described_class.new(:name, Integer, enum: [1, 2, 3]).build expect(prop).to eq({ type: 'array', enum: [1, 2, 3], items: { type: 'integer' } }) end it 'includes the const' do - prop = described_class.new('name', Integer, const: [1]).build + prop = described_class.new(:name, Integer, const: [1]).build expect(prop).to eq({ type: 'array', const: [1], items: { type: 'integer' } }) end context 'with invalid constraint value' do it 'raises an error' do # unclear why this does not throw an error expect do - described_class.new('name', Integer, enum: %w[one two three]).build + described_class.new(:name, Integer, enum: %w[one two three]).build end.to raise_error(TypeError) end end @@ -108,7 +108,7 @@ context 'with invalid keys' do it 'raises an error' do expect do - described_class.new('name', String, invalid: 'key').build + described_class.new(:name, String, invalid: 'key').build end.to raise_error(ArgumentError, 'Unknown key: :invalid. Valid keys are: :title, :description, :optional, :min_items, :max_items, :unique_items, :enum, :const') end @@ -117,7 +117,7 @@ context 'with custom inner type' do class CustomType; end it 'returns a basic json object' do - prop = described_class.new('name', CustomType).build + prop = described_class.new(:name, CustomType).build expect(prop).to eq({ type: 'array', items: { type: 'object' } }) end end diff --git a/spec/easy_talk/examples/company_employees_spec.rb b/spec/easy_talk/examples/company_employees_spec.rb index f8210fb..9aafa71 100644 --- a/spec/easy_talk/examples/company_employees_spec.rb +++ b/spec/easy_talk/examples/company_employees_spec.rb @@ -14,10 +14,10 @@ class Address include EasyTalk::Model define_schema do - property 'street', String - property 'city', String - property 'state', String - property 'zip', String, pattern: '^[0-9]{5}(?:-[0-9]{4})?$' + property :street, String + property :city, String + property :state, String + property :zip, String, pattern: '^[0-9]{5}(?:-[0-9]{4})?$' end end @@ -26,20 +26,20 @@ class Employee define_schema do title 'Employee' description 'Company employee' - property 'name', String, title: 'Full Name' - property 'gender', String, enum: %w[male female other] - property 'department', String - property 'hire_date', Date - property 'active', T::Boolean, default: true - property 'address', T::Array[Address], optional: true + property :name, String, title: 'Full Name' + property :gender, String, enum: %w[male female other] + property :department, String + property :hire_date, Date + property :active, T::Boolean, default: true + property :address, T::Array[Address], optional: true end end it 'enhances the schema using the provided block' do company.define_schema do title 'Company' - property 'name', String - property 'employees', T::Array[Employee] + property :name, String + property :employees, T::Array[Employee] end expect(company.json_schema).to include_json({ diff --git a/spec/easy_talk/examples/payment_spec.rb b/spec/easy_talk/examples/payment_spec.rb index 1199212..65a8b69 100644 --- a/spec/easy_talk/examples/payment_spec.rb +++ b/spec/easy_talk/examples/payment_spec.rb @@ -7,11 +7,11 @@ class CreditCard include EasyTalk::Model define_schema do - property 'CardNumber', String - property 'CardType', String, enum: %w[Visa MasterCard AmericanExpress] - property 'CardExpMonth', Integer, minimum: 1, maximum: 12 - property 'CardExpYear', Integer, minimum: Date.today.year, maximum: Date.today.year + 10 - property 'CardCVV', String, pattern: '^[0-9]{3,4}$' + property :CardNumber, String + property :CardType, String, enum: %w[Visa MasterCard AmericanExpress] + property :CardExpMonth, Integer, minimum: 1, maximum: 12 + property :CardExpYear, Integer, minimum: Date.today.year, maximum: Date.today.year + 10 + property :CardCVV, String, pattern: '^[0-9]{3,4}$' additional_properties false end end @@ -20,8 +20,8 @@ class Paypal include EasyTalk::Model define_schema do - property 'PaypalEmail', String, format: 'email' - property 'PaypalPasswordEncrypted', String + property :PaypalEmail, String, format: 'email' + property :PaypalPasswordEncrypted, String additional_properties false end end @@ -30,10 +30,10 @@ class BankTransfer include EasyTalk::Model define_schema do - property 'BankName', String - property 'AccountNumber', String - property 'RoutingNumber', String - property 'AccountType', String, enum: %w[Checking Savings] + property :BankName, String + property :AccountNumber, String + property :RoutingNumber, String + property :AccountType, String, enum: %w[Checking Savings] additional_properties false end end @@ -44,8 +44,8 @@ class Payment define_schema do title 'Payment' description 'Payment info' - property 'PaymentMethod', String, enum: %w[CreditCard Paypal BankTransfer] - property 'Details', T.any(CreditCard, Paypal, BankTransfer) + property :PaymentMethod, String, enum: %w[CreditCard Paypal BankTransfer] + property :Details, T.any(CreditCard, Paypal, BankTransfer) end end diff --git a/spec/easy_talk/examples/ticketing_spec.rb b/spec/easy_talk/examples/ticketing_spec.rb index 34563e9..364c30b 100644 --- a/spec/easy_talk/examples/ticketing_spec.rb +++ b/spec/easy_talk/examples/ticketing_spec.rb @@ -7,8 +7,8 @@ class Subtask include EasyTalk::Model define_schema do - property 'id', Integer, description: 'Unique identifier for the subtask' - property 'name', String, description: 'Title of the subtask' + property :id, Integer, description: 'Unique identifier for the subtask' + property :name, String, description: 'Title of the subtask' end end @@ -16,15 +16,15 @@ class Ticket include EasyTalk::Model define_schema do - property 'id', Integer, description: 'Unique identifier for the ticket' - property 'name', String, description: 'Title of the ticket' - property 'description', String, description: 'Detailed description of the task' - property 'priority', String, enum: %w[High Meidum Low], description: 'Priority level' - property 'assignees', T::Array[String], description: 'List of users assigned to the task' - property 'subtasks', T::Array[T.nilable(Subtask)], optional: true, - description: 'List of subtasks associated with the main task' - property 'dependencies', T::Array[Integer], optional: true, - description: 'List of ticket IDs that this ticket depends on' + property :id, Integer, description: 'Unique identifier for the ticket' + property :name, String, description: 'Title of the ticket' + property :description, String, description: 'Detailed description of the task' + property :priority, String, enum: %w[High Meidum Low], description: 'Priority level' + property :assignees, T::Array[String], description: 'List of users assigned to the task' + property :subtasks, T::Array[T.nilable(Subtask)], optional: true, + description: 'List of subtasks associated with the main task' + property :dependencies, T::Array[Integer], optional: true, + description: 'List of ticket IDs that this ticket depends on' end end @@ -34,7 +34,7 @@ class ActionItems define_schema do title 'Action Items' description 'A list of action items' - property 'items', T::Array[Ticket] + property :items, T::Array[Ticket] end end diff --git a/spec/easy_talk/examples/user_model_spec.rb b/spec/easy_talk/examples/user_model_spec.rb index ee04058..5315015 100644 --- a/spec/easy_talk/examples/user_model_spec.rb +++ b/spec/easy_talk/examples/user_model_spec.rb @@ -21,8 +21,8 @@ class Phone define_schema do title 'Phone' description 'A phone number' - property 'number', String, title: 'Phone Number', format: 'phone' - property 'type', String, title: 'Phone Type' + property :number, String, title: 'Phone Number', format: 'phone' + property :type, String, title: 'Phone Type' end end @@ -30,12 +30,12 @@ class Phone user.define_schema do title 'User' description 'A user of the system' - property 'name', String, title: "Person's Name" - property 'email', String, format: 'email', title: "Person's email" - property 'dob', Date, title: 'Date of Birth' - property 'group', Integer, enum: [1, 2, 3], default: 1 - property 'phones', T::Array[Phone], title: 'Phones', min_items: 1 - property 'tags', T::Array[String], title: 'Tags' + property :name, String, title: "Person's Name" + property :email, String, format: 'email', title: "Person's email" + property :dob, Date, title: 'Date of Birth' + property :group, Integer, enum: [1, 2, 3], default: 1 + property :phones, T::Array[Phone], title: 'Phones', min_items: 1 + property :tags, T::Array[String], title: 'Tags' end expect(user.json_schema).to include_json({ diff --git a/spec/easy_talk/integer_builder_spec.rb b/spec/easy_talk/integer_builder_spec.rb index 41dc585..4627e84 100644 --- a/spec/easy_talk/integer_builder_spec.rb +++ b/spec/easy_talk/integer_builder_spec.rb @@ -5,57 +5,57 @@ RSpec.describe EasyTalk::Builders::IntegerBuilder do context 'with valid options' do it 'returns a bare json object' do - prop = described_class.new('name').build + prop = described_class.new(:name).build expect(prop).to eq({ type: 'integer' }) end it 'includes a title' do - prop = described_class.new('name', title: 'Title').build + prop = described_class.new(:name, title: 'Title').build expect(prop).to eq({ title: 'Title', type: 'integer' }) end it 'includes a description' do - prop = described_class.new('name', description: 'Description').build + prop = described_class.new(:name, description: 'Description').build expect(prop).to eq({ description: 'Description', type: 'integer' }) end it 'includes the minimum' do - prop = described_class.new('name', minimum: 1).build + prop = described_class.new(:name, minimum: 1).build expect(prop).to eq({ type: 'integer', minimum: 1 }) end it 'includes the maximum' do - prop = described_class.new('name', maximum: 10).build + prop = described_class.new(:name, maximum: 10).build expect(prop).to eq({ type: 'integer', maximum: 10 }) end it 'includes the exclusiveMinimum' do - prop = described_class.new('name', exclusive_minimum: 1).build + prop = described_class.new(:name, exclusive_minimum: 1).build expect(prop).to eq({ type: 'integer', exclusiveMinimum: 1 }) end it 'includes the exclusiveMaximum' do - prop = described_class.new('name', exclusive_maximum: 10).build + prop = described_class.new(:name, exclusive_maximum: 10).build expect(prop).to eq({ type: 'integer', exclusiveMaximum: 10 }) end it 'includes the multipleOf' do - prop = described_class.new('name', multiple_of: 2).build + prop = described_class.new(:name, multiple_of: 2).build expect(prop).to eq({ type: 'integer', multipleOf: 2 }) end it 'includes the enum' do - prop = described_class.new('name', enum: [1, 2, 3]).build + prop = described_class.new(:name, enum: [1, 2, 3]).build expect(prop).to eq({ type: 'integer', enum: [1, 2, 3] }) end it 'includes the const' do - prop = described_class.new('name', const: 1).build + prop = described_class.new(:name, const: 1).build expect(prop).to eq({ type: 'integer', const: 1 }) end it 'includes the default' do - prop = described_class.new('name', default: 1).build + prop = described_class.new(:name, default: 1).build expect(prop).to eq({ type: 'integer', default: 1 }) end end @@ -63,7 +63,7 @@ context 'with invalid options' do it 'raises an error' do expect do - described_class.new('name', invalid: 'option').build + described_class.new(:name, invalid: 'option').build end.to raise_error(ArgumentError) end end @@ -71,19 +71,19 @@ context 'with invalid option values' do it 'raises an error' do expect do - described_class.new('name', minimum: '1').build + described_class.new(:name, minimum: '1').build end.to raise_error(TypeError) end it 'raises an error' do expect do - described_class.new('name', maximum: '10').build + described_class.new(:name, maximum: '10').build end.to raise_error(TypeError) end context 'with nil value' do it 'raises an error with nil value' do - prop = described_class.new('name', minimum: nil).build + prop = described_class.new(:name, minimum: nil).build expect(prop).to eq({ type: 'integer' }) end end diff --git a/spec/easy_talk/model_spec.rb b/spec/easy_talk/model_spec.rb index cfbc0c5..6b427d0 100644 --- a/spec/easy_talk/model_spec.rb +++ b/spec/easy_talk/model_spec.rb @@ -26,7 +26,7 @@ user.define_schema do title 'User' description 'A user of the system' - property :name, String, title: "Person's Name" + property :name, String, title: 'Persons Name' property :tags, T::Array[String], min_items: 1, title: 'Tags' end diff --git a/spec/easy_talk/property_spec.rb b/spec/easy_talk/property_spec.rb index e292cd0..697b4a2 100644 --- a/spec/easy_talk/property_spec.rb +++ b/spec/easy_talk/property_spec.rb @@ -4,57 +4,57 @@ RSpec.describe EasyTalk::Property do it 'returns a string schema' do - prop = described_class.new('name', String).build + prop = described_class.new(:name, String).build expect(prop).to eq(type: 'string') end it 'returns an integer schema' do - prop = described_class.new('name', Integer).build + prop = described_class.new(:name, Integer).build expect(prop).to eq(type: 'integer') end it 'returns a number schema' do - prop = described_class.new('name', Float).build + prop = described_class.new(:name, Float).build expect(prop).to eq(type: 'number') end it 'returns a boolean schema' do - prop = described_class.new('name', T::Boolean).build + prop = described_class.new(:name, T::Boolean).build expect(prop).to eq(type: 'boolean') end it 'returns a null schema' do - prop = described_class.new('name', NilClass).build + prop = described_class.new(:name, NilClass).build expect(prop).to eq(type: 'null') end it 'returns an array of strings schema' do - prop = described_class.new('name', T::Array[String]).build + prop = described_class.new(:name, T::Array[String]).build expect(prop).to eq(type: 'array', items: { type: 'string' }) end it 'returns an array of integers schema' do - prop = described_class.new('name', T::Array[Integer]).build + prop = described_class.new(:name, T::Array[Integer]).build expect(prop).to eq(type: 'array', items: { type: 'integer' }) end it 'returns a date schema' do - prop = described_class.new('name', Date).build + prop = described_class.new(:name, Date).build expect(prop).to eq(type: 'string', format: 'date') end it 'returns a date-time schema' do - prop = described_class.new('name', DateTime).build + prop = described_class.new(:name, DateTime).build expect(prop).to eq(type: 'string', format: 'date-time') end it 'returns a time schema' do - prop = described_class.new('name', Time).build + prop = described_class.new(:name, Time).build expect(prop).to eq(type: 'string', format: 'time') end describe 'with a union type' do - prop = described_class.new('name', T.any(Integer, String)).build + prop = described_class.new(:name, T.any(Integer, String)).build it "returns a schema with 'anyOf' property" do expect(prop.keys).to include(:anyOf) expect(prop[:anyOf].size).to eq(2) @@ -66,34 +66,34 @@ describe 'with missing type' do it 'raises an error' do - expect { described_class.new('name').build }.to raise_error(ArgumentError, 'property type is missing') + expect { described_class.new(:name).build }.to raise_error(ArgumentError, 'property type is missing') end it 'raises an error when type is not supported' do - expect { described_class.new('name', Something) }.to raise_error(NameError, 'uninitialized constant Something') + expect { described_class.new(:name, Something) }.to raise_error(NameError, 'uninitialized constant Something') end it 'raises an error when type is empty' do expect do - described_class.new('name', '').build + described_class.new(:name, '').build end.to raise_error(ArgumentError, 'property type is missing') end it 'raises an error when type is nil' do expect do - described_class.new('name', nil).build + described_class.new(:name, nil).build end.to raise_error(ArgumentError, 'property type is missing') end it 'raises an error when type is blank' do expect do - described_class.new('name', ' ').build + described_class.new(:name, ' ').build end.to raise_error(ArgumentError, 'property type is missing') end it 'raises an error when type is an empty array' do expect do - described_class.new('name', []).build + described_class.new(:name, []).build end.to raise_error(ArgumentError, 'property type is missing') end end @@ -102,7 +102,7 @@ class CustomClass; end it 'returns an array of custom class type' do - prop = described_class.new('name', T::Array[CustomClass]).build + prop = described_class.new(:name, T::Array[CustomClass]).build expect(prop).to eq(type: 'array', items: { type: 'object' }) end end @@ -121,7 +121,7 @@ class CustomClass; end end it 'returns an array schema' do - prop = described_class.new('name', T::Array[user]).build + prop = described_class.new(:name, T::Array[user]).build expect(prop[:type]).to eq('array') expect(prop[:items]).to include(type: 'object') expect(prop[:items][:properties].keys).to eq(%i[name email age]) @@ -130,28 +130,28 @@ class CustomClass; end end # it 'returns an object type' do - # prop = described_class.new('name', T::Hash[Symbol, String]).build + # prop = described_class.new(:name, T::Hash[Symbol, String]).build # expect(prop).to eq(type: 'object') # end # it 'raises an error when type is not supported' do - # expect { described_class.new('name', Object).build }.to raise_error('Type Object not supported') + # expect { described_class.new(:name, Object).build }.to raise_error('Type Object not supported') # end # context 'as_json' do it 'returns a json schema' do - prop = described_class.new('name', String).as_json + prop = described_class.new(:name, String).as_json expect(prop).to include_json(type: 'string') end it 'returns a json schema with options' do - prop = described_class.new('email', String, title: 'Email Address', format: 'email').as_json + prop = described_class.new(:email, String, title: 'Email Address', format: 'email').as_json expect(prop).to include_json(type: 'string', title: 'Email Address', format: 'email') end context 'with a union type' do it 'returns a json schema with anyOf property' do - prop = described_class.new('name', T.any(Integer, String)).as_json + prop = described_class.new(:name, T.any(Integer, String)).as_json expect(prop).to include_json(anyOf: [{ type: 'integer' }, { type: 'string' }]) end end diff --git a/spec/easy_talk/string_builder_spec.rb b/spec/easy_talk/string_builder_spec.rb index ab5285d..c1a0c8a 100644 --- a/spec/easy_talk/string_builder_spec.rb +++ b/spec/easy_talk/string_builder_spec.rb @@ -5,52 +5,52 @@ RSpec.describe EasyTalk::Builders::StringBuilder do context 'with valid options' do it 'returns a bare json object' do - prop = described_class.new('name').build + prop = described_class.new(:name).build expect(prop).to eq({ type: 'string' }) end it 'includes a title' do - prop = described_class.new('name', title: 'Title').build + prop = described_class.new(:name, title: 'Title').build expect(prop).to eq({ title: 'Title', type: 'string' }) end it 'includes a description' do - prop = described_class.new('name', description: 'Description').build + prop = described_class.new(:name, description: 'Description').build expect(prop).to eq({ description: 'Description', type: 'string' }) end it 'includes the format' do - prop = described_class.new('name', format: 'email').build + prop = described_class.new(:name, format: 'email').build expect(prop).to eq({ type: 'string', format: 'email' }) end it 'includes the pattern' do - prop = described_class.new('name', pattern: '^[a-zA-Z]+$').build + prop = described_class.new(:name, pattern: '^[a-zA-Z]+$').build expect(prop).to eq({ type: 'string', pattern: '^[a-zA-Z]+$' }) end it 'includes the minLength' do - prop = described_class.new('name', min_length: 1).build + prop = described_class.new(:name, min_length: 1).build expect(prop).to eq({ type: 'string', minLength: 1 }) end it 'includes the maxLength' do - prop = described_class.new('name', max_length: 10).build + prop = described_class.new(:name, max_length: 10).build expect(prop).to eq({ type: 'string', maxLength: 10 }) end it 'includes the enum' do - prop = described_class.new('name', enum: %w[one two three]).build + prop = described_class.new(:name, enum: %w[one two three]).build expect(prop).to eq({ type: 'string', enum: %w[one two three] }) end it 'includes the const' do - prop = described_class.new('name', const: 'one').build + prop = described_class.new(:name, const: 'one').build expect(prop).to eq({ type: 'string', const: 'one' }) end it 'includes the default' do - prop = described_class.new('name', default: 'default').build + prop = described_class.new(:name, default: 'default').build expect(prop).to eq({ type: 'string', default: 'default' }) end end @@ -59,7 +59,7 @@ it 'raises an error' do error_msg = 'Unknown key: :invalid. Valid keys are: :title, :description, :optional, :format, :pattern, :min_length, :max_length, :enum, :const, :default' expect do - described_class.new('name', invalid: 'invalid').build + described_class.new(:name, invalid: 'invalid').build end.to raise_error(ArgumentError, error_msg) end end @@ -67,7 +67,7 @@ context 'with invalid values' do it 'raises an error' do expect do - described_class.new('name', min_length: 'invalid').build + described_class.new(:name, min_length: 'invalid').build end.to raise_error(TypeError) end end @@ -75,14 +75,14 @@ context 'with empty string value' do it 'raises a type error' do expect do - described_class.new('name', min_length: '').build + described_class.new(:name, min_length: '').build end.to raise_error(TypeError) end end context 'with nil value' do it 'does not include the key' do - prop = described_class.new('name', min_length: nil).build + prop = described_class.new(:name, min_length: nil).build expect(prop).to eq({ type: 'string' }) end end