Add 'set' type
This commit is contained in:
parent
b3455bf1cb
commit
f3f5869f52
|
@ -4,7 +4,6 @@
|
||||||
* Module dependencies
|
* Module dependencies
|
||||||
*/
|
*/
|
||||||
import validate from '../../validator';
|
import validate from '../../validator';
|
||||||
import hasDuplicates from '../../../common/has-duplicates';
|
|
||||||
import parse from '../../../common/text';
|
import parse from '../../../common/text';
|
||||||
import { Post, isValidText } from '../../models/post';
|
import { Post, isValidText } from '../../models/post';
|
||||||
import User from '../../models/user';
|
import User from '../../models/user';
|
||||||
|
@ -32,10 +31,9 @@ module.exports = (params, user, app) =>
|
||||||
if (textErr) return rej('invalid text');
|
if (textErr) return rej('invalid text');
|
||||||
|
|
||||||
// Get 'media_ids' parameter
|
// Get 'media_ids' parameter
|
||||||
const [mediaIds, mediaIdsErr] = validate(params.media_ids, 'array', false, [
|
const [mediaIds, mediaIdsErr] = validate(params.media_ids, 'set', false,
|
||||||
x => !hasDuplicates(x),
|
|
||||||
x => x.length > 4 ? 'too many media' : true
|
x => x.length > 4 ? 'too many media' : true
|
||||||
]);
|
);
|
||||||
if (mediaIdsErr) return rej('invalid media_ids');
|
if (mediaIdsErr) return rej('invalid media_ids');
|
||||||
|
|
||||||
let files = [];
|
let files = [];
|
||||||
|
@ -135,8 +133,7 @@ module.exports = (params, user, app) =>
|
||||||
|
|
||||||
let poll = null;
|
let poll = null;
|
||||||
if (_poll !== null) {
|
if (_poll !== null) {
|
||||||
const [pollChoices, pollChoicesErr] = validate(params.poll, 'array', false, [
|
const [pollChoices, pollChoicesErr] = validate(params.poll, 'set', false, [
|
||||||
choices => !hasDuplicates(choices),
|
|
||||||
choices => {
|
choices => {
|
||||||
const shouldReject = choices.some(choice => {
|
const shouldReject = choices.some(choice => {
|
||||||
if (typeof choice != 'string') return true;
|
if (typeof choice != 'string') return true;
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
import * as mongo from 'mongodb';
|
import * as mongo from 'mongodb';
|
||||||
|
import hasDuplicates from '../common/has-duplicates';
|
||||||
|
|
||||||
type Type = 'id' | 'string' | 'number' | 'boolean' | 'array' | 'object';
|
type Type = 'id' | 'string' | 'number' | 'boolean' | 'array' | 'set' | 'object';
|
||||||
|
|
||||||
type Validator<T> = ((x: T) => boolean | string) | ((x: T) => boolean | string)[];
|
type Validator<T> = ((x: T) => boolean | string) | ((x: T) => boolean | string)[];
|
||||||
|
|
||||||
|
@ -9,6 +10,7 @@ function validate(value: any, type: 'string', isRequired?: boolean, validator?:
|
||||||
function validate(value: any, type: 'number', isRequired?: boolean, validator?: Validator<number>): [number, string];
|
function validate(value: any, type: 'number', isRequired?: boolean, validator?: Validator<number>): [number, string];
|
||||||
function validate(value: any, type: 'boolean', isRequired?: boolean): [boolean, string];
|
function validate(value: any, type: 'boolean', isRequired?: boolean): [boolean, string];
|
||||||
function validate(value: any, type: 'array', isRequired?: boolean, validator?: Validator<any[]>): [any[], string];
|
function validate(value: any, type: 'array', isRequired?: boolean, validator?: Validator<any[]>): [any[], string];
|
||||||
|
function validate(value: any, type: 'set', isRequired?: boolean, validator?: Validator<Set<any>>): [Set<any>, string];
|
||||||
function validate(value: any, type: 'object', isRequired?: boolean, validator?: Validator<Object>): [Object, string];
|
function validate(value: any, type: 'object', isRequired?: boolean, validator?: Validator<Object>): [Object, string];
|
||||||
function validate<T>(value: any, type: Type, isRequired?: boolean, validator?: Validator<T>): [T, string] {
|
function validate<T>(value: any, type: Type, isRequired?: boolean, validator?: Validator<T>): [T, string] {
|
||||||
if (value === undefined || value === null) {
|
if (value === undefined || value === null) {
|
||||||
|
@ -50,6 +52,14 @@ function validate<T>(value: any, type: Type, isRequired?: boolean, validator?: V
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case 'set':
|
||||||
|
if (!Array.isArray(value)) {
|
||||||
|
return [null, 'must-be-an-array'];
|
||||||
|
} else if (hasDuplicates(value)) {
|
||||||
|
return [null, 'duplicated-contents'];
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
case 'object':
|
case 'object':
|
||||||
if (typeof value != 'object') {
|
if (typeof value != 'object') {
|
||||||
return [null, 'must-be-an-onject'];
|
return [null, 'must-be-an-onject'];
|
||||||
|
|
Loading…
Reference in New Issue