Skip to content

Commit

Permalink
Added validation in save_user
Browse files Browse the repository at this point in the history
  • Loading branch information
BenjaminCharmes committed Mar 13, 2024
1 parent ec9e8d2 commit 6e5b8c4
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 12 deletions.
14 changes: 8 additions & 6 deletions pydatalab/pydatalab/models/people.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

import bson
import bson.errors
from pydantic import BaseModel, EmailStr, Field, validator
import re
from pydantic import BaseModel, EmailStr, Field, validator, validate_email

from pydatalab.models.entries import Entry
from pydatalab.models.utils import PyObjectId
Expand Down Expand Up @@ -94,17 +95,18 @@ def set_default_type(cls, _):

@validator("display_name")
def validate_display_name_length(cls, v):
"""Validate that the display name."""
"""Validate the display name."""
if len(v) > 150:
raise ValueError("Display name must be at most 150 characters long.")
raise ValueError(
"Display name must be at most 150 characters long.")
return v

@validator("contact_email")
def validate_contact_email_format(cls, v):
"""Validate that the contact email has a valid email format."""
email_regex = re.compile(r"[^@]+@[^@]+\.[^@]+")
if v is not None:
not email_regex.match(EmailStr(v))
if not validate_email(v):
raise ValueError(
"Invalid email format for contact email.")
return v

@staticmethod
Expand Down
7 changes: 7 additions & 0 deletions pydatalab/pydatalab/routes/v0_1/users.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from bson import ObjectId
from flask import Blueprint, jsonify, request
from flask_login import current_user
from pydantic import validate_email


from pydatalab.mongo import flask_mongo

Expand All @@ -12,7 +14,12 @@ def save_user(user_id):
request_json = request.get_json()

display_name = request_json.get("display_name")
if len(display_name) > 150:
return jsonify(status="error", detail="Name should be less than 150 characters."), 400

contact_email = request_json.get("contact_email")
if not validate_email(contact_email):
return jsonify(status="error", detail="Invalid email format for contact email."), 400

if not current_user.is_authenticated:
return jsonify(status="error"), 401
Expand Down
9 changes: 3 additions & 6 deletions webapp/src/components/EditAccountSettingsModal.vue
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
<template>
<form @submit.prevent="submitForm" class="modal-enclosure">
<Modal
:modelValue="modelValue"
@update:modelValue="$emit('update:modelValue', $event)"
:disableSubmit="
<Modal :modelValue="modelValue" @update:modelValue="$emit('update:modelValue', $event)">
<!-- :disableSubmit="
Boolean(displayNameValidationMessage) || Boolean(contactEmailValidationMessage)
"
>
" -->
<template v-slot:header> Account settings </template>

<template v-slot:body>
Expand Down

0 comments on commit 6e5b8c4

Please sign in to comment.