@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/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
- // type EscapeWithOptions<R=any> = (o: ValidatorOptions) => (v: JSONType) => R
29
- // type EscapeFunction<R=any> = (v: JSONType) => R
30
- // type EscapeToList<R=any> = EscapeFunction<R[]>
31
-
32
- // interface ValidatorOptions {
33
- // maxLength?: number;
34
- // minLength?: number;
35
- // shouldTruncate?: boolean;
36
- // toLower?: boolean;
37
- // isOptional?: boolean;
38
- // emptyStringOk?: boolean;
39
- // emptyListOk?: boolean;
40
- // nullOk?: boolean;
41
- // isObject?: boolean;
42
- // isNumber?: boolean;
43
- // listOf?: boolean;
44
- // isBoolean?: boolean;
45
- // errorMessage?: string;
46
- // trim?: boolean;
47
- // }
48
- // interface ValidatorOptionsForValue extends ValidatorOptions {
49
- // listOf?: false;
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
- // export type EscapeBuilder <R=any> = {
57
- // (o?: ValidatorOptionsForValue): EscapeFunction<R>;
58
- // (o?: ValidatorOptionsForList): EscapeFunction<R[]>;
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
- // type InputValues <T> = { [K in keyof T]: JSONType }
63
- // export type InputValidation<T> = { [K in keyof T]: EscapeFunction }
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) { // omit undefined, optional arguments
189
- validated[field] = escaped
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
@@ -7,8 +7,9 @@
7
7
  "exclude": ["node_modules", "lib"],
8
8
  "include": ["src"],
9
9
  "references": [
10
- { "path": "../types" },
11
10
  { "path": "../constants" },
11
+ { "path": "../types-utilities" },
12
+ { "path": "../types-models" },
12
13
  { "path": "../utilities" }
13
14
  ]
14
15
  }