|
| 1 | +const Joi = require('joi'); |
| 2 | +const db = require('./../db'); |
| 3 | + |
| 4 | +// music schema |
| 5 | +const musicSchema = Joi.object().keys({ |
| 6 | + title: Joi.string() |
| 7 | + .min(1) |
| 8 | + .required(), |
| 9 | + year: Joi.number() |
| 10 | + .min(1650) |
| 11 | + .max(new Date().getFullYear() + 5), |
| 12 | + description: Joi.string().optional(), |
| 13 | + musicReleaseType: Joi.string().length(26) |
| 14 | +}); |
| 15 | + |
| 16 | +// music info schema |
| 17 | +const musicInfoSchema = Joi.object().keys({ |
| 18 | + title: Joi.string() |
| 19 | + .min(1) |
| 20 | + .required(), |
| 21 | + description: Joi.string().optional(), |
| 22 | + encoding: Joi.string().min(1), |
| 23 | + quality: Joi.string().length(26) |
| 24 | +}); |
| 25 | + |
| 26 | +const validateMusic = async ({ |
| 27 | + release, |
| 28 | + musicReleaseTypes, |
| 29 | + musicQualities |
| 30 | +}) => { |
| 31 | + // verifiy release.music |
| 32 | + if (release.music instanceof Object) { |
| 33 | + const { error: musicError } = Joi.validate(release.music, musicSchema); |
| 34 | + if (musicError !== null) { |
| 35 | + // throw new Error('Invalid music object.', musicError); |
| 36 | + throw new Error(musicError); |
| 37 | + } |
| 38 | + if ( |
| 39 | + !musicReleaseTypes |
| 40 | + .map((rt) => rt.id) |
| 41 | + .includes(release.music.musicReleaseType) |
| 42 | + ) { |
| 43 | + throw new Error('Music entry contains invalid release type.'); |
| 44 | + } |
| 45 | + } else { |
| 46 | + const music = await db.getMusic(release.music); |
| 47 | + if (music.rows.length !== 1) { |
| 48 | + throw new Error('Invalid music id.'); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + // verifiy release.info |
| 53 | + const { error: musicInfoError } = Joi.validate(release.info, musicInfoSchema); |
| 54 | + if (musicInfoError !== null) { |
| 55 | + // throw new Error('Invalid music info.', musicInfoError); |
| 56 | + throw new Error(musicInfoError); |
| 57 | + } |
| 58 | + if (!musicQualities.map((mq) => mq.id).includes(release.info.quality)) { |
| 59 | + throw new Error('Info contains invalid quality.'); |
| 60 | + } |
| 61 | +}; |
| 62 | + |
| 63 | +const animeSchema = Joi.object().keys({ |
| 64 | + name: Joi.string() |
| 65 | + .min(1) |
| 66 | + .required(), |
| 67 | + description: Joi.string().optional(), |
| 68 | + season: Joi.string().optional(), |
| 69 | + episode: Joi.string().optional() |
| 70 | +}); |
| 71 | + |
| 72 | +const TVShowSchema = animeSchema; |
| 73 | + |
| 74 | +const validateAnime = async ({ release }) => { |
| 75 | + if (release.anime instanceof Object) { |
| 76 | + const { error: animeError } = Joi.validate(release.anime, animeSchema); |
| 77 | + if (animeError !== null) { |
| 78 | + // throw new Error('Invalid music object.', musicError); |
| 79 | + throw new Error(animeError); |
| 80 | + } |
| 81 | + } else { |
| 82 | + const anime = await db.getAnime(release.anime); |
| 83 | + if (anime.rows.length !== 1) { |
| 84 | + throw new Error('Invalid music id.'); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + const { error: animeInfoError } = Joi.validate(release.info, videoInfoSchema); |
| 89 | + if (animeInfoError !== null) { |
| 90 | + // throw new Error('Invalid music object.', musicError); |
| 91 | + throw new Error(animeInfoError); |
| 92 | + } |
| 93 | +}; |
| 94 | + |
| 95 | +const videoInfoSchema = Joi.object().keys({ |
| 96 | + title: Joi.string() |
| 97 | + .min(1) |
| 98 | + .required(), |
| 99 | + description: Joi.string().optional(), |
| 100 | + quality: Joi.string().length(26) |
| 101 | +}); |
| 102 | + |
| 103 | +const movieSchema = Joi.object().keys({ |
| 104 | + name: Joi.string() |
| 105 | + .min(1) |
| 106 | + .required(), |
| 107 | + description: Joi.string().optional(), |
| 108 | + year: Joi.number().required() |
| 109 | +}); |
| 110 | + |
| 111 | +const validateMovie = async ({ release }) => { |
| 112 | + if (release.movie instanceof Object) { |
| 113 | + const { error: movieError } = Joi.validate(release.movie, movieSchema); |
| 114 | + if (movieError !== null) { |
| 115 | + // throw new Error('Invalid music object.', musicError); |
| 116 | + throw new Error(movieError); |
| 117 | + } |
| 118 | + } else { |
| 119 | + const movie = await db.getMovie(release.movie); |
| 120 | + if (movie.rows.length !== 1) { |
| 121 | + throw new Error('Invalid movie id.'); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + const { error: movieInfoError } = Joi.validate(release.info, videoInfoSchema); |
| 126 | + if (movieInfoError !== null) { |
| 127 | + // throw new Error('Invalid music object.', musicError); |
| 128 | + throw new Error(movieInfoError); |
| 129 | + } |
| 130 | +}; |
| 131 | + |
| 132 | +const validateTV = async ({ release }) => { |
| 133 | + if (release.tv instanceof Object) { |
| 134 | + const { error: tvError } = Joi.validate(release.tv, TVShowSchema); |
| 135 | + if (tvError !== null) { |
| 136 | + // throw new Error('Invalid music object.', musicError); |
| 137 | + throw new Error(tvError); |
| 138 | + } |
| 139 | + } else { |
| 140 | + const tv = await db.getAnime(release.tv); |
| 141 | + if (tv.rows.length !== 1) { |
| 142 | + throw new Error('Invalid TV id.'); |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + const { error: movieInfoError } = Joi.validate(release.info, videoInfoSchema); |
| 147 | + if (movieInfoError !== null) { |
| 148 | + // throw new Error('Invalid music object.', musicError); |
| 149 | + throw new Error(movieInfoError); |
| 150 | + } |
| 151 | +}; |
| 152 | + |
| 153 | +const softwareSchema = Joi.object().keys({ |
| 154 | + name: Joi.string() |
| 155 | + .min(1) |
| 156 | + .required(), |
| 157 | + description: Joi.string().optional(), |
| 158 | + operatingSystem: Joi.string().optional() |
| 159 | +}); |
| 160 | + |
| 161 | +const validateSoftware = async ({ release }) => { |
| 162 | + const { error: softwareError } = Joi.validate( |
| 163 | + release.software, |
| 164 | + softwareSchema |
| 165 | + ); |
| 166 | + if (softwareError !== null) { |
| 167 | + // throw new Error('Invalid music object.', musicError); |
| 168 | + throw new Error(softwareError); |
| 169 | + } |
| 170 | +}; |
| 171 | + |
| 172 | +const artistSchema = Joi.object() |
| 173 | + .keys({ |
| 174 | + name: Joi.string() |
| 175 | + .min(1) |
| 176 | + .optional(), |
| 177 | + description: Joi.string().optional(), |
| 178 | + primary: Joi.boolean().required(), |
| 179 | + id: Joi.string().length(26) |
| 180 | + }) |
| 181 | + .with('name', 'description') |
| 182 | + .without('id', 'name') |
| 183 | + .without('id', 'description'); |
| 184 | + |
| 185 | +const validateArtist = async (artist) => { |
| 186 | + const { error: artistError } = Joi.validate(artist, artistSchema); |
| 187 | + if (artistError !== null) { |
| 188 | + throw new Error(artistError); |
| 189 | + } |
| 190 | + // make sure ids are valid |
| 191 | + if (Object.prototype.hasOwnProperty.call(artist, 'id')) { |
| 192 | + const anime = await db.getArtist(artist.id); |
| 193 | + if (anime.rows.length !== 1) { |
| 194 | + throw new Error('Invalid artist id.'); |
| 195 | + } |
| 196 | + } |
| 197 | +}; |
| 198 | + |
| 199 | +module.exports = { |
| 200 | + validateAnime, |
| 201 | + validateArtist, |
| 202 | + validateMovie, |
| 203 | + validateMusic, |
| 204 | + validateSoftware, |
| 205 | + validateTV |
| 206 | +}; |
0 commit comments