aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMateja <mail@matejamaric.com>2021-05-08 21:20:42 +0200
committerMateja <mail@matejamaric.com>2021-05-08 21:20:42 +0200
commit766b60adf81664c59bda39ebc5350af67e325f4e (patch)
tree772ac45804d47a6bc3f0e2740d2bd4fbdbc71cff
parent0a95a55d5317b1ca0be54b226834f9433365717d (diff)
downloadnode-playground-766b60adf81664c59bda39ebc5350af67e325f4e.tar.gz
node-playground-766b60adf81664c59bda39ebc5350af67e325f4e.zip
Added more validation.
-rw-r--r--models/post.js16
-rw-r--r--models/user.js37
-rw-r--r--package-lock.json5
-rw-r--r--package.json3
4 files changed, 53 insertions, 8 deletions
diff --git a/models/post.js b/models/post.js
index 8227c85..f01431b 100644
--- a/models/post.js
+++ b/models/post.js
@@ -1,9 +1,19 @@
const mongoose = require('mongoose');
const postSchema = new mongoose.Schema({
- title: {type: String, required: true},
- text: {type: String, required: true},
- date: {type: Date, default: Date.now}
+ title: {
+ type: String,
+ required: [true, "You need to provide a title."],
+ maxLength: [100, "You can't have a title more than 100 characters long."]
+ },
+ text: {
+ type: String,
+ required: [true, "You need to provide some text."]
+ },
+ date: {
+ type: Date,
+ default: Date.now
+ }
});
const postModel = mongoose.model('post', postSchema);
diff --git a/models/user.js b/models/user.js
index 3751aae..525e5df 100644
--- a/models/user.js
+++ b/models/user.js
@@ -1,10 +1,39 @@
const mongoose = require('mongoose');
+const validator = require('validator');
const userSchema = new mongoose.Schema({
- firstname: {type: String, required: true},
- lastname: {type: String, required: true},
- email: {type: String, required: true},
- password: {type: String, required: true},
+ firstname: {
+ type: String,
+ required: [true, "You need to provide your first name."],
+ maxLength: [32, "You can't have a first name longer than 32 characters."],
+ validate: {
+ validator: validator.isAlpha,
+ message: props => "Your first name can only contain characters."
+ }
+ },
+ lastname: {
+ type: String,
+ required: [true, "You need to provide your last name."],
+ maxLength: [32, "You can't have a last name longer than 32 characters."],
+ validate: {
+ validator: validator.isAlpha,
+ message: props => "Your last name can only contain characters."
+ }
+ },
+ email: {
+ type: String,
+ required: [true, "You need to provide an email."],
+ maxLength: [100, "You can't have a email longer than 100 characters."],
+ validate: {
+ validator: validator.isEmail,
+ message: props => "Email is not valid."
+ }
+ },
+ password: {
+ type: String,
+ required: [true, "You need to provide a password."],
+ maxLength: [100, "You can't have a password longer than 100 characters."]
+ },
});
const userModel = new mongoose.model('user', userSchema);
diff --git a/package-lock.json b/package-lock.json
index 63ce440..b718689 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -1748,6 +1748,11 @@
"resolved": "https://registry.npmjs.org/utils-merge/-/utils-merge-1.0.1.tgz",
"integrity": "sha1-n5VxD1CiZ5R7LMwSR0HBAoQn5xM="
},
+ "validator": {
+ "version": "13.6.0",
+ "resolved": "https://registry.npmjs.org/validator/-/validator-13.6.0.tgz",
+ "integrity": "sha512-gVgKbdbHgtxpRyR8K0O6oFZPhhB5tT1jeEHZR0Znr9Svg03U0+r9DXWMrnRAB+HtCStDQKlaIZm42tVsVjqtjg=="
+ },
"vary": {
"version": "1.1.2",
"resolved": "https://registry.npmjs.org/vary/-/vary-1.1.2.tgz",
diff --git a/package.json b/package.json
index 69b6f84..ea42da4 100644
--- a/package.json
+++ b/package.json
@@ -19,7 +19,8 @@
"express-session": "^1.17.1",
"mongoose": "^5.12.7",
"passport": "^0.4.1",
- "passport-local": "^1.0.0"
+ "passport-local": "^1.0.0",
+ "validator": "^13.6.0"
},
"devDependencies": {
"nodemon": "^2.0.7"