|
@@ -4,6 +4,18 @@ const mongoose = require('mongoose');
|
|
|
|
|
|
const bluebird = require('bluebird');
|
|
|
|
|
|
+const regex = {
|
|
|
+ azAZ09_: /^[A-Za-z0-9_]+$/,
|
|
|
+ az09_: /^[a-z0-9_]+$/,
|
|
|
+ emailSimple: /^[\x00-\x7F]+@[a-z0-9]+\.[a-z0-9]+(\.[a-z0-9]+)?$/,
|
|
|
+ password: /[a-z]+[A-Z]+[0-9]+[^a-zA-Z0-9]+/,
|
|
|
+ ascii: /^[\x00-\x7F]+$/
|
|
|
+};
|
|
|
+
|
|
|
+const isLength = (string, min, max) => {
|
|
|
+ return !(typeof string !== 'string' || string.length < min || string.length > max);
|
|
|
+}
|
|
|
+
|
|
|
mongoose.Promise = bluebird;
|
|
|
|
|
|
let lib = {
|
|
@@ -33,9 +45,69 @@ let lib = {
|
|
|
report: new mongoose.Schema(require(`./schemas/report`))
|
|
|
};
|
|
|
|
|
|
+ lib.schemas.user.path('username').validate((username) => {
|
|
|
+ return (isLength(username, 2, 32) && regex.azAZ09_.test(username));
|
|
|
+ }, 'Invalid username.');
|
|
|
+
|
|
|
+ lib.schemas.user.path('email.address').validate((email) => {
|
|
|
+ if (!isLength(email, 3, 254)) return false;
|
|
|
+ if (email.indexOf('@') !== email.lastIndexOf('@')) return false;
|
|
|
+ return regex.emailSimple.test(email);
|
|
|
+ }, 'Invalid email.');
|
|
|
+
|
|
|
lib.schemas.station.path('name').validate((id) => {
|
|
|
- return /^[a-z]+$/.test(id);
|
|
|
- }, 'The id can only have the letters a-z.');
|
|
|
+ return (isLength(id, 2, 16) && regex.az09_.test(id));
|
|
|
+ }, 'Invalid station name.');
|
|
|
+
|
|
|
+ lib.schemas.station.path('displayName').validate((displayName) => {
|
|
|
+ return (isLength(displayName, 2, 32) && regex.azAZ09_.test(displayName));
|
|
|
+ }, 'Invalid display name.');
|
|
|
+
|
|
|
+ lib.schemas.station.path('description').validate((description) => {
|
|
|
+ if (!isLength(description, 2, 200)) return false;
|
|
|
+ let characters = description.split("");
|
|
|
+ return characters.filter((character) => {
|
|
|
+ console.log(character.charCodeAt(0), character.charCodeAt(0) === 21328);
|
|
|
+ return character.charCodeAt(0) === 21328;
|
|
|
+ }).length === 0;
|
|
|
+ }, 'Invalid display name.');
|
|
|
+
|
|
|
+ let songTitle = (title) => {
|
|
|
+ return (isLength(title, 1, 64) && regex.ascii.test(title));
|
|
|
+ };
|
|
|
+ lib.schemas.song.path('title').validate(songTitle, 'Invalid title.');
|
|
|
+ lib.schemas.queueSong.path('title').validate(songTitle, 'Invalid title.');
|
|
|
+
|
|
|
+ let songArtists = (artists) => {
|
|
|
+ if (artists.length < 1 || artists.length > 10) return false;
|
|
|
+ return artists.filter((artist) => {
|
|
|
+ return (isLength(artist, 1, 32) && regex.ascii.test(artist) && artist !== "NONE");
|
|
|
+ }).length === artists.length;
|
|
|
+ };
|
|
|
+ lib.schemas.song.path('artists').validate(songArtists, 'Invalid artists.');
|
|
|
+ lib.schemas.queueSong.path('artists').validate(songArtists, 'Invalid artists.');
|
|
|
+
|
|
|
+ let songGenres = (genres) => {
|
|
|
+ return genres.filter((genre) => {
|
|
|
+ return (isLength(genre, 1, 16) && regex.az09_.test(genre));
|
|
|
+ }).length === genres.length;
|
|
|
+ };
|
|
|
+ lib.schemas.song.path('genres').validate(songGenres, 'Invalid genres.');
|
|
|
+ lib.schemas.queueSong.path('genres').validate(songGenres, 'Invalid genres.');
|
|
|
+
|
|
|
+ let songThumbnail = (thumbnail) => {
|
|
|
+ return isLength(thumbnail, 8, 256);
|
|
|
+ };
|
|
|
+ lib.schemas.song.path('thumbnail').validate(songThumbnail, 'Invalid thumbnail.');
|
|
|
+ lib.schemas.queueSong.path('thumbnail').validate(songThumbnail, 'Invalid thumbnail.');
|
|
|
+
|
|
|
+ lib.schemas.playlist.path('displayName').validate((displayName) => {
|
|
|
+ return (isLength(displayName, 1, 16) && regex.ascii.test(displayName));
|
|
|
+ }, 'Invalid display name.');
|
|
|
+
|
|
|
+ lib.schemas.report.path('description').validate((description) => {
|
|
|
+ return (!description || (isLength(description, 0, 400) && regex.ascii.test(description)));
|
|
|
+ }, 'Invalid description.');
|
|
|
|
|
|
lib.models = {
|
|
|
song: mongoose.model('song', lib.schemas.song),
|
|
@@ -49,6 +121,11 @@ let lib = {
|
|
|
|
|
|
cb();
|
|
|
});
|
|
|
+ },
|
|
|
+
|
|
|
+ passwordValid: (password) => {
|
|
|
+ if (!isLength(password, 6, 200)) return false;
|
|
|
+ return regex.password.test(password);
|
|
|
}
|
|
|
};
|
|
|
|