-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathpricing.validator.js
More file actions
270 lines (228 loc) · 7.83 KB
/
Copy pathpricing.validator.js
File metadata and controls
270 lines (228 loc) · 7.83 KB
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/**
* Pricing Configuration Validator
*
* Validates pricing configuration at application startup to catch errors early.
* Ensures:
* - All prices are in valid format ($X.XX)
* - All prices are positive numbers
* - All required fields are present
* - No duplicate endpoints
* - Consistent agent references
*/
/**
* Validate a single price string
* @param {string} price - Price in format '$X.XX'
* @returns {Object} { valid: boolean, error?: string, value?: number }
*/
export function validatePrice(price) {
if (typeof price !== 'string') {
return { valid: false, error: `Price must be a string, got ${typeof price}` }
}
// Check for negative sign
if (price.includes('-')) {
return { valid: false, error: `Price cannot be negative: '${price}'` }
}
const priceRegex = /^\$\d+\.\d{2}$/
if (!priceRegex.test(price)) {
return {
valid: false,
error: `Price must be in format '$X.XX', got '${price}'`,
}
}
const numValue = parseFloat(price.slice(1))
if (isNaN(numValue)) {
return { valid: false, error: `Price value is not a valid number: '${price}'` }
}
if (numValue === 0) {
return { valid: false, error: `Price cannot be zero: '${price}'` }
}
return { valid: true, value: numValue }
}
/**
* Validate endpoint format
* @param {string} endpoint - Endpoint path, e.g., 'GET /api/premium/research'
* @returns {Object} { valid: boolean, error?: string }
*/
export function validateEndpoint(endpoint) {
if (typeof endpoint !== 'string') {
return { valid: false, error: `Endpoint must be a string, got ${typeof endpoint}` }
}
const endpointRegex = /^(GET|POST|PUT|DELETE|PATCH)\s+\/api\/premium\/\w+$/
if (!endpointRegex.test(endpoint)) {
return {
valid: false,
error: `Endpoint must match pattern 'METHOD /api/premium/name', got '${endpoint}'`,
}
}
return { valid: true }
}
/**
* Validate endpoint info object
* @param {string} endpoint - Endpoint path
* @param {Object} info - Endpoint info object
* @returns {Object} { valid: boolean, errors: string[] }
*/
export function validateEndpointInfo(endpoint, info) {
const errors = []
if (!info || typeof info !== 'object') {
return { valid: false, errors: [`Endpoint info must be an object, got ${typeof info}`] }
}
// Validate price
if (!info.price) {
errors.push(`Missing 'price' for endpoint '${endpoint}'`)
} else {
const priceValidation = validatePrice(info.price)
if (!priceValidation.valid) {
errors.push(`Invalid price for '${endpoint}': ${priceValidation.error}`)
}
}
// Validate agent
if (!info.agent) {
errors.push(`Missing 'agent' for endpoint '${endpoint}'`)
} else if (typeof info.agent !== 'string') {
errors.push(`Agent must be a string for '${endpoint}', got ${typeof info.agent}`)
} else if (!info.agent.match(/^[\w-]+$/)) {
errors.push(
`Agent name must be alphanumeric with hyphens for '${endpoint}', got '${info.agent}'`
)
}
// Validate description
if (!info.description) {
errors.push(`Missing 'description' for endpoint '${endpoint}'`)
} else if (typeof info.description !== 'string') {
errors.push(`Description must be a string for '${endpoint}', got ${typeof info.description}`)
}
// Validate emoji (optional but if present, should be valid)
if (info.emoji && typeof info.emoji !== 'string') {
errors.push(`Emoji must be a string for '${endpoint}', got ${typeof info.emoji}`)
}
return { valid: errors.length === 0, errors }
}
/**
* Validate entire pricing configuration
* @param {Object} pricingConfig - Pricing configuration object
* @returns {Object} { valid: boolean, errors: string[] }
*/
export function validatePricingConfig(pricingConfig) {
const errors = []
if (!pricingConfig || typeof pricingConfig !== 'object') {
return { valid: false, errors: ['Pricing config must be an object'] }
}
if (!pricingConfig.endpoints || typeof pricingConfig.endpoints !== 'object') {
return { valid: false, errors: ['Pricing config must have an "endpoints" object'] }
}
const endpoints = Object.keys(pricingConfig.endpoints)
if (endpoints.length === 0) {
errors.push('Pricing config must have at least one endpoint')
}
// Track seen agents and prices for consistency checks
const seenAgents = new Set()
const seenEndpoints = new Set()
for (const [endpoint, info] of Object.entries(pricingConfig.endpoints)) {
// Validate endpoint format
const endpointValidation = validateEndpoint(endpoint)
if (!endpointValidation.valid) {
errors.push(`Invalid endpoint format: ${endpointValidation.error}`)
}
// Check for duplicates
if (seenEndpoints.has(endpoint)) {
errors.push(`Duplicate endpoint: '${endpoint}'`)
}
seenEndpoints.add(endpoint)
// Validate endpoint info
const infoValidation = validateEndpointInfo(endpoint, info)
if (!infoValidation.valid) {
errors.push(...infoValidation.errors)
}
// Check for duplicate agents
if (info.agent && seenAgents.has(info.agent)) {
errors.push(`Duplicate agent: '${info.agent}' used in multiple endpoints`)
}
if (info.agent) {
seenAgents.add(info.agent)
}
}
return { valid: errors.length === 0, errors }
}
/**
* Validate x402 configuration compatibility
* @param {Object} pricingConfig - Pricing configuration
* @param {Object} config - Application config with network and payTo
* @returns {Object} { valid: boolean, errors: string[] }
*/
export function validateX402Compatibility(pricingConfig, config) {
const errors = []
if (!config.network) {
errors.push('Config must have "network" property for x402 compatibility')
}
if (!config.payTo) {
errors.push('Config must have "payTo" property (server address) for x402 compatibility')
}
if (config.payTo && typeof config.payTo !== 'string') {
errors.push(`Config "payTo" must be a string, got ${typeof config.payTo}`)
}
return { valid: errors.length === 0, errors }
}
/**
* Comprehensive validation with detailed error reporting
* @param {Object} pricingConfig - Pricing configuration
* @param {Object} config - Application config
* @returns {Object} { valid: boolean, errors: string[], warnings: string[] }
*/
export function validateAll(pricingConfig, config) {
const errors = []
const warnings = []
// Validate pricing config structure
const pricingValidation = validatePricingConfig(pricingConfig)
if (!pricingValidation.valid) {
errors.push(...pricingValidation.errors)
}
// Validate x402 compatibility
if (config && config.network) {
const x402Validation = validateX402Compatibility(pricingConfig, config)
if (!x402Validation.valid) {
errors.push(...x402Validation.errors)
}
}
return {
valid: errors.length === 0,
errors,
warnings,
}
}
/**
* Format validation errors for console output
* @param {Object} validation - Validation result from validateAll()
* @returns {string} Formatted error message
*/
export function formatValidationErrors(validation) {
if (validation.valid) {
return '✅ Pricing configuration is valid'
}
let message = '❌ Pricing configuration validation failed:\n'
if (validation.errors.length > 0) {
message += '\nErrors:\n'
validation.errors.forEach((err, i) => {
message += ` ${i + 1}. ${err}\n`
})
}
if (validation.warnings.length > 0) {
message += '\nWarnings:\n'
validation.warnings.forEach((warn, i) => {
message += ` ${i + 1}. ${warn}\n`
})
}
return message
}
/**
* Throw error if validation fails
* @param {Object} validation - Validation result
* @param {string} context - Context for error message
* @throws {Error} If validation failed
*/
export function throwIfInvalid(validation, context = 'Pricing configuration') {
if (!validation.valid) {
const message = formatValidationErrors(validation)
throw new Error(`${context} validation failed:\n${message}`)
}
}