Skip to content

Commit

Permalink
validation: refactor enum and lang from functions to attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
Pablo Panero committed Oct 30, 2019
1 parent a5f12e9 commit ac4cdd8
Showing 1 changed file with 17 additions and 44 deletions.
61 changes: 17 additions & 44 deletions invenio_rdm_records/marshmallow/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,14 @@ class ContributorSchemaV1(StrictKeysMixin):

ids = fields.Nested(PersonIdsSchemaV1, many=True)
name = SanitizedUnicode(required=True)
role = SanitizedUnicode()
role = SanitizedUnicode(
validate=validate.OneOf(
choices=ROLES,
error=_('Invalid role. {input} not one of {choices}.')
))
affiliations = fields.List(SanitizedUnicode())
email = fields.Email()

@validates('role')
def validate_role(self, value):
"""Validate that the role is one of the allowed ones."""
if value not in self.ROLES:
raise ValidationError(
_('Invalid role. Not one of {allowed}.'.format(
allowed=self.ROLES)),
field_names=['contributor']
)


class ResourceTypeSchemaV1(StrictKeysMixin):
"""Resource type schema."""
Expand Down Expand Up @@ -109,24 +103,17 @@ class TitleSchemaV1(StrictKeysMixin):
]

title = SanitizedUnicode(required=True, validate=validate.Length(min=3))
title_type = SanitizedUnicode()
title_type = SanitizedUnicode(validate=validate.OneOf(
choices=TITLE_TYPES,
error=_('Invalid title type. {input} not one of {choices}.')
))
lang = SanitizedUnicode()

@validates('lang')
def validate_language(self, value):
"""Validate that language is ISO 639-3 value."""
validate_iso639_3(value)

@validates('title_type')
def validate_title_type(self, value):
"""Validate that the title type is one of the allowed ones."""
if value not in self.TITLE_TYPES:
raise ValidationError(
_('Invalid title type. Not one of {allowed}.'.format(
allowed=self.TITLE_TYPES)),
field_names=['additional_titles']
)


class DescriptionSchemaV1(StrictKeysMixin):
"""Schema for the additional descriptions."""
Expand All @@ -141,24 +128,17 @@ class DescriptionSchemaV1(StrictKeysMixin):
]
description = SanitizedUnicode(required=True,
validate=validate.Length(min=3))
description_type = SanitizedUnicode()
description_type = SanitizedUnicode(validate=validate.OneOf(
choices=DESCRIPTION_TYPES,
error=_('Invalid description type. {input} not one of {choices}.')
))
lang = SanitizedUnicode()

@validates('lang')
def validate_language(self, value):
"""Validate that language is ISO 639-3 value."""
validate_iso639_3(value)

@validates('description_type')
def validate_description_type(self, value):
"""Validate that the description type is one of the allowed ones."""
if value not in self.DESCRIPTION_TYPES:
raise ValidationError(
_('Invalid description type. Not one of {allowed}.'.format(
allowed=self.DESCRIPTION_TYPES)),
field_names=['additional_descriptions']
)


class DateSchemaV1(StrictKeysMixin):
"""Schema for date intervals."""
Expand All @@ -171,19 +151,12 @@ class DateSchemaV1(StrictKeysMixin):

start = DateString()
end = DateString()
type = fields.Str(required=True)
type = fields.Str(required=True, validate=validate.OneOf(
choices=DATE_TYPES,
error=_('Invalid date type. {input} not one of {choices}.')
))
description = fields.Str()

@validates('type')
def validate_type(self, value):
"""Validate that the type is one of the allowed ones."""
if value not in self.DATE_TYPES:
raise ValidationError(
_('Invalid date type. Not one of {allowed}.'.format(
allowed=self.DATE_TYPES)),
field_names=['dates']
)


class RightSchemaV1(StrictKeysMixin):
"""Schema for rights."""
Expand Down

0 comments on commit ac4cdd8

Please sign in to comment.