Skip to content

Commit 6ca6b25

Browse files
committed
yup validation on config file
1 parent 9414c08 commit 6ca6b25

4 files changed

Lines changed: 113 additions & 1 deletion

File tree

api/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
"node-fetch": "^2.6.1",
2626
"nodemailer": "^6.7.8",
2727
"qs": "^6.11.0",
28-
"slugify": "^1.6.5"
28+
"slugify": "^1.6.5",
29+
"yup": "^0.32.11"
2930
},
3031
"devDependencies": {
3132
"@babel/core": "^7.14.6",

api/src/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,11 @@ import {
5050
getStats,
5151
} from './controllers/moderation'
5252
import { rssFeed } from './controllers/rss'
53+
import validateConfig from './utils/validateConfig'
5354
import createAdminUser from './setup/createAdminUser'
5455

56+
validateConfig()
57+
5558
const connectToDb = () => {
5659
console.log('[sq] initiating db connection...')
5760
mongoose

api/src/utils/validateConfig.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import * as yup from 'yup'
2+
import config from '../../../config'
3+
4+
const configSchema = yup
5+
.object({
6+
envs: yup
7+
.object({
8+
SQ_SITE_NAME: yup.string().min(1).max(20).required(),
9+
SQ_SITE_DESCRIPTION: yup.string().min(1).max(80).required(),
10+
SQ_ALLOW_REGISTER: yup
11+
.string()
12+
.oneOf(['open', 'invite', 'closed'])
13+
.required(),
14+
SQ_ALLOW_ANONYMOUS_UPLOADS: yup.boolean().required(),
15+
SQ_MINIMUM_RATIO: yup.number().min(0).required(),
16+
SQ_TORRENT_CATEGORIES: yup
17+
.array()
18+
.of(yup.string())
19+
.min(1)
20+
.test('items-unique', 'Categories must be unique', (value) =>
21+
value.every(
22+
(category) => value.filter((c) => c === category).length === 1
23+
)
24+
)
25+
.required(),
26+
SQ_BASE_URL: yup.string().required(),
27+
SQ_API_URL: yup.string().required(),
28+
SQ_TRACKER_URL: yup.string().required(),
29+
SQ_MONGO_URL: yup.string().required(),
30+
SQ_MAIL_FROM_ADDRESS: yup.string().email().required(),
31+
SQ_SMTP_HOST: yup.string().required(),
32+
SQ_SMTP_PORT: yup.number().integer().min(1).max(65535).required(),
33+
SQ_SMTP_SECURE: yup.boolean().required(),
34+
})
35+
.strict()
36+
.noUnknown()
37+
.required(),
38+
secrets: yup
39+
.object({
40+
SQ_JWT_SECRET: yup.string().required(),
41+
SQ_ADMIN_EMAIL: yup.string().email().required(),
42+
SQ_SMTP_USER: yup.string().required(),
43+
SQ_SMTP_PASS: yup.string().required(),
44+
})
45+
.strict()
46+
.noUnknown()
47+
.required(),
48+
})
49+
.strict()
50+
.noUnknown()
51+
.required()
52+
53+
const validateConfig = async () => {
54+
try {
55+
await configSchema.validate(config)
56+
console.log('[sq] configuration is valid')
57+
} catch (e) {
58+
console.error('[sq] ERROR: invalid configuration:', e.message)
59+
process.exit(1)
60+
}
61+
}
62+
63+
export default validateConfig

yarn.lock

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -910,6 +910,13 @@
910910
dependencies:
911911
regenerator-runtime "^0.13.4"
912912

913+
"@babel/runtime@^7.15.4":
914+
version "7.19.0"
915+
resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.19.0.tgz#22b11c037b094d27a8a2504ea4dcff00f50e2259"
916+
integrity sha512-eR8Lo9hnDS7tqkO7NsV+mKvCmv5boaXFSZ70DnfhcgiEne8hv9oCEd36Klw74EtizEqLsy4YnW8UWwpBVolHZA==
917+
dependencies:
918+
regenerator-runtime "^0.13.4"
919+
913920
"@babel/template@^7.16.0":
914921
version "7.16.0"
915922
resolved "https://registry.yarnpkg.com/@babel/template/-/template-7.16.0.tgz#d16a35ebf4cd74e202083356fab21dd89363ddd6"
@@ -1313,6 +1320,11 @@
13131320
resolved "https://registry.yarnpkg.com/@types/json5/-/json5-0.0.29.tgz#ee28707ae94e11d2b827bcbe5270bcea7f3e71ee"
13141321
integrity sha1-7ihweulOEdK4J7y+UnC86n8+ce4=
13151322

1323+
"@types/lodash@^4.14.175":
1324+
version "4.14.185"
1325+
resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.185.tgz#c9843f5a40703a8f5edfd53358a58ae729816908"
1326+
integrity sha512-evMDG1bC4rgQg4ku9tKpuMh5iBNEwNa3tf9zRHdP1qlv+1WUg44xat4IxCE14gIpZRGUUWAx2VhItCZc25NfMA==
1327+
13161328
"@types/mdast@^3.0.0", "@types/mdast@^3.0.3":
13171329
version "3.0.10"
13181330
resolved "https://registry.yarnpkg.com/@types/mdast/-/mdast-3.0.10.tgz#4724244a82a4598884cbbe9bcfd73dff927ee8af"
@@ -3609,6 +3621,11 @@ locate-path@^3.0.0:
36093621
p-locate "^3.0.0"
36103622
path-exists "^3.0.0"
36113623

3624+
lodash-es@^4.17.21:
3625+
version "4.17.21"
3626+
resolved "https://registry.yarnpkg.com/lodash-es/-/lodash-es-4.17.21.tgz#43e626c46e6591b7750beb2b50117390c609e3ee"
3627+
integrity sha512-mKnC+QJ9pWVzv+C4/U3rRsHapFfHvQFoFB92e52xeyGMcX6/OlIl78je1u8vePzYZSkkogMPJ2yjxxsb89cxyw==
3628+
36123629
lodash.debounce@^4.0.8:
36133630
version "4.0.8"
36143631
resolved "https://registry.yarnpkg.com/lodash.debounce/-/lodash.debounce-4.0.8.tgz#82d79bff30a67c4005ffd5e2515300ad9ca4d7af"
@@ -4345,6 +4362,11 @@ multer@^1.4.2:
43454362
type-is "^1.6.4"
43464363
xtend "^4.0.0"
43474364

4365+
nanoclone@^0.2.1:
4366+
version "0.2.1"
4367+
resolved "https://registry.yarnpkg.com/nanoclone/-/nanoclone-0.2.1.tgz#dd4090f8f1a110d26bb32c49ed2f5b9235209ed4"
4368+
integrity sha512-wynEP02LmIbLpcYw8uBKpcfF6dmg2vcpKqxeH5UcoKEYdExslsdUA4ugFauuaeYdTB76ez6gJW8XAZ6CgkXYxA==
4369+
43484370
nanoid@^3.3.4:
43494371
version "3.3.4"
43504372
resolved "https://registry.yarnpkg.com/nanoid/-/nanoid-3.3.4.tgz#730b67e3cd09e2deacf03c027c81c9d9dbc5e8ab"
@@ -4865,6 +4887,11 @@ prop-types@^15.7.2:
48654887
object-assign "^4.1.1"
48664888
react-is "^16.8.1"
48674889

4890+
property-expr@^2.0.4:
4891+
version "2.0.5"
4892+
resolved "https://registry.yarnpkg.com/property-expr/-/property-expr-2.0.5.tgz#278bdb15308ae16af3e3b9640024524f4dc02cb4"
4893+
integrity sha512-IJUkICM5dP5znhCckHSv30Q4b5/JA5enCtkRHYaOVOAocnH/1BQEYTC5NMfT3AVl/iXKdr3aqQbQn9DxyWknwA==
4894+
48684895
property-information@^6.0.0:
48694896
version "6.1.1"
48704897
resolved "https://registry.yarnpkg.com/property-information/-/property-information-6.1.1.tgz#5ca85510a3019726cb9afed4197b7b8ac5926a22"
@@ -5707,6 +5734,11 @@ toidentifier@1.0.0:
57075734
resolved "https://registry.yarnpkg.com/toidentifier/-/toidentifier-1.0.0.tgz#7e1be3470f1e77948bc43d94a3c8f4d7752ba553"
57085735
integrity sha512-yaOH/Pk/VEhBWWTlhI+qXxDFXlejDGcQipMlyxda9nthulaxLZUNcUqFxokp0vcYnvteJln5FNQDRrxj3YcbVw==
57095736

5737+
toposort@^2.0.2:
5738+
version "2.0.2"
5739+
resolved "https://registry.yarnpkg.com/toposort/-/toposort-2.0.2.tgz#ae21768175d1559d48bef35420b2f4962f09c330"
5740+
integrity sha512-0a5EOkAUp8D4moMi2W8ZF8jcga7BgZd91O/yabJCFY8az+XSzeGyTKs0Aoo897iV1Nj6guFq8orWDS96z91oGg==
5741+
57105742
touch@^3.1.0:
57115743
version "3.1.0"
57125744
resolved "https://registry.yarnpkg.com/touch/-/touch-3.1.0.tgz#fe365f5f75ec9ed4e56825e0bb76d24ab74af83b"
@@ -6130,6 +6162,19 @@ yallist@^4.0.0:
61306162
resolved "https://registry.yarnpkg.com/yallist/-/yallist-4.0.0.tgz#9bb92790d9c0effec63be73519e11a35019a3a72"
61316163
integrity sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A==
61326164

6165+
yup@^0.32.11:
6166+
version "0.32.11"
6167+
resolved "https://registry.yarnpkg.com/yup/-/yup-0.32.11.tgz#d67fb83eefa4698607982e63f7ca4c5ed3cf18c5"
6168+
integrity sha512-Z2Fe1bn+eLstG8DRR6FTavGD+MeAwyfmouhHsIUgaADz8jvFKbO/fXc2trJKZg+5EBjh4gGm3iU/t3onKlXHIg==
6169+
dependencies:
6170+
"@babel/runtime" "^7.15.4"
6171+
"@types/lodash" "^4.14.175"
6172+
lodash "^4.17.21"
6173+
lodash-es "^4.17.21"
6174+
nanoclone "^0.2.1"
6175+
property-expr "^2.0.4"
6176+
toposort "^2.0.2"
6177+
61336178
zwitch@^2.0.0:
61346179
version "2.0.2"
61356180
resolved "https://registry.yarnpkg.com/zwitch/-/zwitch-2.0.2.tgz#91f8d0e901ffa3d66599756dde7f57b17c95dce1"

0 commit comments

Comments
 (0)