summaryrefslogtreecommitdiff
path: root/src/api
diff options
context:
space:
mode:
Diffstat (limited to 'src/api')
-rw-r--r--src/api/endpoints/posts/likes.ts (renamed from src/api/endpoints/posts/likes.js)30
-rw-r--r--src/api/it.ts24
2 files changed, 32 insertions, 22 deletions
diff --git a/src/api/endpoints/posts/likes.js b/src/api/endpoints/posts/likes.ts
index 67898218cf..5c18679d7c 100644
--- a/src/api/endpoints/posts/likes.js
+++ b/src/api/endpoints/posts/likes.ts
@@ -3,7 +3,7 @@
/**
* Module dependencies
*/
-import * as mongo from 'mongodb';
+import it from '../../it';
import Post from '../../models/post';
import Like from '../../models/like';
import serialize from '../../serializers/user';
@@ -19,33 +19,19 @@ module.exports = (params, user) =>
new Promise(async (res, rej) =>
{
// Get 'post_id' parameter
- const postId = params.post_id;
- if (postId === undefined || postId === null) {
- return rej('post_id is required');
- }
+ const [postId, postIdErr] = it(params.post_id, 'id', true);
+ if (postIdErr) return rej('invalid post_id');
// Get 'limit' parameter
- let limit = params.limit;
- if (limit !== undefined && limit !== null) {
- limit = parseInt(limit, 10);
-
- // From 1 to 100
- if (!(1 <= limit && limit <= 100)) {
- return rej('invalid limit range');
- }
- } else {
- limit = 10;
- }
+ const [limit, limitErr] = it(params.limit).expect.number().range(1, 100).default(10).qed();
+ if (limitErr) return rej('invalid limit');
// Get 'offset' parameter
- let offset = params.offset;
- if (offset !== undefined && offset !== null) {
- offset = parseInt(offset, 10);
- } else {
- offset = 0;
- }
+ const [offset, offsetErr] = it(params.offset).expect.number().min(0).default(0).qed();
+ if (offsetErr) return rej('invalid offset');
// Get 'sort' parameter
+ const [sort] = it(params.sort).expect.string().or('desc asc').default('desc').qed();
let sort = params.sort || 'desc';
// Lookup post
diff --git a/src/api/it.ts b/src/api/it.ts
index 5ca8d7dcae..2e6a7a770d 100644
--- a/src/api/it.ts
+++ b/src/api/it.ts
@@ -266,6 +266,30 @@ class StringQuery extends QueryCore {
validate(validator: Validator<string>) {
return super.validate(validator);
}
+
+ /**
+ * このインスタンスの文字列が、与えられたパターン内の文字列のどれかと一致するか検証します
+ * どれとも一致しない場合エラーにします
+ * @param pattern 文字列の配列またはスペースで区切られた文字列
+ */
+ or(pattern: string | string[]) {
+ if (this.error || this.value === null) return this;
+ if (typeof pattern == 'string') pattern = pattern.split(' ');
+ const match = pattern.some(x => x === this.value);
+ if (!match) this.error = new Error('not-match-pattern');
+ return this;
+ }
+
+ /**
+ * このインスタンスの文字列が、与えられた正規表現と一致するか検証します
+ * 一致しない場合エラーにします
+ * @param pattern 正規表現
+ */
+ match(pattern: RegExp) {
+ if (this.error || this.value === null) return this;
+ if (!pattern.test(this.value)) this.error = new Error('not-match-pattern');
+ return this;
+ }
}
class ArrayQuery extends QueryCore {