Skip to content

Commit 92e42db

Browse files
committed
core: allow empty pretest input
1 parent 5f38d63 commit 92e42db

2 files changed

Lines changed: 13 additions & 12 deletions

File tree

framework/framework/validator.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export interface Types {
6161
CommaSeperatedArray: Type<string[]>;
6262
Set: Type<Set<any>>;
6363
Any: Type<any>;
64-
ArrayOf: <T extends Type<any>>(type: T) => (T extends Type<infer R> ? Type<R[]> : never);
64+
ArrayOf: <T extends Type<any>>(type: T, isOptional?: boolean) => (T extends Type<infer R> ? Type<R[]> : never);
6565
AnyOf: <T extends Type<any>>(...type: T[]) => (T extends Type<infer R> ? Type<R> : never);
6666
}
6767

@@ -175,15 +175,21 @@ export const Types = {
175175
(v) => emojiRegex().test(v.toString()),
176176
],
177177
Any: [(v) => v, null],
178-
ArrayOf: (type) => [
178+
ArrayOf: (type, isOptional = false) => [
179179
(v) => {
180180
const arr = v instanceof Array ? v : [v];
181-
return arr.map((i) => type[0](i));
181+
return arr.map((i) => {
182+
if (isOptional && [undefined, null, ''].includes(i)) return undefined;
183+
return type[0](i);
184+
});
182185
},
183186
(v) => {
184187
if (!type[1]) return true;
185188
const arr = v instanceof Array ? v : [v];
186-
return arr.every((i) => type[1](i));
189+
return arr.every((i) => {
190+
if ([undefined, null, ''].includes(i)) return isOptional;
191+
return type[1](i);
192+
});
187193
},
188194
] as any,
189195
AnyOf: (...types) => [
@@ -195,10 +201,4 @@ export const Types = {
195201
try {
196202
const { ObjectId } = require('mongodb');
197203
Types.ObjectId = [((v) => new ObjectId(v)) as any, ObjectId.isValid];
198-
} catch (e) {
199-
200-
}
201-
202-
// @ts-ignore
203-
Types.ObjectID = Types.ObjectId;
204-
// backward compatibility
204+
} catch (e) { }

packages/hydrooj/src/handler/problem.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,7 @@ export class ProblemSubmitHandler extends ProblemDetailHandler {
483483
@param('lang', Types.Name)
484484
@param('code', Types.String, true)
485485
@param('pretest', Types.Boolean)
486-
@param('input', Types.ArrayOf(Types.String), true)
486+
@param('input', Types.ArrayOf(Types.String, true), true)
487487
@param('tid', Types.ObjectId, true)
488488
async post(domainId: string, lang: string, code: string, pretest = false, input: string[] = [], tid?: ObjectId) {
489489
const config = this.pdoc.config;
@@ -499,6 +499,7 @@ export class ProblemSubmitHandler extends ProblemDetailHandler {
499499
throw new ProblemNotAllowPretestError('type');
500500
}
501501
if (!input.length) throw new ValidationError('input');
502+
input = input.map((i) => i || '');
502503
}
503504
await this.limitRate('add_record', 60, system.get('limit.submission_user'), '{{user}}');
504505
await this.limitRate('add_record', 60, pretest ? system.get('limit.pretest') : system.get('limit.submission'));

0 commit comments

Comments
 (0)