misc: add belgian national register number validation (#45039)
This commit is contained in:
parent
d98136de46
commit
3f46c3e404
|
@ -1006,6 +1006,36 @@ def test_wcsextrastringwidget_nir_validation():
|
|||
assert widget.has_error()
|
||||
|
||||
|
||||
def test_wcsextrastringwidget_belgian_nrn_validation():
|
||||
class FakeField:
|
||||
pass
|
||||
|
||||
fakefield = FakeField()
|
||||
fakefield.validation = {'type': 'nrn-be'}
|
||||
|
||||
# regular cases
|
||||
for value in ('85073003328', '17073003384', '40000095579', '00000100364', '40000100133'):
|
||||
widget = WcsExtraStringWidget('test', value='foo', required=False)
|
||||
widget.field = fakefield
|
||||
mock_form_submission(req, widget, {'test': value})
|
||||
assert not widget.has_error()
|
||||
|
||||
# failing cases
|
||||
for value in (
|
||||
'8507300332', # too short
|
||||
'850730033281', # too long
|
||||
'8507300332A', # not just digits
|
||||
'85143003377', # invalid month
|
||||
'85073203365', # invalid day
|
||||
'85073003329', # invalid checksum (<2000)
|
||||
'17073003385', # invalid checksum (≥2000)
|
||||
):
|
||||
widget = WcsExtraStringWidget('test', value='foo', required=False)
|
||||
widget.field = fakefield
|
||||
mock_form_submission(req, widget, {'test': value})
|
||||
assert widget.has_error()
|
||||
|
||||
|
||||
def test_wcsextrastringwidget_iban_validation():
|
||||
class FakeField:
|
||||
pass
|
||||
|
|
|
@ -1112,6 +1112,14 @@ class ValidationWidget(CompositeWidget):
|
|||
'function': 'validate_nir',
|
||||
},
|
||||
),
|
||||
(
|
||||
'nrn-be',
|
||||
{
|
||||
'title': _('National register number (Belgium)'),
|
||||
'error_message': _('Invalid national register number'),
|
||||
'function': 'validate_belgian_nrn',
|
||||
},
|
||||
),
|
||||
(
|
||||
'iban',
|
||||
{
|
||||
|
|
|
@ -842,6 +842,30 @@ def validate_nir(string_value):
|
|||
return int(nir_key) == 97 - int(string_value[:13]) % 97
|
||||
|
||||
|
||||
def validate_belgian_nrn(string_value):
|
||||
# https://fr.wikipedia.org/wiki/Numéro_de_registre_national
|
||||
if not string_value:
|
||||
return False
|
||||
if len(string_value) != 11:
|
||||
return False
|
||||
if not is_ascii_digit(string_value):
|
||||
return False
|
||||
_year, month, day, _index, checksum = (
|
||||
string_value[:2],
|
||||
string_value[2:4],
|
||||
string_value[4:6],
|
||||
string_value[6:9],
|
||||
string_value[9:11],
|
||||
)
|
||||
if int(month) > 12:
|
||||
return False
|
||||
if int(day) > 31:
|
||||
return False
|
||||
return (97 - int(string_value[:9]) % 97 == int(checksum)) or (
|
||||
97 - int('2' + string_value[:9]) % 97 == int(checksum)
|
||||
)
|
||||
|
||||
|
||||
IBAN_LENGTH = {
|
||||
# from https://www.iban.com/structure
|
||||
'AD': 24,
|
||||
|
|
Loading…
Reference in New Issue