diff options
| author | syuilo <syuilotan@yahoo.co.jp> | 2017-03-02 05:31:30 +0900 |
|---|---|---|
| committer | syuilo <syuilotan@yahoo.co.jp> | 2017-03-02 05:31:30 +0900 |
| commit | f3f5869f5214085637276b077ece739eac2e5dff (patch) | |
| tree | 0fde0dcf89140791e902f7b85578d55e30600c57 /src/api/validator.ts | |
| parent | Extract hasDuplicates function (diff) | |
| download | sharkey-f3f5869f5214085637276b077ece739eac2e5dff.tar.gz sharkey-f3f5869f5214085637276b077ece739eac2e5dff.tar.bz2 sharkey-f3f5869f5214085637276b077ece739eac2e5dff.zip | |
Add 'set' type
Diffstat (limited to 'src/api/validator.ts')
| -rw-r--r-- | src/api/validator.ts | 12 |
1 files changed, 11 insertions, 1 deletions
diff --git a/src/api/validator.ts b/src/api/validator.ts index 830786a18d..3c426054e8 100644 --- a/src/api/validator.ts +++ b/src/api/validator.ts @@ -1,6 +1,7 @@ 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)[]; @@ -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: 'boolean', isRequired?: boolean): [boolean, 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<T>(value: any, type: Type, isRequired?: boolean, validator?: Validator<T>): [T, string] { if (value === undefined || value === null) { @@ -50,6 +52,14 @@ function validate<T>(value: any, type: Type, isRequired?: boolean, validator?: V } break; + case 'set': + if (!Array.isArray(value)) { + return [null, 'must-be-an-array']; + } else if (hasDuplicates(value)) { + return [null, 'duplicated-contents']; + } + break; + case 'object': if (typeof value != 'object') { return [null, 'must-be-an-onject']; |