@tellescope/validation 0.0.7 → 0.0.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/cjs/validation.d.ts +78 -1
- package/lib/cjs/validation.d.ts.map +1 -1
- package/lib/cjs/validation.js +52 -39
- package/lib/cjs/validation.js.map +1 -1
- package/lib/esm/validation.d.ts +78 -1
- package/lib/esm/validation.d.ts.map +1 -1
- package/lib/esm/validation.js +49 -37
- package/lib/esm/validation.js.map +1 -1
- package/lib/tsconfig.tsbuildinfo +1 -1
- package/package.json +6 -5
- package/src/validation.ts +122 -38
- package/tsconfig.json +2 -1
package/src/validation.ts
CHANGED
|
@@ -1,6 +1,27 @@
|
|
|
1
|
-
//import "@tellescope/types"
|
|
2
1
|
import { ObjectId } from "bson"
|
|
3
2
|
|
|
3
|
+
import {
|
|
4
|
+
CustomUpdateOptions,
|
|
5
|
+
Indexable,
|
|
6
|
+
JSONType,
|
|
7
|
+
} from "@tellescope/types-utilities"
|
|
8
|
+
|
|
9
|
+
import {
|
|
10
|
+
CustomField,
|
|
11
|
+
Preference,
|
|
12
|
+
JourneyState,
|
|
13
|
+
JourneyStatePriority,
|
|
14
|
+
EmailEncoding,
|
|
15
|
+
ChatRoomTopic,
|
|
16
|
+
ChatRoomType,
|
|
17
|
+
AccountType,
|
|
18
|
+
MessageTemplateType,
|
|
19
|
+
MeetingStatus,
|
|
20
|
+
SessionType,
|
|
21
|
+
AttendeeInfo,
|
|
22
|
+
MeetingInfo,
|
|
23
|
+
} from "@tellescope/types-models"
|
|
24
|
+
|
|
4
25
|
import {
|
|
5
26
|
RequestHandler,
|
|
6
27
|
Request,
|
|
@@ -12,6 +33,7 @@ export const {
|
|
|
12
33
|
isEmail,
|
|
13
34
|
isMobilePhone,
|
|
14
35
|
isMongoId,
|
|
36
|
+
isMimeType,
|
|
15
37
|
} = v
|
|
16
38
|
|
|
17
39
|
// import {
|
|
@@ -25,43 +47,44 @@ import {
|
|
|
25
47
|
to_object_id,
|
|
26
48
|
} from "@tellescope/utilities"
|
|
27
49
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
// interface ValidatorOptionsForList extends ValidatorOptions {
|
|
52
|
-
// listOf: true;
|
|
53
|
-
// }
|
|
54
|
-
// type ValidatorOptionsUnion = ValidatorOptionsForValue | ValidatorOptionsForList
|
|
50
|
+
export interface ValidatorOptions {
|
|
51
|
+
maxLength?: number;
|
|
52
|
+
minLength?: number;
|
|
53
|
+
shouldTruncate?: boolean;
|
|
54
|
+
toLower?: boolean;
|
|
55
|
+
isOptional?: boolean;
|
|
56
|
+
emptyStringOk?: boolean;
|
|
57
|
+
emptyListOk?: boolean;
|
|
58
|
+
nullOk?: boolean;
|
|
59
|
+
isObject?: boolean;
|
|
60
|
+
isNumber?: boolean;
|
|
61
|
+
listOf?: boolean;
|
|
62
|
+
isBoolean?: boolean;
|
|
63
|
+
errorMessage?: string;
|
|
64
|
+
trim?: boolean;
|
|
65
|
+
}
|
|
66
|
+
export interface ValidatorOptionsForValue extends ValidatorOptions {
|
|
67
|
+
listOf?: false;
|
|
68
|
+
}
|
|
69
|
+
export interface ValidatorOptionsForList extends ValidatorOptions {
|
|
70
|
+
listOf: true;
|
|
71
|
+
}
|
|
72
|
+
export type ValidatorOptionsUnion = ValidatorOptionsForValue | ValidatorOptionsForList
|
|
55
73
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
// }
|
|
60
|
-
// type ComplexEscapeBuilder <C,R=any> = (customization: C) => EscapeBuilder<R>
|
|
74
|
+
export type EscapeWithOptions<R=any> = (o: ValidatorOptions) => (v: JSONType) => R
|
|
75
|
+
export type EscapeFunction<R=any> = (v: JSONType) => R
|
|
76
|
+
export type EscapeToList<R=any> = EscapeFunction<R[]>
|
|
61
77
|
|
|
62
|
-
|
|
63
|
-
|
|
78
|
+
export type EscapeBuilder <R=any> = {
|
|
79
|
+
(o?: ValidatorOptionsForValue): EscapeFunction<R>;
|
|
80
|
+
(o?: ValidatorOptionsForList): EscapeFunction<R[]>;
|
|
81
|
+
}
|
|
82
|
+
export type ComplexEscapeBuilder <C,R=any> = (customization: C) => EscapeBuilder<R>
|
|
83
|
+
|
|
84
|
+
export type InputValues <T> = { [K in keyof T]: JSONType }
|
|
85
|
+
export type InputValidation<T> = { [K in keyof T]: EscapeFunction }
|
|
64
86
|
|
|
87
|
+
export const MAX_FILE_SIZE = 25000000 // 25 megabytes in bytes
|
|
65
88
|
const DEFAULT_MAX_LENGTH = 5000
|
|
66
89
|
type BuildValidator_T = {
|
|
67
90
|
(escapeFunction: EscapeFunction, options: ValidatorOptionsForList): EscapeToList;
|
|
@@ -185,9 +208,9 @@ export const objectValidator = <T extends object>(i: InputValidation<Required<T>
|
|
|
185
208
|
for (const field in i) {
|
|
186
209
|
const value = (object as Indexable)[field]
|
|
187
210
|
const escaped = i[field](value) // may be required
|
|
188
|
-
if (escaped
|
|
189
|
-
|
|
190
|
-
|
|
211
|
+
if (escaped === undefined) continue
|
|
212
|
+
|
|
213
|
+
validated[field] = escaped
|
|
191
214
|
}
|
|
192
215
|
|
|
193
216
|
return validated
|
|
@@ -249,6 +272,7 @@ export const listValidator = <T>(b: EscapeFunction<T>): EscapeBuilder<T[]> => o
|
|
|
249
272
|
)
|
|
250
273
|
|
|
251
274
|
export const listOfStringsValidator = listValidator(stringValidator())
|
|
275
|
+
export const listOfObjectAnyFieldsValidator = listValidator(objectAnyFieldsValidator())
|
|
252
276
|
|
|
253
277
|
export const booleanValidator: EscapeBuilder<boolean> = (options={}) => build_validator(
|
|
254
278
|
boolean => {
|
|
@@ -332,6 +356,7 @@ export const numberValidatorBuilder: ComplexEscapeBuilder<{ lower: number, upper
|
|
|
332
356
|
|
|
333
357
|
export const nonNegNumberValidator = numberValidatorBuilder({ lower: 0, upper: 100000000 })
|
|
334
358
|
export const numberValidator = numberValidatorBuilder({ lower: -100000000, upper: 100000000 })
|
|
359
|
+
export const fileSizeValidator = numberValidatorBuilder({ lower: 0, upper: MAX_FILE_SIZE })
|
|
335
360
|
|
|
336
361
|
export const dateValidator: EscapeBuilder<Date> = (options={}) => build_validator(
|
|
337
362
|
(date: any) => {
|
|
@@ -390,6 +415,16 @@ export const phoneValidator: EscapeBuilder<string> = (options={}) => build_valid
|
|
|
390
415
|
{ ...options, maxLength: 25, listOf: false }
|
|
391
416
|
)
|
|
392
417
|
|
|
418
|
+
export const fileTypeValidator: EscapeBuilder<string> = (options={}) => build_validator(
|
|
419
|
+
(s: any) => {
|
|
420
|
+
if (typeof s !== 'string') throw new Error("fileType must be a string")
|
|
421
|
+
if (!isMimeType(s)) throw new Error(`${s} is not a valid file type`)
|
|
422
|
+
|
|
423
|
+
return s
|
|
424
|
+
},
|
|
425
|
+
{ ...options, listOf: false }
|
|
426
|
+
)
|
|
427
|
+
|
|
393
428
|
export const safeBase64Validator = (options={}) => build_validator(
|
|
394
429
|
(sb64: any) => {
|
|
395
430
|
if (typeof sb64 !== 'string') throw new Error("Expecting string")
|
|
@@ -629,8 +664,57 @@ const _MESSAGE_TEMPLATE_TYPES: { [K in MessageTemplateType]: any } = {
|
|
|
629
664
|
export const MESSAGE_TEMPLATE_TYPES = Object.keys(_MESSAGE_TEMPLATE_TYPES) as MessageTemplateType[]
|
|
630
665
|
export const messageTemplateTypeValidator = exactMatchValidator<MessageTemplateType>(MESSAGE_TEMPLATE_TYPES)
|
|
631
666
|
|
|
667
|
+
const _MEETING_STATUSES: { [K in MeetingStatus]: any } = {
|
|
668
|
+
ended: '',
|
|
669
|
+
live: '',
|
|
670
|
+
scheduled: '',
|
|
671
|
+
}
|
|
672
|
+
export const MEETING_STATUSES = Object.keys(_MEETING_STATUSES) as MeetingStatus[]
|
|
673
|
+
export const meetingStatusValidator = exactMatchValidator<MeetingStatus>(MEETING_STATUSES)
|
|
674
|
+
|
|
675
|
+
export const sessionTypeValidator = exactMatchValidator<SessionType>(['user', 'enduser'])
|
|
676
|
+
|
|
632
677
|
export const listOfDisplayNameInfo = listValidator(objectValidator<{ fname: string, lname: string, id: string }>({
|
|
633
678
|
fname: nameValidator(),
|
|
634
679
|
lname: nameValidator(),
|
|
635
680
|
id: listOfMongoIdStringValidator(),
|
|
681
|
+
})())
|
|
682
|
+
|
|
683
|
+
export const attendeeInfoValidator = objectValidator<AttendeeInfo>({
|
|
684
|
+
AttendeeId: stringValidator(),
|
|
685
|
+
ExternalUserId: mongoIdStringValidator(),
|
|
686
|
+
JoinToken: stringValidator(),
|
|
687
|
+
})
|
|
688
|
+
|
|
689
|
+
export const attendeeValidator = objectValidator<{
|
|
690
|
+
type: SessionType,
|
|
691
|
+
id: string,
|
|
692
|
+
info: { Attendee: AttendeeInfo },
|
|
693
|
+
}>({
|
|
694
|
+
type: sessionTypeValidator(),
|
|
695
|
+
id: mongoIdStringValidator(),
|
|
696
|
+
info: attendeeInfoValidator(),
|
|
697
|
+
})
|
|
698
|
+
export const listOfAttendeesValidator = listValidator(attendeeValidator())
|
|
699
|
+
export const meetingInfoValidator = objectValidator<{ Meeting: MeetingInfo }>({
|
|
700
|
+
Meeting: objectAnyFieldsValidator(),
|
|
701
|
+
})
|
|
702
|
+
|
|
703
|
+
export const userIdentityValidator = objectValidator<{
|
|
704
|
+
type: SessionType,
|
|
705
|
+
id: string,
|
|
706
|
+
}>({
|
|
707
|
+
type: sessionTypeValidator(),
|
|
708
|
+
id: mongoIdStringValidator(),
|
|
709
|
+
})
|
|
710
|
+
export const listOfUserIndentitiesValidator = listValidator(userIdentityValidator())
|
|
711
|
+
|
|
712
|
+
export const meetingsListValidator = listValidator(objectValidator<{
|
|
713
|
+
id: string,
|
|
714
|
+
updatedAt: string,
|
|
715
|
+
status: MeetingStatus,
|
|
716
|
+
}>({
|
|
717
|
+
id: mongoIdStringValidator(),
|
|
718
|
+
updatedAt: stringValidator(),
|
|
719
|
+
status: meetingStatusValidator(),
|
|
636
720
|
})())
|
package/tsconfig.json
CHANGED