feat: enhance Notice schema validation and data integrity#135
Open
jikrana1 wants to merge 1 commit into
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
📋 Pull Request — Enhance Notice Schema Validation and Data Integrity
🔗 Related Issue
Closes #130 — Enhance Notice Schema Validation and Data Integrity
📝 Summary
The existing
Noticeschema 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 addingminlength,maxlength,trim, custom error messages, database indexes, and model overwrite protection.🐛 Root Cause
category,content,messagehad notrimtrim: trueto all string fieldstitle,content,message,categoryhad nominlengthminlengthwith custom error messagescategory,targetClass,teacherNamehad nomaxlengthmaxlengthwith custom error messagescategory,content,postedByhad no customrequirederror messagescategory,targetClass,datemongoose.model('Notice', noticeSchema)— could throw overwrite error in dev/hot-reloadmongoose.models.Notice || mongoose.model('Notice', noticeSchema)✅ Changes Made
Modified Files
models/Notice.jstrim,minlength,maxlength, custom error messages to all string fields; added 3 indexes; fixed model overwrite issue🛡️ Validation Changes Summary
titlerequired,trim,maxlength: 200minlength: 3with custom messagecategoryrequired: trueonlytrim,minlength: 2,maxlength: 100, custom messagescontentrequired: trueonlytrim,minlength: 10, custom messagesmessagerequired,trimminlength: 5, custom messagetargetClassdefault: 'All'onlytrim,maxlength: 50teacherNametrim,maxlength: 100postedByrequired: trueonly📈 Indexes Added
category{ category: 1 }targetClass{ targetClass: 1 }date{ date: -1 }🧪 Testing Checklist
titlereturnsTitle is requiredtitleless than 3 chars returnsTitle must be at least 3 characterscategoryreturnsCategory is requiredcontentless than 10 chars returnsContent must be at least 10 charactersmessageless than 5 chars returnsMessage must be at least 5 charactersOverwriteModelErrorin development/hot-reload environmentsdb.notices.getIndexes()💻 How to Test Locally
📌 Coding Standards Followed