allow integers only

This commit is contained in:
sebastiencarceles 2023-02-22 15:56:31 +01:00
parent 0aacaee2c2
commit 35f4874b69
6 changed files with 57 additions and 2 deletions

View file

@ -21,4 +21,13 @@
# type_de_champ_id :integer # type_de_champ_id :integer
# #
class Champs::DossierLinkChamp < Champ class Champs::DossierLinkChamp < Champ
validate :value_integerable, if: -> { value.present? }, on: :prefill
private
def value_integerable
Integer(value)
rescue ArgumentError
errors.add(:value, :not_integerable)
end
end end

View file

@ -37,7 +37,8 @@ class PrefillParams
TypeDeChamp.type_champs.fetch(:regions), TypeDeChamp.type_champs.fetch(:regions),
TypeDeChamp.type_champs.fetch(:departements), TypeDeChamp.type_champs.fetch(:departements),
TypeDeChamp.type_champs.fetch(:multiple_drop_down_list), TypeDeChamp.type_champs.fetch(:multiple_drop_down_list),
TypeDeChamp.type_champs.fetch(:epci) TypeDeChamp.type_champs.fetch(:epci),
TypeDeChamp.type_champs.fetch(:dossier_link)
] ]
attr_reader :champ, :value, :dossier attr_reader :champ, :value, :dossier

View file

@ -509,6 +509,10 @@ en:
not_in_departement_epci_codes: "must be a valid EPCI code of the matching department" not_in_departement_epci_codes: "must be a valid EPCI code of the matching department"
value: value:
not_in_departement_epci_names: "must be a valid EPCI name of the matching department" not_in_departement_epci_names: "must be a valid EPCI name of the matching department"
"champs/dossier_link_champ":
attributes:
value:
not_integerable: "must be an integer"
errors: errors:
format: "Field « %{attribute} » %{message}" format: "Field « %{attribute} » %{message}"
messages: messages:

View file

@ -504,6 +504,10 @@ fr:
not_in_departement_epci_codes: "doit être un code d'EPCI du département correspondant" not_in_departement_epci_codes: "doit être un code d'EPCI du département correspondant"
value: value:
not_in_departement_epci_names: "doit être un nom d'EPCI du département correspondant" not_in_departement_epci_names: "doit être un nom d'EPCI du département correspondant"
"champs/dossier_link_champ":
attributes:
value:
not_integerable: "doit être un entier"
errors: errors:
format: "Le champ « %{attribute} » %{message}" format: "Le champ « %{attribute} » %{message}"
messages: messages:

View file

@ -0,0 +1,37 @@
describe Champs::DossierLinkChamp, type: :model do
describe 'prefilling validations' do
describe 'value' do
subject { build(:champ_dossier_link, value: value).valid?(:prefill) }
context 'when nil' do
let(:value) { nil }
it { expect(subject).to eq(true) }
end
context 'when empty' do
let(:value) { '' }
it { expect(subject).to eq(true) }
end
context 'when an integer' do
let(:value) { 42 }
it { expect(subject).to eq(true) }
end
context 'when a string representing an integer' do
let(:value) { "42" }
it { expect(subject).to eq(true) }
end
context 'when it can be casted as integer' do
let(:value) { 'totoro' }
it { expect(subject).to eq(false) }
end
end
end
end

View file

@ -203,7 +203,7 @@ RSpec.describe PrefillParams do
it_behaves_like "a champ public value that is unauthorized", :decimal_number, "non decimal string" it_behaves_like "a champ public value that is unauthorized", :decimal_number, "non decimal string"
it_behaves_like "a champ public value that is unauthorized", :integer_number, "non integer string" it_behaves_like "a champ public value that is unauthorized", :integer_number, "non integer string"
it_behaves_like "a champ public value that is unauthorized", :number, "value" it_behaves_like "a champ public value that is unauthorized", :number, "value"
# TODO: SEB it_behaves_like "a champ public value that is unauthorized", :dossier_link, "value" it_behaves_like "a champ public value that is unauthorized", :dossier_link, "value"
it_behaves_like "a champ public value that is unauthorized", :titre_identite, "value" it_behaves_like "a champ public value that is unauthorized", :titre_identite, "value"
it_behaves_like "a champ public value that is unauthorized", :civilite, "value" it_behaves_like "a champ public value that is unauthorized", :civilite, "value"
it_behaves_like "a champ public value that is unauthorized", :date, "value" it_behaves_like "a champ public value that is unauthorized", :date, "value"