ℹ️ Select 'Choose Exercise', or randomize 'Next Random Exercise' in selected language.

Choose Exercise:
Timer 00:00
WPM --
Score --
Acc --
Correct chars --

PL/pgSQL Data Validation Function

PL/pgSQL

Goal -- WPM

Ready
Exercise Algorithm Area
1CREATE OR REPLACE FUNCTION validate_user_profile(
2p_username VARCHAR,
3p_email VARCHAR,
4p_age INTEGER,
5p_phone_number VARCHAR DEFAULT NULL
6) RETURNS TABLE(is_valid BOOLEAN, error_message TEXT) AS $$
7DECLARE
8v_errors TEXT[] := ARRAY[]::TEXT[];
9v_is_valid BOOLEAN := TRUE;
10BEGIN
11-- Rule 1: Username must not be empty and have at least 3 characters
12IF p_username IS NULL OR LENGTH(TRIM(p_username)) < 3 THEN
13v_errors := array_append(v_errors, 'Username must be at least 3 characters long.');
14v_is_valid := FALSE;
15END IF;
16
17-- Rule 2: Email must be a valid format (basic check)
18IF p_email IS NULL OR p_email !~ '^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$' THEN
19v_errors := array_append(v_errors, 'Invalid email format.');
20v_is_valid := FALSE;
21END IF;
22
23-- Rule 3: Age must be between 18 and 120 (inclusive)
24IF p_age IS NULL OR p_age < 18 OR p_age > 120 THEN
25v_errors := array_append(v_errors, 'Age must be between 18 and 120.');
26v_is_valid := FALSE;
27END IF;
28
29-- Rule 4: Phone number (if provided) must match a basic pattern
30IF p_phone_number IS NOT NULL AND p_phone_number != '' THEN
31-- Example: Basic pattern for digits, hyphens, spaces, parentheses
32IF p_phone_number !~ '^\+?[0-9\-\s\(\)]+$' THEN
33v_errors := array_append(v_errors, 'Invalid phone number format.');
34v_is_valid := FALSE;
35END IF;
36END IF;
37
38-- Return results
39IF v_is_valid THEN
40RETURN QUERY SELECT TRUE, NULL;
41ELSE
42-- Concatenate all errors into a single string for the error_message column
43RETURN QUERY SELECT FALSE, array_to_string(v_errors, '; ');
44END IF;
45END;
46$$ LANGUAGE plpgsql;
Algorithm description viewbox

PL/pgSQL Data Validation Function

Algorithm description:

This PL/pgSQL function provides a flexible data validation framework. It accepts multiple input parameters (e.g., username, email, age) and checks them against a series of predefined rules, such as length constraints, format requirements (using regular expressions), and range checks. If any rule is violated, an error message is recorded. The function returns a boolean indicating overall validity and a concatenated string of all validation errors, making it easy to integrate into data submission or processing workflows.

Algorithm explanation:

The `validate_user_profile` function takes several parameters and uses a `v_errors` array to collect any validation failures. Each validation rule is implemented as an `IF` statement. For example, username length is checked, email format is validated using a regular expression (`~`), and age is checked against a numerical range. If a rule fails, an appropriate error message is appended to `v_is_valid` is set to `FALSE`. After all checks, if `v_is_valid` is still `TRUE`, it returns `TRUE` with `NULL` error message. Otherwise, it returns `FALSE` and a semicolon-separated string of all collected error messages using `array_to_string`. The time complexity is O(k * R), where k is the number of validation rules and R is the average cost of applying a rule (e.g., regex matching). Space complexity is O(E), where E is the number of validation errors collected, for the `v_errors` array.

Pseudocode:

FUNCTION validate_user_profile(username, email, age, phone_number):
  errors = empty list
  is_valid = TRUE

  IF username is invalid (e.g., too short):
    add 'Username error' to errors
    is_valid = FALSE

  IF email is invalid (e.g., bad format):
    add 'Email error' to errors
    is_valid = FALSE

  IF age is invalid (e.g., out of range):
    add 'Age error' to errors
    is_valid = FALSE

  IF phone_number is provided and invalid:
    add 'Phone error' to errors
    is_valid = FALSE

  IF is_valid:
    RETURN (TRUE, NULL)
  ELSE:
    RETURN (FALSE, join errors with '; ')
END FUNCTION