@sanity/blueprints 0.9.0 → 0.11.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 +326 -14
- package/dist/definers/blueprints.d.ts +2 -0
- package/dist/definers/blueprints.js +2 -0
- package/dist/definers/blueprints.js.map +1 -1
- package/dist/definers/cors.d.ts +4 -0
- package/dist/definers/cors.js +4 -0
- package/dist/definers/cors.js.map +1 -1
- package/dist/definers/datasets.d.ts +5 -0
- package/dist/definers/datasets.js +5 -0
- package/dist/definers/datasets.js.map +1 -1
- package/dist/definers/functions.d.ts +41 -12
- package/dist/definers/functions.js +60 -16
- package/dist/definers/functions.js.map +1 -1
- package/dist/definers/resources.d.ts +2 -0
- package/dist/definers/resources.js +2 -0
- package/dist/definers/resources.js.map +1 -1
- package/dist/definers/robots.d.ts +4 -0
- package/dist/definers/robots.js +4 -0
- package/dist/definers/robots.js.map +1 -1
- package/dist/definers/roles.d.ts +6 -0
- package/dist/definers/roles.js +6 -0
- package/dist/definers/roles.js.map +1 -1
- package/dist/definers/webhooks.d.ts +4 -0
- package/dist/definers/webhooks.js +4 -0
- package/dist/definers/webhooks.js.map +1 -1
- package/dist/index.d.ts +20 -0
- package/dist/index.js +21 -0
- package/dist/index.js.map +1 -1
- package/dist/types/blueprints.d.ts +16 -4
- package/dist/types/cors.d.ts +4 -0
- package/dist/types/datasets.d.ts +14 -2
- package/dist/types/errors.d.ts +4 -1
- package/dist/types/functions.d.ts +81 -16
- package/dist/types/functions.js +5 -1
- package/dist/types/functions.js.map +1 -1
- package/dist/types/resources.d.ts +16 -1
- package/dist/types/robots.d.ts +12 -3
- package/dist/types/roles.d.ts +11 -3
- package/dist/types/webhooks.d.ts +18 -6
- 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 +23 -16
- package/dist/validation/resources.js.map +1 -1
- package/dist/validation/robots.d.ts +7 -0
- package/dist/validation/robots.js +7 -0
- 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 +4 -1
- package/src/definers/blueprints.ts +2 -0
- package/src/definers/cors.ts +4 -0
- package/src/definers/datasets.ts +5 -0
- package/src/definers/functions.ts +65 -18
- package/src/definers/resources.ts +2 -0
- package/src/definers/robots.ts +4 -0
- package/src/definers/roles.ts +6 -0
- package/src/definers/webhooks.ts +4 -0
- package/src/index.ts +22 -0
- package/src/types/blueprints.ts +16 -4
- package/src/types/cors.ts +4 -0
- package/src/types/datasets.ts +14 -2
- package/src/types/errors.ts +4 -1
- package/src/types/functions.ts +93 -15
- package/src/types/resources.ts +16 -1
- package/src/types/robots.ts +12 -3
- package/src/types/roles.ts +11 -3
- package/src/types/webhooks.ts +18 -6
- 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 +24 -16
- package/src/validation/robots.ts +7 -0
- package/src/validation/roles.ts +3 -1
- package/src/validation/webhooks.ts +1 -0
package/src/types/robots.ts
CHANGED
|
@@ -1,10 +1,15 @@
|
|
|
1
|
-
import type {BlueprintResource} from './resources'
|
|
1
|
+
import type {BlueprintProjectResourceLifecycle, BlueprintResource} from './resources'
|
|
2
2
|
|
|
3
|
-
/**
|
|
3
|
+
/**
|
|
4
|
+
* Resource types that robots can be attached to.
|
|
5
|
+
* @internal
|
|
6
|
+
*/
|
|
4
7
|
export type RobotResourceType = 'organization' | 'project'
|
|
5
8
|
|
|
6
9
|
/**
|
|
7
10
|
* Defines the robot's roles within a given resource.
|
|
11
|
+
* @beta This feature is subject to breaking changes.
|
|
12
|
+
* @category Resource Types
|
|
8
13
|
*/
|
|
9
14
|
export interface RobotMembership {
|
|
10
15
|
resourceType: RobotResourceType
|
|
@@ -15,8 +20,10 @@ export interface RobotMembership {
|
|
|
15
20
|
/**
|
|
16
21
|
* A robot that provides a token for automated access.
|
|
17
22
|
* @see https://www.sanity.io/docs/content-lake/http-auth#k4c21d7b829fe
|
|
23
|
+
* @beta This feature is subject to breaking changes.
|
|
24
|
+
* @category Resource Types
|
|
18
25
|
*/
|
|
19
|
-
export interface BlueprintRobotResource extends BlueprintResource {
|
|
26
|
+
export interface BlueprintRobotResource extends BlueprintResource<BlueprintProjectResourceLifecycle> {
|
|
20
27
|
type: 'sanity.access.robot'
|
|
21
28
|
/** A descriptive label for the robot and its use case */
|
|
22
29
|
label: string
|
|
@@ -29,6 +36,8 @@ export interface BlueprintRobotResource extends BlueprintResource {
|
|
|
29
36
|
/**
|
|
30
37
|
* Configuration for a robot that provides a token for automated access.
|
|
31
38
|
* @see https://www.sanity.io/docs/content-lake/http-auth#k4c21d7b829fe
|
|
39
|
+
* @beta This feature is subject to breaking changes.
|
|
40
|
+
* @category Resource Types
|
|
32
41
|
*/
|
|
33
42
|
export type BlueprintRobotConfig = Omit<BlueprintRobotResource, 'type' | 'label' | 'token'> & {
|
|
34
43
|
label?: string
|
package/src/types/roles.ts
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
|
-
import type {BlueprintResource} from '../index.js'
|
|
1
|
+
import type {BlueprintProjectResourceLifecycle, BlueprintResource} from '../index.js'
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* A permission definition for a role.
|
|
5
|
+
* @beta This feature is subject to breaking changes.
|
|
6
|
+
* @category Resource Types
|
|
5
7
|
*/
|
|
6
8
|
export interface RolePermission {
|
|
7
9
|
/** Predefined permission name (e.g., 'sanity-all-documents') */
|
|
@@ -14,8 +16,10 @@ export interface RolePermission {
|
|
|
14
16
|
|
|
15
17
|
/**
|
|
16
18
|
* Configuration for a custom role.
|
|
19
|
+
* @beta This feature is subject to breaking changes.
|
|
20
|
+
* @category Resource Types
|
|
17
21
|
*/
|
|
18
|
-
export interface BlueprintRoleConfig extends Omit<BlueprintResource
|
|
22
|
+
export interface BlueprintRoleConfig extends Omit<BlueprintResource<BlueprintProjectResourceLifecycle>, 'type'> {
|
|
19
23
|
title: string
|
|
20
24
|
description?: string
|
|
21
25
|
appliesToUsers: boolean
|
|
@@ -25,13 +29,17 @@ export interface BlueprintRoleConfig extends Omit<BlueprintResource, 'type'> {
|
|
|
25
29
|
|
|
26
30
|
/**
|
|
27
31
|
* A custom role resource
|
|
32
|
+
* @beta This feature is subject to breaking changes.
|
|
33
|
+
* @category Resource Types
|
|
28
34
|
*/
|
|
29
|
-
export interface BlueprintRoleResource extends BlueprintRoleConfig, BlueprintResource {
|
|
35
|
+
export interface BlueprintRoleResource extends BlueprintRoleConfig, BlueprintResource<BlueprintProjectResourceLifecycle> {
|
|
30
36
|
type: 'sanity.access.role'
|
|
31
37
|
}
|
|
32
38
|
|
|
33
39
|
/**
|
|
34
40
|
* A custom role resource that is tied to a specific project
|
|
41
|
+
* @beta This feature is subject to breaking changes.
|
|
42
|
+
* @category Resource Types
|
|
35
43
|
*/
|
|
36
44
|
export interface BlueprintProjectRoleResource extends BlueprintRoleResource {
|
|
37
45
|
resourceType: 'project'
|
package/src/types/webhooks.ts
CHANGED
|
@@ -1,10 +1,18 @@
|
|
|
1
|
-
import type {BlueprintResource} from '../index.js'
|
|
1
|
+
import type {BlueprintProjectResourceLifecycle, BlueprintResource} from '../index.js'
|
|
2
2
|
|
|
3
|
-
/**
|
|
3
|
+
/**
|
|
4
|
+
* Types of events that can trigger a webhook
|
|
5
|
+
* @beta This feature is subject to breaking changes.
|
|
6
|
+
* @category Resource Types
|
|
7
|
+
*/
|
|
4
8
|
export type WebhookTrigger = 'create' | 'update' | 'delete'
|
|
5
9
|
|
|
6
|
-
/**
|
|
7
|
-
|
|
10
|
+
/**
|
|
11
|
+
* A webhook resource definition
|
|
12
|
+
* @beta This feature is subject to breaking changes.
|
|
13
|
+
* @category Resource Types
|
|
14
|
+
*/
|
|
15
|
+
export interface BlueprintDocumentWebhookResource extends BlueprintResource<BlueprintProjectResourceLifecycle> {
|
|
8
16
|
type: 'sanity.project.webhook'
|
|
9
17
|
project?: string
|
|
10
18
|
displayName?: string
|
|
@@ -19,9 +27,13 @@ export interface BlueprintDocumentWebhookResource extends BlueprintResource {
|
|
|
19
27
|
includeDrafts?: boolean
|
|
20
28
|
includeAllVersions?: boolean
|
|
21
29
|
secret?: string
|
|
22
|
-
dataset
|
|
30
|
+
dataset: string
|
|
23
31
|
apiVersion: string
|
|
24
32
|
}
|
|
25
33
|
|
|
26
|
-
/**
|
|
34
|
+
/**
|
|
35
|
+
* Configuration for a webhook
|
|
36
|
+
* @beta This feature is subject to breaking changes.
|
|
37
|
+
* @category Resource Types
|
|
38
|
+
*/
|
|
27
39
|
export type BlueprintDocumentWebhookConfig = Omit<BlueprintDocumentWebhookResource, 'type'>
|
|
@@ -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[] {
|