UNPKG

autotel

Version:
138 lines (135 loc) 4.91 kB
'use strict'; var chunkT5WRA76K_cjs = require('./chunk-T5WRA76K.cjs'); var chunkE6TERL5O_cjs = require('./chunk-E6TERL5O.cjs'); var chunkWJH6IYU2_cjs = require('./chunk-WJH6IYU2.cjs'); var chunkEE6CPXKH_cjs = require('./chunk-EE6CPXKH.cjs'); require('./chunk-ESLWRGAG.cjs'); require('./chunk-YREV3LGG.cjs'); var api = require('@opentelemetry/api'); var mismatchCounter; function counter() { if (!mismatchCounter) { mismatchCounter = chunkWJH6IYU2_cjs.createCounter(chunkT5WRA76K_cjs.VALIDATION_METRICS.mismatches, { description: "Input payloads that did not match their declared shape" }); } return mismatchCounter; } var listeners = /* @__PURE__ */ new Set(); function onValidationMismatch(handler) { listeners.add(handler); return () => { listeners.delete(handler); }; } var truncate = (values) => values.slice(0, chunkT5WRA76K_cjs.VALIDATION_ISSUE_CAP).join(","); function recordValidationMismatch(mismatch) { try { const paths = mismatch.issues.map((i) => i.path).filter(Boolean); const codes = [...new Set(mismatch.issues.map((i) => i.code))]; const span = api.trace.getActiveSpan(); if (span) { span.setAttributes({ [chunkT5WRA76K_cjs.VALIDATION_ATTR.name]: mismatch.name, [chunkT5WRA76K_cjs.VALIDATION_ATTR.boundary]: mismatch.boundary, [chunkT5WRA76K_cjs.VALIDATION_ATTR.mode]: mismatch.mode, [chunkT5WRA76K_cjs.VALIDATION_ATTR.issueCount]: mismatch.issues.length, [chunkT5WRA76K_cjs.VALIDATION_ATTR.issuePaths]: truncate(paths), [chunkT5WRA76K_cjs.VALIDATION_ATTR.issueCodes]: truncate(codes), ...mismatch.hash ? { [chunkT5WRA76K_cjs.VALIDATION_ATTR.hash]: mismatch.hash } : {}, ...mismatch.severity ? { [chunkT5WRA76K_cjs.VALIDATION_ATTR.severity]: mismatch.severity } : {} }); } try { counter().add(1, { boundary: mismatch.boundary, validation: mismatch.name, mode: mismatch.mode }); } catch { } for (const listener of listeners) { try { listener(mismatch); } catch { } } } catch { } } function formatValidationIssues(error) { const raw = extractRawIssues(error); return raw.map((issue) => toSafeIssue(issue)); } function extractRawIssues(error) { if (error && typeof error === "object") { const candidate = error.issues ?? error.errors; if (Array.isArray(candidate)) { return candidate.filter( (i) => i !== null && typeof i === "object" ); } } return []; } function toSafeIssue(issue) { const rawPath = issue.path; const path = Array.isArray(rawPath) ? rawPath.map(String).join(".") : typeof rawPath === "string" ? rawPath : ""; const code = typeof issue.code === "string" ? issue.code : "invalid"; const expected = typeof issue.expected === "string" ? issue.expected : void 0; return expected ? { path, code, expected } : { path, code }; } function defaultRejectError(issues, name) { return chunkEE6CPXKH_cjs.createStructuredError({ name: "ValidationError", status: 400, code: "validation_failed", message: `Input for "${name}" did not match its declared shape.`, why: `${issues.length} field(s) failed validation: ${issues.map((i) => i.path || "(root)").slice(0, chunkT5WRA76K_cjs.VALIDATION_ISSUE_CAP).join(", ")}.`, fix: "Send a payload that matches the schema, or switch this validator to observe mode while you investigate.", // PII-safe: paths + codes only, no received values. details: { validation: name, issues } }); } function defineValidator(name, schema, options = {}) { const mode = options.onMismatch ?? "reject"; const boundary = options.boundary ?? "input"; const hash = options.toJsonSchema ? chunkE6TERL5O_cjs.hashJson(options.toJsonSchema(schema)) : void 0; const record = (issues) => { recordValidationMismatch({ name, boundary, mode, issues, hash, severity: options.severity }); }; return { name, mode, safeParse(input) { const parsed = schema.safeParse(input); if (parsed.success) return { success: true, data: parsed.data }; const issues = formatValidationIssues(parsed.error); record(issues); return { success: false, issues }; }, parse(input) { const parsed = schema.safeParse(input); if (parsed.success) return parsed.data; const issues = formatValidationIssues(parsed.error); record(issues); if (mode === "reject") { throw options.onReject?.(issues, name) ?? defaultRejectError(issues, name); } return input; } }; } exports.defineValidator = defineValidator; exports.formatValidationIssues = formatValidationIssues; exports.onValidationMismatch = onValidationMismatch; exports.recordValidationMismatch = recordValidationMismatch; //# sourceMappingURL=validate.cjs.map //# sourceMappingURL=validate.cjs.map