@sanity/blueprints 0.10.0 → 0.12.0
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/README.md +55 -20
- package/dist/definers/blueprints.d.ts +17 -3
- package/dist/definers/blueprints.js +42 -3
- package/dist/definers/blueprints.js.map +1 -1
- package/dist/definers/cors.d.ts +9 -2
- package/dist/definers/cors.js +21 -2
- package/dist/definers/cors.js.map +1 -1
- package/dist/definers/datasets.d.ts +10 -2
- package/dist/definers/datasets.js +23 -2
- package/dist/definers/datasets.js.map +1 -1
- package/dist/definers/functions.d.ts +58 -53
- package/dist/definers/functions.js +101 -86
- package/dist/definers/functions.js.map +1 -1
- package/dist/definers/resources.d.ts +12 -0
- package/dist/definers/resources.js +23 -0
- package/dist/definers/resources.js.map +1 -1
- package/dist/definers/robotTokens.d.ts +22 -0
- package/dist/definers/robotTokens.js +53 -0
- package/dist/definers/robotTokens.js.map +1 -0
- package/dist/definers/robots.d.ts +2 -18
- package/dist/definers/robots.js +2 -29
- package/dist/definers/robots.js.map +1 -1
- package/dist/definers/roles.d.ts +22 -25
- package/dist/definers/roles.js +70 -23
- package/dist/definers/roles.js.map +1 -1
- package/dist/definers/webhooks.d.ts +9 -2
- package/dist/definers/webhooks.js +29 -2
- package/dist/definers/webhooks.js.map +1 -1
- package/dist/index.d.ts +23 -0
- package/dist/index.js +24 -0
- package/dist/index.js.map +1 -1
- package/dist/types/blueprints.d.ts +16 -4
- package/dist/types/cors.d.ts +6 -0
- package/dist/types/datasets.d.ts +19 -3
- package/dist/types/errors.d.ts +4 -1
- package/dist/types/functions.d.ts +139 -17
- package/dist/types/functions.js +5 -1
- package/dist/types/functions.js.map +1 -1
- package/dist/types/resources.d.ts +17 -0
- package/dist/types/robotTokens.d.ts +45 -0
- package/dist/types/robotTokens.js +2 -0
- package/dist/types/robotTokens.js.map +1 -0
- package/dist/types/robots.d.ts +8 -30
- package/dist/types/robots.js +1 -0
- package/dist/types/robots.js.map +1 -1
- package/dist/types/roles.d.ts +12 -3
- package/dist/types/webhooks.d.ts +23 -5
- package/dist/utils/schedule-parser.d.ts +42 -0
- package/dist/utils/schedule-parser.js +450 -0
- package/dist/utils/schedule-parser.js.map +1 -0
- package/dist/utils/validation.d.ts +1 -0
- package/dist/utils/validation.js +1 -0
- package/dist/utils/validation.js.map +1 -1
- package/dist/validation/blueprints.d.ts +1 -0
- package/dist/validation/blueprints.js +1 -0
- package/dist/validation/blueprints.js.map +1 -1
- package/dist/validation/cors.d.ts +1 -0
- package/dist/validation/cors.js +1 -0
- package/dist/validation/cors.js.map +1 -1
- package/dist/validation/datasets.d.ts +2 -0
- package/dist/validation/datasets.js +2 -0
- package/dist/validation/datasets.js.map +1 -1
- package/dist/validation/functions.d.ts +11 -0
- package/dist/validation/functions.js +37 -1
- package/dist/validation/functions.js.map +1 -1
- package/dist/validation/resources.d.ts +7 -1
- package/dist/validation/resources.js +20 -14
- package/dist/validation/resources.js.map +1 -1
- package/dist/validation/robotTokens.d.ts +15 -0
- package/dist/validation/robotTokens.js +104 -0
- package/dist/validation/robotTokens.js.map +1 -0
- package/dist/validation/robots.d.ts +4 -8
- package/dist/validation/robots.js +5 -96
- package/dist/validation/robots.js.map +1 -1
- package/dist/validation/roles.d.ts +3 -1
- package/dist/validation/roles.js +3 -1
- package/dist/validation/roles.js.map +1 -1
- package/dist/validation/webhooks.d.ts +1 -0
- package/dist/validation/webhooks.js +1 -0
- package/dist/validation/webhooks.js.map +1 -1
- package/package.json +7 -4
- package/src/definers/blueprints.ts +42 -3
- package/src/definers/cors.ts +21 -2
- package/src/definers/datasets.ts +23 -2
- package/src/definers/functions.ts +159 -123
- package/src/definers/resources.ts +23 -0
- package/src/definers/robotTokens.ts +56 -0
- package/src/definers/robots.ts +2 -33
- package/src/definers/roles.ts +70 -23
- package/src/definers/webhooks.ts +29 -2
- package/src/index.ts +25 -0
- package/src/types/blueprints.ts +16 -4
- package/src/types/cors.ts +6 -0
- package/src/types/datasets.ts +19 -3
- package/src/types/errors.ts +4 -1
- package/src/types/functions.ts +156 -16
- package/src/types/resources.ts +17 -1
- package/src/types/robotTokens.ts +50 -0
- package/src/types/robots.ts +10 -35
- package/src/types/roles.ts +12 -3
- package/src/types/webhooks.ts +23 -5
- package/src/utils/schedule-parser.ts +502 -0
- package/src/utils/validation.ts +1 -0
- package/src/validation/blueprints.ts +1 -0
- package/src/validation/cors.ts +1 -0
- package/src/validation/datasets.ts +2 -0
- package/src/validation/functions.ts +37 -0
- package/src/validation/resources.ts +20 -14
- package/src/validation/robotTokens.ts +103 -0
- package/src/validation/robots.ts +5 -95
- package/src/validation/roles.ts +3 -1
- package/src/validation/webhooks.ts +1 -0
|
@@ -0,0 +1,502 @@
|
|
|
1
|
+
import type {BlueprintError} from '../types/errors.js'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Day of week mappings (cron uses 0=Sunday, 1=Monday, etc.)
|
|
5
|
+
*/
|
|
6
|
+
const DAYS_OF_WEEK: Record<string, number> = {
|
|
7
|
+
sunday: 0,
|
|
8
|
+
sun: 0,
|
|
9
|
+
su: 0,
|
|
10
|
+
monday: 1,
|
|
11
|
+
mon: 1,
|
|
12
|
+
mo: 1,
|
|
13
|
+
tuesday: 2,
|
|
14
|
+
tue: 2,
|
|
15
|
+
tu: 2,
|
|
16
|
+
wednesday: 3,
|
|
17
|
+
wed: 3,
|
|
18
|
+
we: 3,
|
|
19
|
+
thursday: 4,
|
|
20
|
+
thu: 4,
|
|
21
|
+
th: 4,
|
|
22
|
+
friday: 5,
|
|
23
|
+
fri: 5,
|
|
24
|
+
fr: 5,
|
|
25
|
+
saturday: 6,
|
|
26
|
+
sat: 6,
|
|
27
|
+
sa: 6,
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Named time mappings (hour, minute)
|
|
32
|
+
*/
|
|
33
|
+
const NAMED_TIMES: Record<string, {hour: number; minute: number}> = {
|
|
34
|
+
midnight: {hour: 0, minute: 0},
|
|
35
|
+
noon: {hour: 12, minute: 0},
|
|
36
|
+
midday: {hour: 12, minute: 0},
|
|
37
|
+
morning: {hour: 9, minute: 0},
|
|
38
|
+
afternoon: {hour: 14, minute: 0},
|
|
39
|
+
evening: {hour: 18, minute: 0},
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Noise words to ignore during parsing
|
|
44
|
+
*/
|
|
45
|
+
const NOISE_WORDS = new Set(['every', 'at', 'on', 'the', 'of', 'in', 'and', 'to'])
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Check if a string looks like a cron expression
|
|
49
|
+
*/
|
|
50
|
+
function isCronExpression(expr: string): boolean {
|
|
51
|
+
return /^[\d*,/-]+(\s+[\d*,/-]+){4}$/.test(expr.trim())
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Result from trying to parse a time token
|
|
56
|
+
*/
|
|
57
|
+
type TimeParseResult = {success: true; hour: number; minute: number} | {success: false; error: BlueprintError} | {success: null} // not a time pattern
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Try to parse a token as a time
|
|
61
|
+
*/
|
|
62
|
+
function tryParseTime(token: string): TimeParseResult {
|
|
63
|
+
const normalized = token.toLowerCase().replace(/\s+/g, '')
|
|
64
|
+
|
|
65
|
+
// Check named times
|
|
66
|
+
if (normalized in NAMED_TIMES) {
|
|
67
|
+
return {success: true, ...NAMED_TIMES[normalized]}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// Just :MM format (e.g., "every hour at :30")
|
|
71
|
+
const minuteOnly = normalized.match(/^:(\d{1,2})$/)
|
|
72
|
+
if (minuteOnly) {
|
|
73
|
+
const minute = parseInt(minuteOnly[1], 10)
|
|
74
|
+
if (minute < 0 || minute > 59) {
|
|
75
|
+
return {
|
|
76
|
+
success: false,
|
|
77
|
+
error: {type: 'invalid_value', message: `Invalid time: minute must be 0-59, got \`${minute}\``},
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
return {success: true, hour: -1, minute} // -1 signals "use current hour context"
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// 12-hour format: 9am, 9:30am, 9:30pm
|
|
84
|
+
const match12hr = normalized.match(/^(\d{1,2})(?::(\d{2}))?(am|pm)$/)
|
|
85
|
+
if (match12hr) {
|
|
86
|
+
const hourRaw = parseInt(match12hr[1], 10)
|
|
87
|
+
const minute = match12hr[2] ? parseInt(match12hr[2], 10) : 0
|
|
88
|
+
const period = match12hr[3]
|
|
89
|
+
|
|
90
|
+
if (hourRaw < 1 || hourRaw > 12) {
|
|
91
|
+
return {
|
|
92
|
+
success: false,
|
|
93
|
+
error: {type: 'invalid_value', message: `Invalid time: hour must be 1-12 for 12-hour format, got \`${hourRaw}\``},
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
if (minute < 0 || minute > 59) {
|
|
97
|
+
return {
|
|
98
|
+
success: false,
|
|
99
|
+
error: {type: 'invalid_value', message: `Invalid time: minute must be 0-59, got \`${minute}\``},
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
let hour = hourRaw
|
|
104
|
+
if (period === 'am') {
|
|
105
|
+
hour = hour === 12 ? 0 : hour
|
|
106
|
+
} else {
|
|
107
|
+
hour = hour === 12 ? 12 : hour + 12
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
return {success: true, hour, minute}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// 24-hour format: 14:30, 09:00
|
|
114
|
+
const match24hr = normalized.match(/^(\d{1,2}):(\d{2})$/)
|
|
115
|
+
if (match24hr) {
|
|
116
|
+
const hour = parseInt(match24hr[1], 10)
|
|
117
|
+
const minute = parseInt(match24hr[2], 10)
|
|
118
|
+
|
|
119
|
+
if (hour < 0 || hour > 23) {
|
|
120
|
+
return {
|
|
121
|
+
success: false,
|
|
122
|
+
error: {type: 'invalid_value', message: `Invalid time: hour must be 0-23 for 24-hour format, got \`${hour}\``},
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
if (minute < 0 || minute > 59) {
|
|
126
|
+
return {
|
|
127
|
+
success: false,
|
|
128
|
+
error: {type: 'invalid_value', message: `Invalid time: minute must be 0-59, got \`${minute}\``},
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
return {success: true, hour, minute}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
return {success: null}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Try to parse two tokens as a time (e.g., "9" + "am" or "9:30" + "am")
|
|
140
|
+
*/
|
|
141
|
+
function tryParseTwoTokenTime(token1: string, token2: string): TimeParseResult {
|
|
142
|
+
const combined = token1 + token2
|
|
143
|
+
return tryParseTime(combined)
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Try to parse a token as a day of week
|
|
148
|
+
*/
|
|
149
|
+
function tryParseDay(token: string): number | null {
|
|
150
|
+
const normalized = token.toLowerCase().replace(/s$/, '') // Remove trailing 's' for plurals
|
|
151
|
+
if (normalized in DAYS_OF_WEEK) {
|
|
152
|
+
return DAYS_OF_WEEK[normalized]
|
|
153
|
+
}
|
|
154
|
+
return null
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Try to parse a day range like "mon-wed" or "monday-friday"
|
|
159
|
+
*/
|
|
160
|
+
function tryParseDayRange(token: string): number[] | null {
|
|
161
|
+
const match = token.toLowerCase().match(/^(\w+)-(\w+)$/)
|
|
162
|
+
if (!match) return null
|
|
163
|
+
|
|
164
|
+
const startDay = tryParseDay(match[1])
|
|
165
|
+
const endDay = tryParseDay(match[2])
|
|
166
|
+
|
|
167
|
+
if (startDay === null || endDay === null) return null
|
|
168
|
+
|
|
169
|
+
// Build range
|
|
170
|
+
const days: number[] = []
|
|
171
|
+
if (startDay <= endDay) {
|
|
172
|
+
for (let d = startDay; d <= endDay; d++) days.push(d)
|
|
173
|
+
} else {
|
|
174
|
+
// Wrap around (e.g., fri-mon = 5,6,0,1)
|
|
175
|
+
for (let d = startDay; d <= 6; d++) days.push(d)
|
|
176
|
+
for (let d = 0; d <= endDay; d++) days.push(d)
|
|
177
|
+
}
|
|
178
|
+
return days
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* Result from trying to parse an ordinal
|
|
183
|
+
*/
|
|
184
|
+
type OrdinalParseResult = {success: true; value: number} | {success: false; error: BlueprintError} | {success: null} // not an ordinal pattern
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* Try to parse an ordinal day of month (1st, 2nd, 15th, etc.)
|
|
188
|
+
*/
|
|
189
|
+
function tryParseOrdinal(token: string): OrdinalParseResult {
|
|
190
|
+
const match = token.match(/^(\d+)(?:st|nd|rd|th)$/)
|
|
191
|
+
if (!match) return {success: null}
|
|
192
|
+
const num = parseInt(match[1], 10)
|
|
193
|
+
if (num < 1 || num > 31) {
|
|
194
|
+
return {
|
|
195
|
+
success: false,
|
|
196
|
+
error: {type: 'invalid_value', message: `Invalid day of month: must be 1-31, got \`${num}\``},
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
return {success: true, value: num}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* Result from trying to parse an interval
|
|
204
|
+
*/
|
|
205
|
+
type IntervalParseResult =
|
|
206
|
+
| {success: true; type: 'minute' | 'hour'; value: number; consumed: number}
|
|
207
|
+
| {success: false; error: BlueprintError}
|
|
208
|
+
| {success: null} // not an interval pattern
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* Try to parse interval like "5 minutes" or "2 hours" from consecutive tokens
|
|
212
|
+
*/
|
|
213
|
+
function tryParseInterval(tokens: string[], index: number): IntervalParseResult {
|
|
214
|
+
if (index >= tokens.length - 1) return {success: null}
|
|
215
|
+
|
|
216
|
+
const num = parseInt(tokens[index], 10)
|
|
217
|
+
if (Number.isNaN(num)) return {success: null}
|
|
218
|
+
|
|
219
|
+
const unit = tokens[index + 1].toLowerCase()
|
|
220
|
+
if (unit === 'minute' || unit === 'minutes') {
|
|
221
|
+
if (num < 1 || num > 59) {
|
|
222
|
+
return {
|
|
223
|
+
success: false,
|
|
224
|
+
error: {type: 'invalid_value', message: `Invalid interval: minutes must be 1-59, got \`${num}\``},
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
return {success: true, type: 'minute', value: num, consumed: 2}
|
|
228
|
+
}
|
|
229
|
+
if (unit === 'hour' || unit === 'hours') {
|
|
230
|
+
if (num < 1 || num > 23) {
|
|
231
|
+
return {
|
|
232
|
+
success: false,
|
|
233
|
+
error: {type: 'invalid_value', message: `Invalid interval: hours must be 1-23, got \`${num}\``},
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
return {success: true, type: 'hour', value: num, consumed: 2}
|
|
237
|
+
}
|
|
238
|
+
return {success: null}
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/**
|
|
242
|
+
* Internal parsing logic shared between validate and parse functions
|
|
243
|
+
*/
|
|
244
|
+
function parseExpressionInternal(expression: string): {
|
|
245
|
+
errors: BlueprintError[]
|
|
246
|
+
cron: string | null
|
|
247
|
+
} {
|
|
248
|
+
const errors: BlueprintError[] = []
|
|
249
|
+
const trimmed = expression.trim()
|
|
250
|
+
|
|
251
|
+
// If it's already a cron expression, return as-is
|
|
252
|
+
if (isCronExpression(trimmed)) {
|
|
253
|
+
return {errors: [], cron: trimmed}
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
// Normalize and tokenize
|
|
257
|
+
const normalized = trimmed.toLowerCase().replace(/\s+/g, ' ')
|
|
258
|
+
// Split on spaces and commas, keep hyphenated tokens together
|
|
259
|
+
const rawTokens = normalized.split(/[\s,]+/).filter(Boolean)
|
|
260
|
+
|
|
261
|
+
// Extract components
|
|
262
|
+
let time: {hour: number; minute: number} | null = null
|
|
263
|
+
const days: number[] = []
|
|
264
|
+
let dayOfMonth: number | null = null
|
|
265
|
+
let interval: {type: 'minute' | 'hour'; value: number} | null = null
|
|
266
|
+
let hasWeekdays = false
|
|
267
|
+
let hasWeekends = false
|
|
268
|
+
let hasDaily = false
|
|
269
|
+
let hasMinute = false
|
|
270
|
+
let hasHour = false
|
|
271
|
+
|
|
272
|
+
// Check for special keywords in original normalized string
|
|
273
|
+
if (/\bweekdays?\b/.test(normalized)) hasWeekdays = true
|
|
274
|
+
if (/\bweekends?\b/.test(normalized)) hasWeekends = true
|
|
275
|
+
if (/\bdaily\b/.test(normalized) || /\bevery\s+day\b/.test(normalized)) hasDaily = true
|
|
276
|
+
if (/\bevery\s+minute\b/.test(normalized) || /\bminutely\b/.test(normalized)) hasMinute = true
|
|
277
|
+
if (/\bevery\s+hour\b/.test(normalized) || /\bhourly\b/.test(normalized)) hasHour = true
|
|
278
|
+
if (/\bfirst\b/.test(normalized) && /\bmonth\b/.test(normalized)) dayOfMonth = 1
|
|
279
|
+
|
|
280
|
+
// Process tokens
|
|
281
|
+
let i = 0
|
|
282
|
+
while (i < rawTokens.length) {
|
|
283
|
+
const token = rawTokens[i]
|
|
284
|
+
|
|
285
|
+
// Skip noise words
|
|
286
|
+
if (NOISE_WORDS.has(token) || token === 'day' || token === 'month') {
|
|
287
|
+
i++
|
|
288
|
+
continue
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
// Try interval (e.g., "5 minutes")
|
|
292
|
+
const parsedInterval = tryParseInterval(rawTokens, i)
|
|
293
|
+
if (parsedInterval.success === true) {
|
|
294
|
+
interval = {type: parsedInterval.type, value: parsedInterval.value}
|
|
295
|
+
i += parsedInterval.consumed
|
|
296
|
+
continue
|
|
297
|
+
} else if (parsedInterval.success === false) {
|
|
298
|
+
errors.push(parsedInterval.error)
|
|
299
|
+
i += 2 // skip past the invalid interval
|
|
300
|
+
continue
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
// Try two-token time first (e.g., "9" + "am" or "9:30" + "pm")
|
|
304
|
+
if (i < rawTokens.length - 1) {
|
|
305
|
+
const twoTokenTime = tryParseTwoTokenTime(token, rawTokens[i + 1])
|
|
306
|
+
if (twoTokenTime.success === true) {
|
|
307
|
+
time = {hour: twoTokenTime.hour, minute: twoTokenTime.minute}
|
|
308
|
+
i += 2
|
|
309
|
+
continue
|
|
310
|
+
} else if (twoTokenTime.success === false) {
|
|
311
|
+
errors.push(twoTokenTime.error)
|
|
312
|
+
i += 2
|
|
313
|
+
continue
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
// Try single-token time
|
|
318
|
+
const parsedTime = tryParseTime(token)
|
|
319
|
+
if (parsedTime.success === true) {
|
|
320
|
+
// Handle :MM format (minute only)
|
|
321
|
+
if (parsedTime.hour === -1) {
|
|
322
|
+
time = {hour: time?.hour ?? 0, minute: parsedTime.minute}
|
|
323
|
+
} else {
|
|
324
|
+
time = {hour: parsedTime.hour, minute: parsedTime.minute}
|
|
325
|
+
}
|
|
326
|
+
i++
|
|
327
|
+
continue
|
|
328
|
+
} else if (parsedTime.success === false) {
|
|
329
|
+
errors.push(parsedTime.error)
|
|
330
|
+
i++
|
|
331
|
+
continue
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
// Try day range (mon-fri)
|
|
335
|
+
const dayRange = tryParseDayRange(token)
|
|
336
|
+
if (dayRange) {
|
|
337
|
+
for (const d of dayRange) {
|
|
338
|
+
if (!days.includes(d)) days.push(d)
|
|
339
|
+
}
|
|
340
|
+
i++
|
|
341
|
+
continue
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
// Try single day
|
|
345
|
+
const parsedDay = tryParseDay(token)
|
|
346
|
+
if (parsedDay !== null) {
|
|
347
|
+
if (!days.includes(parsedDay)) {
|
|
348
|
+
days.push(parsedDay)
|
|
349
|
+
}
|
|
350
|
+
i++
|
|
351
|
+
continue
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
// Try ordinal (1st, 15th)
|
|
355
|
+
const ordinal = tryParseOrdinal(token)
|
|
356
|
+
if (ordinal.success === true) {
|
|
357
|
+
dayOfMonth = ordinal.value
|
|
358
|
+
i++
|
|
359
|
+
continue
|
|
360
|
+
} else if (ordinal.success === false) {
|
|
361
|
+
errors.push(ordinal.error)
|
|
362
|
+
i++
|
|
363
|
+
continue
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
// Try named time period (morning, evening, etc.)
|
|
367
|
+
if (token in NAMED_TIMES) {
|
|
368
|
+
time = NAMED_TIMES[token]
|
|
369
|
+
i++
|
|
370
|
+
continue
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
// Unrecognized token - skip (forgiving)
|
|
374
|
+
i++
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
// Build cron expression
|
|
378
|
+
// Format: minute hour dayOfMonth month dayOfWeek
|
|
379
|
+
|
|
380
|
+
// Handle interval patterns
|
|
381
|
+
if (interval) {
|
|
382
|
+
if (interval.type === 'minute') {
|
|
383
|
+
return {errors, cron: `*/${interval.value} * * * *`}
|
|
384
|
+
}
|
|
385
|
+
if (interval.type === 'hour') {
|
|
386
|
+
return {errors, cron: `0 */${interval.value} * * *`}
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
// Handle every minute
|
|
391
|
+
if (hasMinute) {
|
|
392
|
+
return {errors, cron: '* * * * *'}
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
// Handle every hour
|
|
396
|
+
if (hasHour) {
|
|
397
|
+
const minute = time?.minute ?? 0
|
|
398
|
+
return {errors, cron: `${minute} * * * *`}
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
// Determine day of week field
|
|
402
|
+
let dayOfWeekField = '*'
|
|
403
|
+
if (hasWeekdays) {
|
|
404
|
+
dayOfWeekField = '1-5'
|
|
405
|
+
} else if (hasWeekends) {
|
|
406
|
+
dayOfWeekField = '0,6'
|
|
407
|
+
} else if (days.length > 0) {
|
|
408
|
+
const uniqueDays = [...new Set(days)].sort((a, b) => a - b)
|
|
409
|
+
dayOfWeekField = uniqueDays.join(',')
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
// Determine day of month field
|
|
413
|
+
const dayOfMonthField = dayOfMonth !== null ? String(dayOfMonth) : '*'
|
|
414
|
+
|
|
415
|
+
// If we have a day of month, reset day of week (unless explicitly set)
|
|
416
|
+
if (dayOfMonth !== null && days.length === 0 && !hasWeekdays && !hasWeekends) {
|
|
417
|
+
dayOfWeekField = '*'
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
// Determine time
|
|
421
|
+
const minute = time?.minute ?? 0
|
|
422
|
+
const hour = time?.hour ?? 0
|
|
423
|
+
|
|
424
|
+
// Validate we have something meaningful
|
|
425
|
+
const hasAnySchedule = time || days.length > 0 || hasWeekdays || hasWeekends || hasDaily || dayOfMonth !== null
|
|
426
|
+
if (!hasAnySchedule) {
|
|
427
|
+
// Only add generic "could not parse" if we don't already have specific errors
|
|
428
|
+
if (errors.length === 0) {
|
|
429
|
+
errors.push({
|
|
430
|
+
type: 'invalid_value',
|
|
431
|
+
message: `Could not parse schedule expression \`${trimmed}\``,
|
|
432
|
+
})
|
|
433
|
+
}
|
|
434
|
+
return {errors, cron: null}
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
return {errors, cron: `${minute} ${hour} ${dayOfMonthField} * ${dayOfWeekField}`}
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
/**
|
|
441
|
+
* Validates a schedule expression and returns any errors found.
|
|
442
|
+
*
|
|
443
|
+
* Checks for:
|
|
444
|
+
* - Empty expressions
|
|
445
|
+
* - Invalid time formats (hours, minutes out of range)
|
|
446
|
+
* - Invalid intervals (minutes, hours out of range)
|
|
447
|
+
* - Invalid day of month values
|
|
448
|
+
* - Unrecognizable expressions
|
|
449
|
+
*
|
|
450
|
+
* @param expression Natural language schedule or cron expression
|
|
451
|
+
* @returns Array of validation errors, empty if valid
|
|
452
|
+
* @internal
|
|
453
|
+
*/
|
|
454
|
+
export function validateScheduleExpression(expression: string): BlueprintError[] {
|
|
455
|
+
if (!expression || expression.trim() === '') {
|
|
456
|
+
return [{type: 'invalid_value', message: '`expression` cannot be empty'}]
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
const result = parseExpressionInternal(expression)
|
|
460
|
+
return result.errors
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
/**
|
|
464
|
+
* Parse a natural language schedule expression into a cron expression.
|
|
465
|
+
*
|
|
466
|
+
* The parser is **forgiving** and **freeform** - it extracts recognized parts
|
|
467
|
+
* (days, times, intervals) and ignores unrecognized tokens like "at", "on", "the".
|
|
468
|
+
*
|
|
469
|
+
* Supported patterns:
|
|
470
|
+
* - "every minute", "every 5 minutes", "every hour", "every 2 hours"
|
|
471
|
+
* - "every day at 9am", "daily 14:30", "9am daily"
|
|
472
|
+
* - "at midnight", "noon"
|
|
473
|
+
* - "morning", "evening"
|
|
474
|
+
* - "mondays 9am", "mon wed fri 9:00", "mon-fri 8am"
|
|
475
|
+
* - "9pm fridays and wed", "wednesday friday 9pm"
|
|
476
|
+
* - "weekdays 9am", "weekends 10am"
|
|
477
|
+
* - "first of the month 9am", "15th noon"
|
|
478
|
+
*
|
|
479
|
+
* If the input is already a valid cron expression, it is returned unchanged.
|
|
480
|
+
*
|
|
481
|
+
* This function assumes the expression has already been validated via
|
|
482
|
+
* `validateScheduleExpression`. For invalid input, behavior is undefined.
|
|
483
|
+
*
|
|
484
|
+
* @param expression Natural language schedule or cron expression
|
|
485
|
+
* @returns Cron expression string
|
|
486
|
+
* @internal
|
|
487
|
+
*/
|
|
488
|
+
export function parseScheduleExpression(expression: string): string {
|
|
489
|
+
const trimmed = expression.trim()
|
|
490
|
+
|
|
491
|
+
// Fast path for cron expressions
|
|
492
|
+
if (isCronExpression(trimmed)) {
|
|
493
|
+
return trimmed
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
const result = parseExpressionInternal(expression)
|
|
497
|
+
if (!result.cron) {
|
|
498
|
+
// Should never happen if validateScheduleExpression was called first
|
|
499
|
+
throw new Error(`Failed to parse schedule expression: ${expression}`)
|
|
500
|
+
}
|
|
501
|
+
return result.cron
|
|
502
|
+
}
|
package/src/utils/validation.ts
CHANGED
|
@@ -3,6 +3,7 @@ import type {BlueprintError} from '../types/errors'
|
|
|
3
3
|
/**
|
|
4
4
|
* Executes the given validator function and throws a formatted error if any are returned.
|
|
5
5
|
* @param validator A function that returns a list of validation errors.
|
|
6
|
+
* @internal
|
|
6
7
|
*/
|
|
7
8
|
export function runValidation(validator: () => BlueprintError[]) {
|
|
8
9
|
const errors = validator()
|
|
@@ -3,6 +3,7 @@ import type {BlueprintError} from '../index.js'
|
|
|
3
3
|
/**
|
|
4
4
|
* Validates that the given input is a valid Blueprint
|
|
5
5
|
* @param blueprintConfig The blueprint configuration to be validated
|
|
6
|
+
* @category Validation
|
|
6
7
|
* @returns A list of validation errors
|
|
7
8
|
*/
|
|
8
9
|
export function validateBlueprint(blueprintConfig: unknown): BlueprintError[] {
|
package/src/validation/cors.ts
CHANGED
|
@@ -3,6 +3,7 @@ import {type BlueprintError, validateResource} from '../index.js'
|
|
|
3
3
|
/**
|
|
4
4
|
* Validates that the given resource is a valid CORS origin.
|
|
5
5
|
* @param resource The CORS origin resource
|
|
6
|
+
* @category Validation
|
|
6
7
|
* @returns A list of validation errors
|
|
7
8
|
*/
|
|
8
9
|
export function validateCorsOrigin(resource: unknown): BlueprintError[] {
|
|
@@ -3,6 +3,8 @@ import {type BlueprintError, validateResource} from '../index.js'
|
|
|
3
3
|
/**
|
|
4
4
|
* Validates that the given resource is a valid Dataset.
|
|
5
5
|
* @param resource The Dataset resource
|
|
6
|
+
* @hidden
|
|
7
|
+
* @category Validation
|
|
6
8
|
* @returns A list of validation errors
|
|
7
9
|
*/
|
|
8
10
|
export function validateDataset(resource: unknown): BlueprintError[] {
|
|
@@ -3,8 +3,11 @@ import {
|
|
|
3
3
|
type BlueprintError,
|
|
4
4
|
type BlueprintFunctionBaseResourceEvent,
|
|
5
5
|
type BlueprintMediaLibraryFunctionResourceEvent,
|
|
6
|
+
type FunctionRuntimes,
|
|
7
|
+
VALID_RUNTIMES,
|
|
6
8
|
validateResource,
|
|
7
9
|
} from '../index.js'
|
|
10
|
+
import {validateScheduleExpression} from '../utils/schedule-parser.js'
|
|
8
11
|
|
|
9
12
|
type BaseFunctionEventKey = keyof BlueprintFunctionBaseResourceEvent
|
|
10
13
|
const BASE_EVENT_KEYS = new Set<BaseFunctionEventKey>(['on', 'filter', 'projection', 'includeDrafts'])
|
|
@@ -17,6 +20,7 @@ const MEDIA_LIBRARY_EVENT_KEYS = new Set<MediaLibraryFunctionEventKey>(['resourc
|
|
|
17
20
|
* Validates a document function resource configuration.
|
|
18
21
|
* Checks that the function has a valid event configuration, correct type, and all required base properties.
|
|
19
22
|
* @param functionResource The function resource to validate
|
|
23
|
+
* @category Functions Types
|
|
20
24
|
* @returns Array of validation errors, empty if valid
|
|
21
25
|
*/
|
|
22
26
|
export function validateDocumentFunction(functionResource: unknown): BlueprintError[] {
|
|
@@ -52,6 +56,7 @@ export function validateDocumentFunction(functionResource: unknown): BlueprintEr
|
|
|
52
56
|
* Validates a media library asset function resource configuration.
|
|
53
57
|
* Checks that the function has a valid event configuration with required resource, correct type, and all required base properties.
|
|
54
58
|
* @param functionResource The function resource to validate
|
|
59
|
+
* @category Functions Types
|
|
55
60
|
* @returns Array of validation errors, empty if valid
|
|
56
61
|
*/
|
|
57
62
|
export function validateMediaLibraryAssetFunction(functionResource: unknown): BlueprintError[] {
|
|
@@ -77,6 +82,7 @@ export function validateMediaLibraryAssetFunction(functionResource: unknown): Bl
|
|
|
77
82
|
* Validates base function resource properties.
|
|
78
83
|
* Checks that required fields (name, type) are present and that optional fields have correct types.
|
|
79
84
|
* @param functionResource The function resource to validate
|
|
85
|
+
* @internal
|
|
80
86
|
* @returns Array of validation errors, empty if valid
|
|
81
87
|
*/
|
|
82
88
|
export function validateFunction(functionResource: unknown): BlueprintError[] {
|
|
@@ -115,6 +121,12 @@ export function validateFunction(functionResource: unknown): BlueprintError[] {
|
|
|
115
121
|
}
|
|
116
122
|
}
|
|
117
123
|
|
|
124
|
+
if ('runtime' in functionResource) {
|
|
125
|
+
if (typeof functionResource.runtime !== 'undefined' && !VALID_RUNTIMES.includes(functionResource.runtime as FunctionRuntimes)) {
|
|
126
|
+
errors.push({type: 'invalid_value', message: `\`runtime\` must be one of ${VALID_RUNTIMES.join(', ')}`})
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
118
130
|
return errors
|
|
119
131
|
}
|
|
120
132
|
|
|
@@ -176,6 +188,14 @@ function validateMediaLibraryFunctionEvent(event: unknown): BlueprintError[] {
|
|
|
176
188
|
return errors
|
|
177
189
|
}
|
|
178
190
|
|
|
191
|
+
/**
|
|
192
|
+
* Validates a schedule function resource configuration.
|
|
193
|
+
* @param functionResource The function resource to validate
|
|
194
|
+
* @alpha
|
|
195
|
+
* @hidden
|
|
196
|
+
* @category Functions Types
|
|
197
|
+
* @returns Array of validation errors, empty if valid
|
|
198
|
+
*/
|
|
179
199
|
export function validateScheduleFunction(functionResource: unknown): BlueprintError[] {
|
|
180
200
|
if (!functionResource) return [{type: 'invalid_value', message: 'Function config must be provided'}]
|
|
181
201
|
if (typeof functionResource !== 'object') return [{type: 'invalid_type', message: 'Function config must be an object'}]
|
|
@@ -201,6 +221,11 @@ export function validateScheduleFunction(functionResource: unknown): BlueprintEr
|
|
|
201
221
|
return errors
|
|
202
222
|
}
|
|
203
223
|
|
|
224
|
+
/**
|
|
225
|
+
* Validates a schedule function event configuration.
|
|
226
|
+
* @param event The event configuration to validate
|
|
227
|
+
* @returns Array of validation errors, empty if valid
|
|
228
|
+
*/
|
|
204
229
|
function validateScheduleFunctionEvent(event: unknown): BlueprintError[] {
|
|
205
230
|
if (!event) return [{type: 'invalid_value', message: 'Function event must be provided'}]
|
|
206
231
|
if (typeof event !== 'object') return [{type: 'invalid_type', message: 'Function event must be an object'}]
|
|
@@ -220,6 +245,13 @@ function validateScheduleFunctionEvent(event: unknown): BlueprintError[] {
|
|
|
220
245
|
type: 'missing_parameter',
|
|
221
246
|
message: 'Either `expression` or explicit cron fields (`minute`, `hour`, `dayOfMonth`, `month`, `dayOfWeek`) must be provided',
|
|
222
247
|
})
|
|
248
|
+
} else if (hasExpression) {
|
|
249
|
+
const expr = (event as {expression: unknown}).expression
|
|
250
|
+
if (typeof expr !== 'string') {
|
|
251
|
+
errors.push({type: 'invalid_type', message: '`expression` must be a string'})
|
|
252
|
+
} else {
|
|
253
|
+
errors.push(...validateScheduleExpression(expr))
|
|
254
|
+
}
|
|
223
255
|
} else if (hasExplicitFields) {
|
|
224
256
|
if (!('minute' in event)) {
|
|
225
257
|
errors.push({
|
|
@@ -266,6 +298,11 @@ function validateScheduleFunctionEvent(event: unknown): BlueprintError[] {
|
|
|
266
298
|
return errors
|
|
267
299
|
}
|
|
268
300
|
|
|
301
|
+
/**
|
|
302
|
+
* Validates a schedule function timezone configuration.
|
|
303
|
+
* @param timezone The timezone to validate
|
|
304
|
+
* @returns Array of validation errors, empty if valid
|
|
305
|
+
*/
|
|
269
306
|
function validateScheduleFunctionTimezone(timezone: unknown): BlueprintError[] {
|
|
270
307
|
if (typeof timezone !== 'string') return [{type: 'invalid_type', message: 'Function timezone must be a string'}]
|
|
271
308
|
|