-
Notifications
You must be signed in to change notification settings - Fork 154
/
index.js
139 lines (125 loc) · 4.76 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import * as yup from 'yup'
// yup url validation blocks localhost, so use a more flexible regex instead
// taken from https://github.com/jquense/yup/issues/224#issuecomment-417172609
// This does mean that the url validation error is
// "url must match the following: ...." as opposed to "url must be a valid URL"
export const urlRegex = /^(?:([a-z0-9+.-]+):\/\/)(?:\S+(?::\S*)?@)?(?:(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*\.?)(?::\d{2,5})?(?:[/?#]\S*)?$/
const dateTimeSchema = ({ required }) => yup.lazy((value) => {
if (typeof value === 'number') {
return yup.number().integer().min(0)
}
if (typeof value === 'string') {
return yup.string().required()
}
if (!required && typeof value === 'undefined') {
return yup.mixed().oneOf([undefined])
}
return yup.array().required().min(3).max(7).of(yup.lazy((item, options) => {
const itemIndex = options.parent.indexOf(options.value)
return [
yup.number().integer(),
yup.number().integer().min(1).max(12),
yup.number().integer().min(1).max(31),
yup.number().integer().min(0).max(23),
yup.number().integer().min(0).max(60),
yup.number().integer().min(0).max(60)
][itemIndex]
}))
}
)
const durationSchema = yup.object().shape({
before: yup.boolean(),//option to set before alaram
weeks: yup.number(),
days: yup.number(),
hours: yup.number(),
minutes: yup.number(),
seconds: yup.number()
}).noUnknown()
const contactSchema = yup.object().shape({
name: yup.string(),
email: yup.string().email(),
rsvp: yup.boolean(),
dir: yup.string().matches(urlRegex),
partstat: yup.string(),
role: yup.string(),
cutype: yup.string(),
xNumGuests: yup.number()
}).noUnknown()
const organizerSchema = yup.object().shape({
name: yup.string(),
email: yup.string().email(),
dir: yup.string(),
sentBy: yup.string()
}).noUnknown()
const alarmSchema = yup.object().shape({
action: yup.string().matches(/^(audio|display|email)$/).required(),
trigger: yup.mixed().required(),
description: yup.string(),
duration: durationSchema,
repeat: yup.number(),
attach: yup.string(),
attachType: yup.string(),
summary: yup.string(),
attendee: contactSchema,
'x-prop': yup.mixed(),
'iana-prop': yup.mixed()
}).noUnknown()
const headerShape = {
productId: yup.string(),
method: yup.string(),
calName: yup.string()
}
const headerSchema = yup.object().shape(headerShape).noUnknown()
const eventShape = {
summary: yup.string(),
timestamp: dateTimeSchema({ required: false }),
title: yup.string(),
uid: yup.string(),
sequence: yup.number().integer().max(2_147_483_647),
start: dateTimeSchema({ required: true }),
duration: durationSchema,
startType: yup.string().matches(/^(utc|local)$/),
startInputType: yup.string().matches(/^(utc|local)$/),
startOutputType: yup.string().matches(/^(utc|local)$/),
end: dateTimeSchema({ required: false }),
endInputType: yup.string().matches(/^(utc|local)$/),
endOutputType: yup.string().matches(/^(utc|local)$/),
description: yup.string(),
url: yup.string().matches(urlRegex),
geo: yup.object().shape({lat: yup.number(), lon: yup.number()}),
location: yup.string(),
status: yup.string().matches(/^(TENTATIVE|CANCELLED|CONFIRMED)$/i),
categories: yup.array().of(yup.string()),
organizer: organizerSchema,
attendees: yup.array().of(contactSchema),
alarms: yup.array().of(alarmSchema),
recurrenceRule: yup.string(),
busyStatus: yup.string().matches(/^(TENTATIVE|FREE|BUSY|OOF)$/i),
transp: yup.string().matches(/^(TRANSPARENT|OPAQUE)$/i),
classification: yup.string(),
created: dateTimeSchema({ required: false }),
lastModified: dateTimeSchema({ required: false }),
exclusionDates: yup.array().of(dateTimeSchema({ required: true })),
htmlContent: yup.string()
}
const headerAndEventSchema = yup.object().shape({ ...headerShape, ...eventShape }).test('xor', `object should have end or duration (but not both)`, val => {
const hasEnd = !!val.end
const hasDuration = !!val.duration
return ((hasEnd && !hasDuration) || (!hasEnd && hasDuration) || (!hasEnd && !hasDuration))
}).noUnknown()
export function validateHeader (candidate) {
try {
const value = headerSchema.validateSync(candidate, {abortEarly: false, strict: true})
return {error: null, value}
} catch (error) {
return {error: Object.assign({}, error), value: undefined}
}
}
export function validateHeaderAndEvent (candidate) {
try {
const value = headerAndEventSchema.validateSync(candidate, {abortEarly: false, strict: true})
return {error: null, value}
} catch (error) {
return {error: Object.assign({}, error), value: undefined}
}
}