diff options
Diffstat (limited to 'server/helpers/custom-validators/misc.ts')
-rw-r--r-- | server/helpers/custom-validators/misc.ts | 84 |
1 files changed, 48 insertions, 36 deletions
diff --git a/server/helpers/custom-validators/misc.ts b/server/helpers/custom-validators/misc.ts index 229e9f03c..528bfcfb8 100644 --- a/server/helpers/custom-validators/misc.ts +++ b/server/helpers/custom-validators/misc.ts | |||
@@ -2,6 +2,7 @@ import 'multer' | |||
2 | import { UploadFilesForCheck } from 'express' | 2 | import { UploadFilesForCheck } from 'express' |
3 | import { sep } from 'path' | 3 | import { sep } from 'path' |
4 | import validator from 'validator' | 4 | import validator from 'validator' |
5 | import { isShortUUID, shortToUUID } from '../uuid' | ||
5 | 6 | ||
6 | function exists (value: any) { | 7 | function exists (value: any) { |
7 | return value !== undefined && value !== null | 8 | return value !== undefined && value !== null |
@@ -50,42 +51,7 @@ function isIntOrNull (value: any) { | |||
50 | return value === null || validator.isInt('' + value) | 51 | return value === null || validator.isInt('' + value) |
51 | } | 52 | } |
52 | 53 | ||
53 | function toIntOrNull (value: string) { | 54 | // --------------------------------------------------------------------------- |
54 | const v = toValueOrNull(value) | ||
55 | |||
56 | if (v === null || v === undefined) return v | ||
57 | if (typeof v === 'number') return v | ||
58 | |||
59 | return validator.toInt('' + v) | ||
60 | } | ||
61 | |||
62 | function toBooleanOrNull (value: any) { | ||
63 | const v = toValueOrNull(value) | ||
64 | |||
65 | if (v === null || v === undefined) return v | ||
66 | if (typeof v === 'boolean') return v | ||
67 | |||
68 | return validator.toBoolean('' + v) | ||
69 | } | ||
70 | |||
71 | function toValueOrNull (value: string) { | ||
72 | if (value === 'null') return null | ||
73 | |||
74 | return value | ||
75 | } | ||
76 | |||
77 | function toArray (value: any) { | ||
78 | if (value && isArray(value) === false) return [ value ] | ||
79 | |||
80 | return value | ||
81 | } | ||
82 | |||
83 | function toIntArray (value: any) { | ||
84 | if (!value) return [] | ||
85 | if (isArray(value) === false) return [ validator.toInt(value) ] | ||
86 | |||
87 | return value.map(v => validator.toInt(v)) | ||
88 | } | ||
89 | 55 | ||
90 | function isFileFieldValid ( | 56 | function isFileFieldValid ( |
91 | files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[], | 57 | files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[], |
@@ -160,6 +126,51 @@ function isFileValid ( | |||
160 | 126 | ||
161 | // --------------------------------------------------------------------------- | 127 | // --------------------------------------------------------------------------- |
162 | 128 | ||
129 | function toCompleteUUID (value: string) { | ||
130 | if (isShortUUID(value)) return shortToUUID(value) | ||
131 | |||
132 | return value | ||
133 | } | ||
134 | |||
135 | function toIntOrNull (value: string) { | ||
136 | const v = toValueOrNull(value) | ||
137 | |||
138 | if (v === null || v === undefined) return v | ||
139 | if (typeof v === 'number') return v | ||
140 | |||
141 | return validator.toInt('' + v) | ||
142 | } | ||
143 | |||
144 | function toBooleanOrNull (value: any) { | ||
145 | const v = toValueOrNull(value) | ||
146 | |||
147 | if (v === null || v === undefined) return v | ||
148 | if (typeof v === 'boolean') return v | ||
149 | |||
150 | return validator.toBoolean('' + v) | ||
151 | } | ||
152 | |||
153 | function toValueOrNull (value: string) { | ||
154 | if (value === 'null') return null | ||
155 | |||
156 | return value | ||
157 | } | ||
158 | |||
159 | function toArray (value: any) { | ||
160 | if (value && isArray(value) === false) return [ value ] | ||
161 | |||
162 | return value | ||
163 | } | ||
164 | |||
165 | function toIntArray (value: any) { | ||
166 | if (!value) return [] | ||
167 | if (isArray(value) === false) return [ validator.toInt(value) ] | ||
168 | |||
169 | return value.map(v => validator.toInt(v)) | ||
170 | } | ||
171 | |||
172 | // --------------------------------------------------------------------------- | ||
173 | |||
163 | export { | 174 | export { |
164 | exists, | 175 | exists, |
165 | isArrayOf, | 176 | isArrayOf, |
@@ -169,6 +180,7 @@ export { | |||
169 | isIdValid, | 180 | isIdValid, |
170 | isSafePath, | 181 | isSafePath, |
171 | isUUIDValid, | 182 | isUUIDValid, |
183 | toCompleteUUID, | ||
172 | isIdOrUUIDValid, | 184 | isIdOrUUIDValid, |
173 | isDateValid, | 185 | isDateValid, |
174 | toValueOrNull, | 186 | toValueOrNull, |