diff --git a/swh/model/identifiers.py b/swh/model/identifiers.py --- a/swh/model/identifiers.py +++ b/swh/model/identifiers.py @@ -695,6 +695,13 @@ if not o: raise ValidationError('Wrong input: Supported types are %s' % ( list(_object_type_map.keys()))) + if namespace != PID_NAMESPACE: + raise ValidationError( + "Wrong format: only supported namespace is '%s'" + % PID_NAMESPACE) + if scheme_version != PID_VERSION: + raise ValidationError( + 'Wrong format: only supported version is %d' % PID_VERSION) # internal swh representation resolution if isinstance(object_id, dict): object_id = object_id[o['key_id']] @@ -773,22 +780,8 @@ # Checking for parsing errors _ns, _version, _type, _id = pid_data - if _ns != PID_NAMESPACE: - raise ValidationError( - "Wrong format: only supported namespace is '%s'" % PID_NAMESPACE) - - if _version != str(PID_VERSION): - raise ValidationError( - 'Wrong format: only supported version is %d' % PID_VERSION) - pid_data[1] = int(pid_data[1]) - expected_types = PID_TYPES - if _type not in expected_types: - raise ValidationError( - 'Wrong format: Supported types are %s' % ( - ', '.join(expected_types))) - for otype, data in _object_type_map.items(): if _type == data['short_name']: pid_data[2] = otype @@ -798,12 +791,6 @@ raise ValidationError( 'Wrong format: Identifier should be present') - try: - validate_sha1(_id) - except ValidationError: - raise ValidationError( - 'Wrong format: Identifier should be a valid hash') - persistent_id_metadata = {} for part in persistent_id_parts: try: @@ -813,4 +800,5 @@ msg = 'Contextual data is badly formatted, form key=val expected' raise ValidationError(msg) pid_data.append(persistent_id_metadata) - return PersistentId._make(pid_data) + return PersistentId(pid_data[0], pid_data[1], + pid_data[2], pid_data[3], pid_data[4]) diff --git a/swh/model/tests/test_identifiers.py b/swh/model/tests/test_identifiers.py --- a/swh/model/tests/test_identifiers.py +++ b/swh/model/tests/test_identifiers.py @@ -894,6 +894,26 @@ ValidationError, _error): identifiers.parse_persistent_identifier(pid) + def test_persistentid_class_validation_error(self): + for _ns, _version, _type, _id, _error in [ + ('foo', 1, CONTENT, 'abc8bc9d7a6bcf6db04f476d29314f157507d505', + 'Wrong format: only supported namespace is \'swh\''), + ('swh', 2, DIRECTORY, 'def8bc9d7a6bcf6db04f476d29314f157507d505', + 'Wrong format: only supported version is 1'), + ('swh', 1, 'foo', 'fed8bc9d7a6bcf6db04f476d29314f157507d505', + 'Wrong input: Supported types are'), + ('swh', 1, SNAPSHOT, 'gh6959356d30f1a4e9b7f6bca59b9a336464c03d', + 'Unexpected characters') + ]: + with self.assertRaisesRegex( + ValidationError, _error): + PersistentId( + namespace=_ns, + scheme_version=_version, + object_type=_type, + object_id=_id + ) + class OriginIdentifier(unittest.TestCase): def setUp(self):