aleeusgr / hs-clojure

Other
0 stars 0 forks source link

add input validation #17

Open aleeusgr opened 3 months ago

aleeusgr commented 3 months ago

Based on the provided code, I will outline the functional requirements for a data validation module:

Module: Data Validation Module

Purpose: To validate user input data to ensure it meets the required format and constraints before processing or storing it in the database.

Functional Requirements:

  1. Name Validation:
    • The module shall validate the name input to ensure it consists of two words containing only letters (e.g., "John Doe").
    • The module shall reject names with numbers, special characters, or single-word inputs.
  2. Sex Validation:
    • The module shall validate the sex input to ensure it is either "Male" or "Female" (case-insensitive).
    • The module shall reject any other values for sex.
  3. Date of Birth Validation:
    • The module shall validate the date of birth input to ensure it is in the format "YYYY-MM-DD".
    • The module shall reject dates outside the range of 1900-01-01 to the current date.
  4. Address Validation:
    • The module shall validate the address input to ensure it contains only alphanumeric characters, spaces, and commas.
    • The module shall reject addresses with special characters or numbers only.
  5. Social Security Number Validation:
    • The module shall validate the social security number input to ensure it is a 9-digit number (XXX-XX-XXXX).
    • The module shall reject social security numbers with invalid formats or lengths.
  6. Error Handling:
    • The module shall raise an exception or return an error message when invalid input is detected.
    • The module shall provide a descriptive error message indicating the specific validation error.

Assumptions and Dependencies:

Acceptance Criteria:

By fulfilling these functional requirements, the data validation module will ensure that only valid and consistent data is stored in the database, reducing errors and inconsistencies in the patient records.

aleeusgr commented 3 months ago

You can add a function to validate the inputs in the patients.clj file. Here's an example of how you can do it:

(defn validate-inputs
  [name sex date-of-birth address social-security-number]
  (let [name-pattern #"[A-Za-z]+ [A-Za-z]+"]
    (if (re-matches name-pattern name)
      true
      (throw (Exception. "Name must be two words containing only letters")))))

(defn add-patient
  [name sex date-of-birth address social-security-number]
  (if (validate-inputs name sex date-of-birth address social-security-number)
    (let [date-of-birth (java.sql.Date/valueOf date-of-birth)]
      (db-patients/insert-patient db/spec
                                   {:name name
                                    :sex sex
                                    :date_of_birth date-of-birth
                                    :address address
                                    :social_security_number social-security-number}))
    (throw (Exception. "Invalid input"))))

In this example, the validate-inputs function checks if the name input matches the regex pattern "[A-Za-z]+ [A-Za-z]+", which requires two words containing only letters. If the input is valid, the add-patient function proceeds to insert the patient into the database. If the input is invalid, an exception is thrown.

You can also add validation for other inputs, such as sex, date-of-birth, address, and social-security-number, by adding more conditions to the validate-inputs function.

Note that this is a simple example, and you may want to add more robust validation and error handling depending on your specific requirements.

aleeusgr commented 3 months ago

Here are some actions to validate user input in your Clojure API:

  1. Use a validation library: Consider using a validation library like clj-validator or clojure.data.json to simplify input validation. These libraries provide a set of built-in validators for common data types like strings, numbers, and dates.
  2. Define validation rules: Define validation rules for each API endpoint based on the expected input data. For example, you can define a rule to ensure that a patient's name is not empty or null.
  3. Use regular expressions: Use regular expressions to validate input data, such as email addresses, phone numbers, or dates.
  4. Check for invalid characters: Check for invalid characters in input data, such as special characters or whitespace.
  5. Validate data types: Validate the data type of input data, such as ensuring that a date is in the correct format.
  6. Check for null or empty values: Check for null or empty values in input data, such as ensuring that a required field is not empty.
  7. Use a schema validation library: Consider using a schema validation library like schema or clj-schema to define a schema for your API endpoints and validate input data against that schema.
  8. Implement input validation in API handlers: Implement input validation in your API handlers, such as in the add-patient function in hs_clojure.patients. For example, you can add a check to ensure that the name parameter is not empty.
  9. Use a validation framework: Consider using a validation framework like clj-validator or clojure.data.json to simplify input validation. These frameworks provide a set of built-in validators and a simple API for defining validation rules.
  10. Test validation rules: Test your validation rules thoroughly to ensure that they are working correctly and that invalid input data is rejected.

Here's an example of how you can implement input validation in your add-patient function:

(defn add-patient
  [name sex date-of-birth address social-security-number]
  (when-not (string? name) (throw (Exception. "Name must be a string")))
  (when-not (re-matches #"[a-zA-Z ]+" sex) (throw (Exception. "Sex must be a string")))
  (when-not (instance? java.sql.Date date-of-birth) (throw (Exception. "Date of birth must be a Date object")))
  (when-not (re-matches #"[a-zA-Z0-9 ]+" address) (throw (Exception. "Address must be a string")))
  (when-not (re-matches #"[0-9]{3}-[0-9]{2}-[0-9]{4}" social-security-number) (throw (Exception. "Social security number must be in the format XXX-XX-XXXX")))
  ...)

In this example, the add-patient function uses when-not to check if the input data is valid. If the input data is invalid, it throws an exception with a descriptive error message.