@@ -2,6 +2,7 @@ import path from 'path';
2
2
import fs from 'fs-extra' ;
3
3
import axios from 'axios' ;
4
4
import _ from 'lodash' ;
5
+ import Joi from 'joi' ;
5
6
import type { Middleware } from 'koa' ;
6
7
7
8
import config from './config' ;
@@ -60,20 +61,26 @@ export const parseExportData: Middleware<State> = (ctx, next) => {
60
61
export function validateExportData ( data : PageData ) {
61
62
if ( ! _ . isObject ( data ) ) throw new Error ( 'data should be an object' ) ;
62
63
63
- if ( ! _ . isInteger ( data . semester ) || data . semester < 1 || data . semester > 4 ) {
64
- throw new Error ( 'Invalid semester' ) ;
65
- }
66
-
67
- // TODO: Improve these validation
68
- if ( ! _ . isObject ( data . timetable ) ) {
69
- throw new Error ( 'Invalid timetable' ) ;
70
- }
71
-
72
- if ( ! _ . isObject ( data . settings ) ) {
73
- throw new Error ( 'Invalid settings' ) ;
74
- }
64
+ const timetableSchema = Joi . object ( ) . pattern (
65
+ Joi . string ( ) ,
66
+ Joi . object ( ) . pattern ( Joi . string ( ) , Joi . string ( ) ) ,
67
+ ) ;
68
+ const themeSchema = Joi . object ( {
69
+ id : Joi . string ( ) ,
70
+ timetableOrientation : Joi . string ( ) . valid ( 'HORIZONTAL' , 'VERTICAL' ) ,
71
+ showTitle : Joi . boolean ( ) ,
72
+ } ) ;
73
+ const pageDataSchema = Joi . object ( {
74
+ semester : Joi . number ( ) . integer ( ) . greater ( 0 ) . less ( 5 ) ,
75
+ timetable : timetableSchema ,
76
+ settings : Joi . object ( {
77
+ hiddenInTimeTable : Joi . array ( ) . items ( Joi . string ( ) ) ,
78
+ } ) ,
79
+ theme : themeSchema ,
80
+ } ) ;
75
81
76
- if ( ! _ . isObject ( data . theme ) ) {
77
- throw new Error ( 'Invalid theme' ) ;
82
+ const result = pageDataSchema . validate ( data , { allowUnknown : true } ) ;
83
+ if ( result . error !== undefined ) {
84
+ throw new Error ( JSON . stringify ( result . error ) ) ;
78
85
}
79
86
}
0 commit comments