diff --git a/lib/api/decorators/property_schema_representer.rb b/lib/api/decorators/property_schema_representer.rb index c4aafe9706..56ae787b88 100644 --- a/lib/api/decorators/property_schema_representer.rb +++ b/lib/api/decorators/property_schema_representer.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 diff --git a/lib/api/decorators/schema.rb b/lib/api/decorators/schema.rb index 8dfde046f0..d11b3ea33c 100644 --- a/lib/api/decorators/schema.rb +++ b/lib/api/decorators/schema.rb @@ -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 }, diff --git a/lib/api/v3/utilities/custom_field_injector.rb b/lib/api/v3/utilities/custom_field_injector.rb index b54bb368b8..0345890446 100644 --- a/lib/api/v3/utilities/custom_field_injector.rb +++ b/lib/api/v3/utilities/custom_field_injector.rb @@ -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) diff --git a/spec/lib/api/v3/utilities/custom_field_injector_spec.rb b/spec/lib/api/v3/utilities/custom_field_injector_spec.rb index 9ebd92dd7c..08a06b6d44 100644 --- a/spec/lib/api/v3/utilities/custom_field_injector_spec.rb +++ b/spec/lib/api/v3/utilities/custom_field_injector_spec.rb @@ -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