diff --git a/g2p_registry_base/models/reg_id.py b/g2p_registry_base/models/reg_id.py index ab05f1b4..567126ac 100644 --- a/g2p_registry_base/models/reg_id.py +++ b/g2p_registry_base/models/reg_id.py @@ -3,6 +3,7 @@ import re from odoo import api, fields, models +from odoo.exceptions import ValidationError class G2PRegistrantID(models.Model): @@ -67,4 +68,3 @@ class G2PIDType(models.Model): "Name of the ID types should be unique", ), ] - diff --git a/g2p_registry_rest_api/models/registrant.py b/g2p_registry_rest_api/models/registrant.py index 1c89cfe0..2c6bfd46 100644 --- a/g2p_registry_rest_api/models/registrant.py +++ b/g2p_registry_rest_api/models/registrant.py @@ -80,6 +80,22 @@ def validate_id_type_no_spaces(cls, value): # noqa: B902 ) return value + @validator("value") + def validate_id_value(cls, value, values): + id_type = values.get("id_type") + if id_type: + id_type_id = request.env["g2p.id.type"].search( + [("name", "=", id_type)], limit=1 + ) + if not re.match(id_type_id.id_validation, value): + raise G2PApiValidationError( + error_message=G2PErrorCodes.G2P_REQ_005.get_error_message(), + error_code=G2PErrorCodes.G2P_REQ_005.get_error_code(), + error_description=f"The provided {id_type_id.name} ID '{value}' is invalid.", + ) + + return value + class RegistrantInfoIn(NaiveOrmModel): name: str