Skip to content

Commit 3cb4a47

Browse files
committed
Validate presence of name for public project
Somewhat surprisingly there is currently no validation for the presence of `name` on the `Project` model itself, although I suspect the name is probably implicitly guaranteed not to be blank by the clients of editor-api. Since admin users in experience-cs are going to be entering the name manually in the administrate UI, I want to protect against a blank name and I want to be able to provide a sensible error message if none is set. Ideally I'd add a validation to the `Project` model itself. However, since I don't have access [1] to staging or production data for `editor-api`, I can't be sure that some existing projects would become invalid. So as before I've added the validation to the `PublicProject` wrapper class which means the validation will only be used in the context of the `Api::PublicProjectsController`. [1]: https://raspberrypifoundation.slack.com/archives/C08831T17H6/p1747900618263229
1 parent 8ef6c18 commit 3cb4a47

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

app/models/public_project.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ class PublicProject
55

66
attr_reader :project
77

8-
delegate :identifier, to: :project
8+
delegate :identifier, :name, to: :project
99

1010
validates :identifier, format: { with: PhraseIdentifier::PATTERN, allow_blank: true }
11+
validates :name, presence: true
1112

1213
def initialize(project)
1314
@project = project

spec/concepts/public_project/create_spec.rb

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@
77
subject(:create_project) { described_class.call(project_hash:) }
88

99
let(:identifier) { 'foo-bar-baz' }
10+
let(:name) { 'Foo bar baz' }
1011
let(:project_hash) do
1112
{
1213
identifier:,
1314
locale: 'en',
1415
project_type: Project::Types::SCRATCH,
15-
name: 'Foo bar baz'
16+
name:
1617
}
1718
end
1819

@@ -67,5 +68,17 @@
6768
expect(create_project[:error]).to eq('Error creating project: Validation failed: Identifier is invalid')
6869
end
6970
end
71+
72+
context 'when name is blank' do
73+
let(:name) { '' }
74+
75+
it 'returns failure' do
76+
expect(create_project.failure?).to be(true)
77+
end
78+
79+
it 'returns error message' do
80+
expect(create_project[:error]).to eq("Error creating project: Validation failed: Name can't be blank")
81+
end
82+
end
7083
end
7184
end

0 commit comments

Comments
 (0)