#3914 Add validators for the API field types on create_new_project
Closed 5 years ago by lenkaseg. Opened 5 years ago by lenkaseg.
Unknown source api_field_validators  into  master

file modified
+37
@@ -470,6 +470,43 @@

          viewonly=True,

      )

  

+     @validates('name')

+     def validate_name(self, key, name):

+         if not name:

+             raise AssertionError('No username provided')

+         if type(name) != six.text_type:

+             raise AssertionError('Name must be a string')

+         return name

+ 

+     @validates('description')

+     def validate_description(self, key, description):

+          if not description:

+              raise AssertionError('No description provided')

+          if type(description) != six.text_type:

+              raise AssertionError('Description must be a string {0}'.format(type(description)))

+          return description

+ 

+     @validates('namespace')

+     def validate_namespace(self, key, namespace):

+         if namespace:

+             if type(namespace) != six.text_type:

+                 raise AssertionError('If you type namespace, please make it a string')

+         return namespace

+ 

+     @validates('url')

+     def validate_url(self, key, url):

+         if url:

+             if type(url) != six.text_type:

+                 raise AssertionError('If you type url, please make it a string')

+         return url

+ 

+     @validates('avatar_email')

+     def validates_avatar_email(self, key, avatar_email):

+         if avatar_email:

+             if not re.match("[^@]+@[^@]+\.[^@]+", avatar_email):

+                 raise AssertionError('If you type avatar_email, please make it a real email')

+         return avatar_email

+ 

      @property

      def isa(self):

          """ A string to allow finding out that this is a project. """

Partly fixes #2485.

I added the sqlalchemy validators for the field types for create new project.
Is it ok like this? (If yes, I'll do the same with create new issue and so on.)

I had some problems with validating the create_readme field => should be boolean. While testing, even when I was passing non boolean value, no exception was raised. Then I figured out, that create_readme always switches to "False', which then passes as bool. Do you have any idea why this happens or should I investigate more?

The rest should be working, but since it's still not finished I was not running the tests.

This looks neat but it's adding some code without removing any, shouldn't this allow us to drop some of our existing code?
Also, out of curiosity, did you see any issue with the current code or is it more a general improvement in the way of validating things?

The only validation I found was that name and description cannot be empty. You mean to drop these?
You mean why I used sqlalchemy validators? I just saw it later in model.py as an ssh_key validation and I googled a nice example of how to write it :)

Pull-Request has been closed by lenkaseg

5 years ago