Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use symbols for property names, instead of strings. #2

Merged
merged 1 commit into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/easy_talk/builders/array_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions lib/easy_talk/builders/base_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions lib/easy_talk/builders/boolean_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/easy_talk/builders/integer_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions lib/easy_talk/builders/null_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions lib/easy_talk/builders/number_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/easy_talk/builders/string_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/easy_talk/schema_definition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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:))
Expand Down
40 changes: 20 additions & 20 deletions spec/easy_talk/array_builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,99 +6,99 @@
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
end

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
Expand All @@ -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
Expand All @@ -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
Expand Down
24 changes: 12 additions & 12 deletions spec/easy_talk/examples/company_employees_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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({
Expand Down
26 changes: 13 additions & 13 deletions spec/easy_talk/examples/payment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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

Expand Down
24 changes: 12 additions & 12 deletions spec/easy_talk/examples/ticketing_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,24 @@ 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

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

Expand All @@ -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

Expand Down
16 changes: 8 additions & 8 deletions spec/easy_talk/examples/user_model_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,21 @@ 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

it 'enhances the schema using the provided block' do
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({
Expand Down
Loading