@tellescope/validation 0.0.51 → 0.0.54

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
@@ -18,7 +18,6 @@ import {
18
18
  JourneyState,
19
19
  JourneyStatePriority,
20
20
  EmailEncoding,
21
- ChatRoomTopic,
22
21
  ChatRoomType,
23
22
  AccountType,
24
23
  MessageTemplateType,
@@ -28,6 +27,32 @@ import {
28
27
  MeetingInfo,
29
28
  CUDSubscription,
30
29
  FormField,
30
+ AutomationEventType,
31
+ AutomationActionType,
32
+ AutomationEvent,
33
+ AutomationAction,
34
+ EnterStateAutomationEvent,
35
+ LeaveStateAutomationEvent,
36
+ FormResponseAutomationEvent,
37
+ AutomationForForm,
38
+ AutomationForAutomation,
39
+ AutomationForMessage,
40
+ AutomationForFormRequest,
41
+ AutomationForNotification,
42
+ AutomationForJourneyAndState,
43
+ AutomationForWebhook,
44
+ SendEmailAutomationAction,
45
+ SendSMSAutomationAction,
46
+ SendFormAutomationAction,
47
+ // CreateTaskAutomationAction,
48
+ SendNotificationAutomationAction,
49
+ SendWebhookAutomationAction,
50
+ UpdateStateForJourneyAutomationAction,
51
+ RemoveFromSequenceAutomationAction,
52
+ AddToSequenceAutomationAction,
53
+ AutomationEnduserStatus,
54
+ AutomationForTemplate,
55
+ CreateTaskAutomationAction,
31
56
  } from "@tellescope/types-models"
32
57
  import {
33
58
  UserDisplayInfo,
@@ -217,6 +242,18 @@ export const binaryOrValidator = <A, B>(f1: EscapeFunction<A>, f2: EscapeFunctio
217
242
  },
218
243
  { ...o, listOf: false }
219
244
  )
245
+ export const orValidator = <T>(escapeFunctions: { [K in keyof T]: EscapeFunction<T[K]> }): EscapeBuilder<T[keyof T]> => (o={}) => build_validator(
246
+ value => {
247
+ for (const field in escapeFunctions) {
248
+ const escape = escapeFunctions[field]
249
+ try {
250
+ return escape(value)
251
+ } catch(err) { continue }
252
+ }
253
+ throw 'Value does not match any of the expected options'
254
+ },
255
+ { ...o, listOf: false }
256
+ )
220
257
 
221
258
  export const filterCommandsValidator: EscapeBuilder<FilterType> = (o={}) => build_validator(
222
259
  (value: any) => {
@@ -318,8 +355,12 @@ export const SMSMessageValidator: EscapeBuilder<string> = (o={}) => build_valida
318
355
  export const listValidator = <T>(b: EscapeFunction<T>): EscapeBuilder<T[]> => o => build_validator(
319
356
  b, { ...o, listOf: true }
320
357
  )
358
+ export const listValidatorEmptyOk = <T>(b: EscapeFunction<T>): EscapeBuilder<T[]> => o => build_validator(
359
+ b, { ...o, listOf: true, emptyListOk: true }
360
+ )
321
361
 
322
362
  export const listOfStringsValidator = listValidator(stringValidator())
363
+ export const listOfStringsValidatorEmptyOk = listValidatorEmptyOk(stringValidator())
323
364
  export const listOfObjectAnyFieldsValidator = listValidator(objectAnyFieldsValidator())
324
365
 
325
366
  export const booleanValidator: EscapeBuilder<boolean> = (options={}) => build_validator(
@@ -833,13 +874,13 @@ export const listOfFormFieldsValidator: EscapeBuilder<FormField[]> = (options={}
833
874
  )
834
875
 
835
876
 
836
- // to ensure all topics in type have coverage at compile-time
837
- const _CHAT_ROOM_TOPICS: { [K in ChatRoomTopic]: any } = {
838
- task: '',
839
- enduser: '',
840
- }
841
- export const CHAT_ROOM_TOPICS = Object.keys(_CHAT_ROOM_TOPICS) as ChatRoomTopic[]
842
- export const chatRoomTopicValidator = exactMatchValidator<ChatRoomTopic>(CHAT_ROOM_TOPICS)
877
+ // // to ensure all topics in type have coverage at compile-time
878
+ // const _CHAT_ROOM_TOPICS: { [K in ChatRoomTopic]: any } = {
879
+ // task: '',
880
+ // enduser: '',
881
+ // }
882
+ // export const CHAT_ROOM_TOPICS = Object.keys(_CHAT_ROOM_TOPICS) as ChatRoomTopic[]
883
+ // export const chatRoomTopicValidator = exactMatchValidator<ChatRoomTopic>(CHAT_ROOM_TOPICS)
843
884
 
844
885
  // to ensure all topics in type have coverage at compile-time
845
886
  const _CHAT_ROOM_TYPES: { [K in ChatRoomType]: any } = {
@@ -949,4 +990,89 @@ export const userDisplayInfoValidator = objectValidator<UserDisplayInfo>({
949
990
  lastLogout: dateValidator(),
950
991
  email: emailValidator(),
951
992
  })
952
- export const meetingDisplayInfoValidator = indexableValidator(mongoIdStringRequired, userDisplayInfoValidator())
993
+ export const meetingDisplayInfoValidator = indexableValidator(mongoIdStringRequired, userDisplayInfoValidator())
994
+
995
+ const _AUTOMATION_ENDUSER_STATUS: { [K in AutomationEnduserStatus]: any } = {
996
+ active: '',
997
+ paused: '',
998
+ finished: '',
999
+ }
1000
+ export const AUTOMATION_ENDUSER_STATUS = Object.keys(_AUTOMATION_ENDUSER_STATUS) as AutomationEnduserStatus[]
1001
+ export const automationEnduserStatusValidator = exactMatchValidator<AutomationEnduserStatus>(AUTOMATION_ENDUSER_STATUS)
1002
+
1003
+ const _AUTOMATION_EVENTS: { [K in AutomationEventType]: any } = {
1004
+ enterState: '',
1005
+ formResponse: '',
1006
+ leaveState: '',
1007
+ }
1008
+ export const AUTOMATION_EVENTS = Object.keys(_AUTOMATION_EVENTS) as AutomationEventType[]
1009
+ export const automationEventTypeValidator = exactMatchValidator<AutomationEventType>(AUTOMATION_EVENTS)
1010
+
1011
+ const _AUTOMATION_ACTIONS: { [K in AutomationActionType]: any } = {
1012
+ addToSequence: '',
1013
+ removeFromSequence: '',
1014
+ createTask: '',
1015
+ sendEmail: '',
1016
+ sendSMS: '',
1017
+ sendForm: '',
1018
+ sendNotification: '',
1019
+ updateStateForJourney: '',
1020
+ sendWebhook: '',
1021
+ }
1022
+ export const AUTOMATION_ACTIONS = Object.keys(_AUTOMATION_ACTIONS) as AutomationActionType[]
1023
+ export const automationActionTypeValidator = exactMatchValidator<AutomationActionType>(AUTOMATION_ACTIONS)
1024
+
1025
+ export const automationEventValidator = orValidator<{ [K in AutomationEventType]: AutomationEvent & { type: K } } >({
1026
+ enterState: objectValidator<EnterStateAutomationEvent>({
1027
+ type: exactMatchValidator(['enterState'])(),
1028
+ info: objectValidator<AutomationForJourneyAndState>({ state: stringValidator100(), journeyId: mongoIdStringRequired })(),
1029
+ })(),
1030
+ leaveState: objectValidator<LeaveStateAutomationEvent>({
1031
+ type: exactMatchValidator(['leaveState'])(),
1032
+ info: objectValidator<AutomationForJourneyAndState>({ state: stringValidator100(), journeyId: mongoIdStringRequired })(),
1033
+ })(),
1034
+ formResponse: objectValidator<FormResponseAutomationEvent>({
1035
+ type: exactMatchValidator(['formResponse'])(),
1036
+ info: objectValidator<AutomationForForm>({ formId: mongoIdStringValidator() })(),
1037
+ })(),
1038
+ })
1039
+
1040
+ export const automationActionValidator = orValidator<{ [K in AutomationActionType]: AutomationAction & { type: K } } >({
1041
+ sendEmail: objectValidator<SendEmailAutomationAction>({
1042
+ type: exactMatchValidator(['sendEmail'])(),
1043
+ info: objectValidator<AutomationForMessage>({ senderId: mongoIdStringValidator(), templateId: mongoIdStringValidator() })(),
1044
+ })(),
1045
+ sendSMS: objectValidator<SendSMSAutomationAction>({
1046
+ type: exactMatchValidator(['sendSMS'])(),
1047
+ info: objectValidator<AutomationForMessage>({ senderId: mongoIdStringValidator(), templateId: mongoIdStringValidator() })(),
1048
+ })(),
1049
+ sendForm: objectValidator<SendFormAutomationAction>({
1050
+ type: exactMatchValidator(['sendForm'])(),
1051
+ info: objectValidator<AutomationForFormRequest>({ senderId: mongoIdStringValidator(), formId: mongoIdStringValidator() })(),
1052
+ })(),
1053
+ createTask: objectValidator<CreateTaskAutomationAction>({
1054
+ type: exactMatchValidator(['createTask'])(),
1055
+ info: objectValidator<AutomationForTemplate>({ templateId: mongoIdStringValidator() })(),
1056
+ })(),
1057
+ sendNotification: objectValidator<SendNotificationAutomationAction>({
1058
+ type: exactMatchValidator(['sendNotification'])(),
1059
+ info: objectValidator<AutomationForNotification>({ templateId: mongoIdStringRequired, destination: stringValidator5000() })(),
1060
+ })(),
1061
+ sendWebhook: objectValidator<SendWebhookAutomationAction>({
1062
+ type: exactMatchValidator(['sendWebhook'])(),
1063
+ info: objectValidator<AutomationForWebhook>({ message: stringValidator5000() })(),
1064
+ })(),
1065
+ updateStateForJourney: objectValidator<UpdateStateForJourneyAutomationAction>({
1066
+ type: exactMatchValidator(['updateStateForJourney'])(),
1067
+ info: objectValidator<AutomationForJourneyAndState>({ journeyId: mongoIdStringRequired, state: stringValidator100() })(),
1068
+ })(),
1069
+ addToSequence: objectValidator<AddToSequenceAutomationAction>({
1070
+ type: exactMatchValidator(['addToSequence'])(),
1071
+ info: objectValidator<AutomationForAutomation>({ automationId: mongoIdStringValidator() })(),
1072
+ })(),
1073
+ removeFromSequence: objectValidator<RemoveFromSequenceAutomationAction>({
1074
+ type: exactMatchValidator(['removeFromSequence'])(),
1075
+ info: objectValidator<AutomationForAutomation>({ automationId: mongoIdStringValidator() })(),
1076
+ })(),
1077
+ })
1078
+