Merge pull request #10242 from demarches-simplifiees/US/fix-decimal-number-validator

Correctif(annotations privées): ETQ instructeur, je ne pouvais modifier une annotation quand un usager avait saisi des valeur de champs incorrectes
This commit is contained in:
mfo 2024-04-02 07:13:07 +00:00 committed by GitHub
commit 4da36531d4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
28 changed files with 231 additions and 146 deletions

View file

@ -74,8 +74,8 @@ class Migrations::BatchUpdatePaysValuesJob < ApplicationJob
def perform(ids)
ids.each do |id|
pays_champ = Champs::PaysChamp.find(id)
next if pays_champ.valid?
next if pays_champ.valid?(pays_champ.public? ? :champs_public_value : :champs_private_value)
code = APIGeoService.country_code(pays_champ.value)
value = if code.present?
APIGeoService.country_name(code)

View file

@ -5,7 +5,11 @@ class Champs::BooleanChamp < Champ
before_validation :set_value_to_nil, if: -> { value.blank? }
before_validation :set_value_to_false, unless: -> { ([nil, TRUE_VALUE, FALSE_VALUE]).include?(value) }
validates :value, inclusion: [TRUE_VALUE, FALSE_VALUE], allow_nil: true, allow_blank: false
# TODO: if: -> { validate_champ_value? || validation_context == :prefill }
validates :value, inclusion: [TRUE_VALUE, FALSE_VALUE],
allow_nil: true,
allow_blank: false,
if: :validate_champ_value?
def true?
value == TRUE_VALUE

View file

@ -1,5 +1,8 @@
class Champs::CiviliteChamp < Champ
validates :value, inclusion: ["M.", "Mme"], allow_nil: true, allow_blank: false
validates :value, inclusion: ["M.", "Mme"],
allow_nil: true,
allow_blank: false,
if: -> { validate_champ_value? || validation_context == :prefill }
def legend_label?
true

View file

@ -1,5 +1,6 @@
class Champs::CnafChamp < Champs::TextChamp
# see https://github.com/betagouv/api-particulier/blob/master/src/presentation/middlewares/cnaf-input-validation.middleware.ts
# TODO: if: -> { validate_champ_value? || validation_context == :prefill }
validates :numero_allocataire, format: { with: /\A\d{1,7}\z/ }, if: -> { code_postal.present? && validate_champ_value? }
validates :code_postal, format: { with: /\A\w{5}\z/ }, if: -> { numero_allocataire.present? && validate_champ_value? }

View file

@ -14,7 +14,7 @@ class Champs::DecimalNumberChamp < Champ
message: -> (object, _data) {
"« #{object.libelle} » " + object.errors.generate_message(:value, :not_a_number)
}
}
}, if: -> { validate_champ_value? || validation_context == :prefill }
def for_export
processed_value

View file

@ -1,5 +1,6 @@
class Champs::DepartementChamp < Champs::TextChamp
store_accessor :value_json, :code_region
# TODO: if: -> { validate_champ_value? || validation_context == :prefill }
validate :value_in_departement_names, unless: -> { value.nil? }
validate :external_id_in_departement_codes, unless: -> { external_id.nil? }
before_save :store_code_region

View file

@ -1,5 +1,6 @@
class Champs::DgfipChamp < Champs::TextChamp
# see https://github.com/betagouv/api-particulier/blob/master/src/presentation/middlewares/dgfip-input-validation.middleware.ts
# TODO: if: -> { validate_champ_value? || validation_context == :prefill }
validates :numero_fiscal, format: { with: /\A\w{13,14}\z/ }, if: -> { reference_avis.present? && validate_champ_value? }
validates :reference_avis, format: { with: /\A\w{13,14}\z/ }, if: -> { numero_fiscal.present? && validate_champ_value? }

View file

@ -1,4 +1,5 @@
class Champs::DossierLinkChamp < Champ
# TODO: if: -> { validate_champ_value? || validation_context == :prefill }
validate :value_integerable, if: -> { value.present? }, on: :prefill
private

View file

@ -5,6 +5,7 @@ class Champs::DropDownListChamp < Champ
OTHER = '__other__'
delegate :options_without_empty_value_when_mandatory, to: :type_de_champ
# TODO: if: -> { validate_champ_value? || validation_context == :prefill }
validate :value_is_in_options, unless: -> { value.blank? || drop_down_other? }
def render_as_radios?

View file

@ -1,5 +1,6 @@
class Champs::EmailChamp < Champs::TextChamp
include EmailSanitizableConcern
before_validation -> { sanitize_email(:value) }
# TODO: if: -> { validate_champ_value? || validation_context == :prefill }
validates :value, allow_blank: true, format: { with: StrictEmailValidator::REGEXP }, if: :validate_champ_value?
end

View file

@ -1,7 +1,8 @@
class Champs::EngagementJuridiqueChamp < Champ
# cf: https://communaute.chorus-pro.gouv.fr/documentation/creer-un-engagement/#1522314752186-a34f3662-0644b5d1-16c22add-8ea097de-3a0a
# TODO: if: -> { validate_champ_value? || validation_context == :prefill }
validates_with ExpressionReguliereValidator,
expression_reguliere: /([A-Z]|[0-9]|\-|\_|\+|\/)+/,
expression_reguliere_error_message: "Le numéro d'EJ ne peut contenir que des caractères alphanumérique et les caractères spéciaux suivant : “-“ ; “_“ ; “+“ ; “/“",
if: -> { validation_context != :brouillon }
if: :validate_champ_value?
end

View file

@ -3,6 +3,7 @@ class Champs::EpciChamp < Champs::TextChamp
before_validation :on_departement_change
before_validation :on_epci_name_changes
# TODO: if: -> { validate_champ_value? || validation_context == :prefill }
validate :code_departement_in_departement_codes, unless: -> { code_departement.nil? }
validate :external_id_in_departement_epci_codes, unless: -> { code_departement.nil? || external_id.nil? }
validate :value_in_departement_epci_names, unless: -> { code_departement.nil? || external_id.nil? || value.nil? }

View file

@ -1,3 +1,4 @@
class Champs::ExpressionReguliereChamp < Champ
# TODO: if: -> { validate_champ_value? || validation_context == :prefill }
validates_with ExpressionReguliereValidator, if: :validate_champ_value?
end

View file

@ -1,4 +1,5 @@
class Champs::IbanChamp < Champ
# TODO: if: -> { validate_champ_value? || validation_context == :prefill }
validates_with IbanValidator, if: :validate_champ_value?
after_validation :format_iban

View file

@ -7,7 +7,7 @@ class Champs::IntegerNumberChamp < Champ
# i18n-tasks-use t('errors.messages.not_an_integer')
"« #{object.libelle} » " + object.errors.generate_message(:value, :not_an_integer)
}
}
}, if: -> { validate_champ_value? || validation_context == :prefill }
def for_export
processed_value

View file

@ -1,4 +1,5 @@
class Champs::MultipleDropDownListChamp < Champ
# TODO: if: -> { validate_champ_value? || validation_context == :prefill }
validate :values_are_in_options, if: -> { value.present? }
def options?

View file

@ -1,6 +1,15 @@
class Champs::PaysChamp < Champs::TextChamp
validates :value, inclusion: APIGeoService.countries.pluck(:name), allow_nil: true, allow_blank: false
validates :external_id, inclusion: APIGeoService.countries.pluck(:code), allow_nil: true, allow_blank: false
with_options if: :validate_champ_value? do
validates :external_id, inclusion: APIGeoService.countries.pluck(:code), allow_nil: true, allow_blank: false
validates :value, inclusion: APIGeoService.countries.pluck(:name), allow_nil: true, allow_blank: false
end
# def value=(code) can reset champs to nil if value is empty, in case of prefill
# we do not want to try to save the champ with an nil value
with_options if: -> { validation_context == :prefill } do
validates :external_id, inclusion: APIGeoService.countries.pluck(:code), allow_nil: false, allow_blank: false
validates :value, inclusion: APIGeoService.countries.pluck(:name), allow_nil: false, allow_blank: false
end
def for_export
[name, code]
@ -26,8 +35,13 @@ class Champs::PaysChamp < Champs::TextChamp
self.external_id = nil
super(nil)
elsif code != value
self.external_id = APIGeoService.country_code(code)
super(code)
self.external_id = APIGeoService.country_code(code) # lookup by code which is a country name
if self.external_id # if we match a country code, lookup for country name with code
super(APIGeoService.country_name(self.external_id, locale: 'FR'))
else # if we did not match any country code, external_id is nil as well as value
super(nil)
end
end
end

View file

@ -25,7 +25,8 @@ class Champs::PhoneChamp < Champs::TextChamp
possible: true,
allow_blank: true,
message: I18n.t(:not_a_phone, scope: 'activerecord.errors.messages')
}, unless: -> { Phonelib.valid_for_countries?(value, DEFAULT_COUNTRY_CODES) }
},
if: -> { (validate_champ_value? || validation_context == :prefill) && !Phonelib.valid_for_countries?(value, DEFAULT_COUNTRY_CODES) }
def to_s
return '' if value.blank?

View file

@ -1,6 +1,7 @@
class Champs::PieceJustificativeChamp < Champ
FILE_MAX_SIZE = 200.megabytes
# TODO: if: -> { validate_champ_value? || validation_context == :prefill }
validates :piece_justificative_file,
size: { less_than: FILE_MAX_SIZE },
if: -> { !type_de_champ.skip_pj_validation }

View file

@ -1,4 +1,5 @@
class Champs::RegionChamp < Champs::TextChamp
# TODO: if: -> { validate_champ_value? || validation_context == :prefill }
validate :value_in_region_names, unless: -> { value.nil? }
validate :external_id_in_region_codes, unless: -> { external_id.nil? }

View file

@ -1,6 +1,7 @@
class Champs::RNAChamp < Champ
include RNAChampAssociationFetchableConcern
# TODO: if: -> { validate_champ_value? || validation_context == :prefill }
validates :value, allow_blank: true, format: {
with: /\AW[0-9A-Z]{9}\z/, message: I18n.t(:not_a_rna, scope: 'activerecord.errors.messages')
}, if: :validate_champ_value?

View file

@ -1,6 +1,7 @@
class Champs::TitreIdentiteChamp < Champ
FILE_MAX_SIZE = 20.megabytes
ACCEPTED_FORMATS = ['image/png', 'image/jpeg']
# TODO: if: -> { validate_champ_value? || validation_context == :prefill }
validates :piece_justificative_file, content_type: ACCEPTED_FORMATS, size: { less_than: FILE_MAX_SIZE }
def main_value_name