Skip to content

feat: enhance Notice schema validation and data integrity#135

Open
jikrana1 wants to merge 1 commit into
Sitaram8472:mainfrom
jikrana1:feature/application-schema-validation
Open

feat: enhance Notice schema validation and data integrity#135
jikrana1 wants to merge 1 commit into
Sitaram8472:mainfrom
jikrana1:feature/application-schema-validation

Conversation

@jikrana1

Copy link
Copy Markdown
Contributor

📋 Pull Request — Enhance Notice Schema Validation and Data Integrity

🔗 Related Issue

Closes #130Enhance Notice Schema Validation and Data Integrity


📝 Summary

The existing Notice schema validated only a few fields and lacked proper sanitization and validation for several string fields. This allowed empty or improperly formatted data to be stored in the database. This PR improves the Notice schema by adding minlength, maxlength, trim, custom error messages, database indexes, and model overwrite protection.


🐛 Root Cause

# Problem Fix Applied
1 category, content, message had no trim Added trim: true to all string fields
2 title, content, message, category had no minlength Added minlength with custom error messages
3 category, targetClass, teacherName had no maxlength Added maxlength with custom error messages
4 category, content, postedBy had no custom required error messages Added descriptive custom messages to all required fields
5 No indexes on frequently queried fields Added indexes on category, targetClass, date
6 mongoose.model('Notice', noticeSchema) — could throw overwrite error in dev/hot-reload Changed to mongoose.models.Notice || mongoose.model('Notice', noticeSchema)

✅ Changes Made

Modified Files

File What Changed
models/Notice.js Added trim, minlength, maxlength, custom error messages to all string fields; added 3 indexes; fixed model overwrite issue

🛡️ Validation Changes Summary

Field Before After
title required, trim, maxlength: 200 + minlength: 3 with custom message
category required: true only + trim, minlength: 2, maxlength: 100, custom messages
content required: true only + trim, minlength: 10, custom messages
message required, trim + minlength: 5, custom message
targetClass default: 'All' only + trim, maxlength: 50
teacherName No validation + trim, maxlength: 100
postedBy required: true only + custom required error message

📈 Indexes Added

Field Index Reason
category { category: 1 } Frequently filtered by category
targetClass { targetClass: 1 } Frequently filtered by class
date { date: -1 } Notices usually sorted by latest date

🧪 Testing Checklist

  • Creating a notice without title returns Title is required
  • Creating a notice with title less than 3 chars returns Title must be at least 3 characters
  • Creating a notice without category returns Category is required
  • Creating a notice with content less than 10 chars returns Content must be at least 10 characters
  • Creating a notice with message less than 5 chars returns Message must be at least 5 characters
  • String fields are trimmed before saving (no leading/trailing spaces stored)
  • Valid notice saves successfully with no errors
  • No OverwriteModelError in development/hot-reload environments
  • Indexes visible in MongoDB Atlas or via db.notices.getIndexes()

💻 How to Test Locally

# Start server
npm run dev

# Test validation via API
curl -X POST http://localhost:5000/api/notices \
  -H "Content-Type: application/json" \
  -d '{"title": "Hi", "category": "", "content": "short"}'
# Expected: validation error responses

📌 Coding Standards Followed

  • Mongoose built-in validators only — no external libraries added
  • Custom error messages on all validators for better API error responses
  • Indexes defined at schema level (not manually in DB)
  • Model overwrite protection consistent with Next.js/hot-reload best practices
  • No change to controller or route logic

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature] :Enhance Notice Schema Validation and Data Integrity

1 participant