From 3f918d6f703c15579e9d3e7203a72c0c3d985985 Mon Sep 17 00:00:00 2001 From: syuilo Date: Fri, 3 Mar 2017 05:52:12 +0900 Subject: [PATCH] wip --- .../endpoints/posts/{likes.js => likes.ts} | 30 +++++-------------- src/api/it.ts | 24 +++++++++++++++ 2 files changed, 32 insertions(+), 22 deletions(-) rename src/api/endpoints/posts/{likes.js => likes.ts} (64%) diff --git a/src/api/endpoints/posts/likes.js b/src/api/endpoints/posts/likes.ts similarity index 64% rename from src/api/endpoints/posts/likes.js rename to src/api/endpoints/posts/likes.ts index 67898218c..5c18679d7 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 5ca8d7dca..2e6a7a770 100644 --- a/src/api/it.ts +++ b/src/api/it.ts @@ -266,6 +266,30 @@ class StringQuery extends QueryCore { validate(validator: Validator) { 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 {