question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Validating value is unique

See original GitHub issue

Hi

I have a user schema with an email field. The email is validated to be unique:

class UserSchema(ma.Schema):
    def validate_email(value):
        return User.query.filter(User.email == value).first() is None

    username = ma.fields.String()
    email = ma.fields.Email(validate=validate_email)

This works great when creating users, but when updating a user with same email address it fails because there is already a user with same email address: the same user as the one being updated!

The validator unfortunately only has access to the email value so I can’t check if the user found is the same as the one being updated.

How do I best handle this?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
maximkulkincommented, Oct 17, 2016

I would suggest moving such validation outside of schema:

class UserSchema(ma.Schema):
    username = ma.fields.String()
    email = ma.fields.Email()

def user_exists(email):
    return User.query.filter(User.email == email).first() is not None

def user_create(payload):
    user_data, _ = UserSchema(strict=True).load(payload)
    if user_exists(user_data['email']):
        return {'error': 'User with this email already exists'}, 422
    user = User(**user_data)
    user.save()
0reactions
kazitanvirahsancommented, Aug 27, 2022

You can use another schema for update.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Excel formula: Data validation unique values only - Got It AI
To allow only unique values in a given range, you can use data validation with a custom formula based on the COUNTIF function....
Read more >
Laravel Validator unique value - Stack Overflow
This rule will check if the email is unique where the brand has the given value. More information here : http://laravel.com/docs/validation#rule ...
Read more >
Asking fields to be unique - FormValidation
Sometime the user need to fill multiple fields in form, also each of them must be unique. Any of them has to be...
Read more >
Validating Data To Be Unique In Excel Range
Problem: Validating the values entered in column A, so that a value could not be entered more than once. Solution: use data validation...
Read more >
Unique Record Validation - Tadabase
Depending on your requirements, checking fields for unique values can often be somewhat complex and can vary in complexity depending on your requirements....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found