Skip to content

Commit

Permalink
#556 -Add validation for document_id to prevent nil or empty values
Browse files Browse the repository at this point in the history
  • Loading branch information
Trehana committed Aug 16, 2024
1 parent 3557ff5 commit 9dcbbaa
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/meilisearch/index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,10 @@ def delete_documents!(documents_ids)
alias delete_multiple_documents! delete_documents!

def delete_document(document_id)
if document_id.nil? || document_id.empty?
raise MeiliSearch::Error::InvalidDocumentId, 'document_id cannot be empty or nil'
end

encode_document = URI.encode_www_form_component(document_id)
response = http_delete "/indexes/#{@uid}/documents/#{encode_document}"

Expand Down
4 changes: 4 additions & 0 deletions spec/meilisearch/index/documents_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,10 @@
describe '#delete_document' do
before { index.add_documents(documents).await }

it 'if the document id is nil, it raises an error' do
expect { index.delete_document(nil).await }.to raise_error(MeiliSearch::ApiError)
end

it 'deletes one document from index' do
id = 456
index.delete_document(id).await
Expand Down

0 comments on commit 9dcbbaa

Please sign in to comment.