include more schema details for custom fields

pull/2550/head
Jan Sandbrink 10 years ago
parent 7219eebee5
commit 6f90674440
  1. 4
      lib/api/decorators/property_schema_representer.rb
  2. 8
      lib/api/decorators/schema.rb
  3. 5
      lib/api/v3/utilities/custom_field_injector.rb
  4. 38
      spec/lib/api/v3/utilities/custom_field_injector_spec.rb

@ -47,7 +47,8 @@ module API
:required,
:writable,
:min_length,
:max_length
:max_length,
:regular_expression
property :type, exec_context: :decorator
property :name, exec_context: :decorator
@ -55,6 +56,7 @@ module API
property :writable, exec_context: :decorator
property :min_length, exec_context: :decorator
property :max_length, exec_context: :decorator
property :regular_expression, exec_context: :decorator
end
end
end

@ -37,15 +37,17 @@ module API
required: true,
writable: true,
min_length: nil,
max_length: nil)
max_length: nil,
regular_expression: nil)
raise ArgumentError if property.nil?
schema = ::API::Decorators::PropertySchemaRepresenter.new(type: type,
name: title,
required: required,
writable: writable)
schema.min_length = min_length if min_length
schema.max_length = max_length if max_length
schema.min_length = min_length
schema.max_length = max_length
schema.regular_expression = regular_expression
property property,
getter: -> (*) { schema },

@ -221,7 +221,10 @@ module API
type: TYPE_MAP[custom_field.field_format],
title: custom_field.name,
required: custom_field.is_required,
writable: true
writable: true,
min_length: (custom_field.min_length if custom_field.min_length > 0),
max_length: (custom_field.max_length if custom_field.max_length > 0),
regular_expression: (custom_field.regexp unless custom_field.regexp.blank?)
end
def path_method_for(custom_field)

@ -69,13 +69,49 @@ describe ::API::V3::Utilities::CustomFieldInjector do
let(:writable) { true }
end
context 'when the custom field is not required' do
it 'indicates no regular expression' do
is_expected.to_not have_json_path("#{cf_path}/regularExpression")
end
it 'indicates no minimum size' do
is_expected.to_not have_json_path("#{cf_path}/minLength")
end
it 'indicates no maximum size' do
is_expected.to_not have_json_path("#{cf_path}/maxLength")
end
context 'custom field is not required' do
let(:custom_field) { FactoryGirl.build(:custom_field, is_required: false) }
it 'marks the field as not required' do
is_expected.to be_json_eql(false.to_json).at_path("#{cf_path}/required")
end
end
context 'custom field has regex' do
let(:custom_field) { FactoryGirl.build(:custom_field, regexp: 'Foo+bar') }
it 'renders the regular expression' do
is_expected.to be_json_eql('Foo+bar'.to_json).at_path("#{cf_path}/regularExpression")
end
end
context 'custom field has minimum length' do
let(:custom_field) { FactoryGirl.build(:custom_field, min_length: 5) }
it 'renders the minimum length' do
is_expected.to be_json_eql(5.to_json).at_path("#{cf_path}/minLength")
end
end
context 'custom field has maximum length' do
let(:custom_field) { FactoryGirl.build(:custom_field, max_length: 5) }
it 'renders the maximum length' do
is_expected.to be_json_eql(5.to_json).at_path("#{cf_path}/maxLength")
end
end
end
describe 'version custom field' do

Loading…
Cancel
Save